package org.gcube.portlets.user.workspace.client.view.panels;

import org.gcube.portlets.user.workspace.client.ConstantsPortlet;
import org.gcube.portlets.user.workspace.client.view.ExplorerPanel;
import org.gcube.portlets.user.workspace.client.view.GxtDetailsContainer;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.BoxComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;


/**
 * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
 *
 */
public class GxtBorderLayoutPanel extends ContentPanel {
	private ContentPanel north = new ContentPanel();
	private ContentPanel west = new ContentPanel();
	private ContentPanel center = new ContentPanel();
	private ContentPanel east = new ContentPanel();
	private ContentPanel south = new ContentPanel();
	private GxtDetailsContainer detailsContainer = null;
	private ExplorerPanel expPanel = null; //TODO change position
	private GxtSeachAndFilterPanel searchAndFilterContainer;
	private GxtCardLayoutResultPanel gxtCardLayoutResultPanel;

	
	public GxtBorderLayoutPanel(
			GxtSeachAndFilterPanel searchAndFilterContainer2,
			ExplorerPanel explorerPanel,
			GxtCardLayoutResultPanel gxtCardLayoutResultPanel,
			GxtDetailsContainer detailsContainer2) {
		
		this.searchAndFilterContainer = searchAndFilterContainer2;
		this.expPanel = explorerPanel;
		this.gxtCardLayoutResultPanel = gxtCardLayoutResultPanel;
		this.detailsContainer = detailsContainer2;
		
		this.initLayout();
		this.createLayouts();
	}

	private void initLayout(){
		
		north.setId("NorthPanel");
		north.setLayout(new FitLayout());
		west.setId("WestPanel");
		west.setLayout(new FitLayout());
		center.setId("CenterPanel");
		center.setLayout(new FitLayout());
	    center.setHeaderVisible(false);
		east.setId("EastPanel");
		center.setScrollMode(Scroll.AUTOX);
		center.setBorders(false);
		
	    north.setHeaderVisible(false);
	    west.setHeading(ConstantsPortlet.EXPLORER);
	    south.setHeading(ConstantsPortlet.DETAILS);
	    
	    south.setLayout(new FitLayout());
	    
	    east.setVisible(false);
	    south.setVisible(false);

		this.createLayouts();
	}
		
	public void createLayouts(){
		
		final BorderLayout borderLayout = new BorderLayout();
		setLayout(borderLayout);
		//setStyleAttribute("padding", "10px");
		setHeaderVisible(false);

//		center.add(this.gridFilter);

	    BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 55, 55, 70);  
	    northData.setCollapsible(true);  
	    
	    northData.setSplit(true);  //Split bar between regions
//	    northData.setFloatable(true);  
	    northData.setCollapsible(true);  
//	    northData.setHideCollapseTool(false);  
//	    northData.setSplit(true);  
	    northData.setMargins(new Margins(0, 0, 1, 0));  
	  
	    BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 330,330,400);  
	    westData.setSplit(true);  
	    westData.setCollapsible(true);  
	    westData.setMargins(new Margins(0,1,0,0));
	  
	    BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(0));
	  
	    BorderLayoutData eastData = new BorderLayoutData(LayoutRegion.EAST, 150,50,150);  
        eastData.setSplit(true);  
	    eastData.setCollapsible(true);  
	    eastData.setMargins(new Margins(0,0,0,1));  
	  
	    BorderLayoutData southData = new BorderLayoutData(LayoutRegion.SOUTH, 140,140,140);  
	    southData.setSplit(true);  
	    southData.setCollapsible(true);  
	    southData.setMargins(new Margins(1, 0, 0, 0));  
	    
	    north.add(this.searchAndFilterContainer);
	    west.add(this.expPanel);
	    
//		center.add(this.toolbarContainer);
	    
	    center.add(this.gxtCardLayoutResultPanel);
		
	    south.add(this.detailsContainer);
	    
	    west.addListener(Events.Resize, new Listener<BoxComponentEvent>(){

			@Override
			public void handleEvent(BoxComponentEvent be) {

			    expPanel.getAsycTreePanel().setSizeTreePanel(expPanel.getWidth()-13, expPanel.getHeight()-39);
			    
			    if(expPanel.getSmartFolderPanel()!=null)
			    	expPanel.getSmartFolderPanel().setSizeSmartPanel(expPanel.getWidth()-2, expPanel.getHeight()-29);		
			    
			}
			
		});

	    add(north, northData);  
	    add(west, westData); 
	    add(center, centerData);  
	    add(east, eastData);
	}
}