package com.dev.swms.client.ui.page;

import java.util.HashMap;
import java.util.Map;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.ToolTip;
import com.gwtext.client.widgets.grid.EditorGridPanel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.event.GridCellListenerAdapter;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.dev.swms.client.ui.constant.CT_IV;
import com.dev.swms.client.ui.data.Page_IV_DataAccessor;
import com.dev.swms.client.ui.javabean.JB_Location_IV;
import com.dev.swms.client.ui.javabean.JB_Location_RC_IV;
import com.dev.swms.client.ui.util.Sub_L_Image_Panel_Util;
import com.dev.energy.client.message.IMessagePage;

/**
 *@author  dev
 *@version $Project:swms $Date$Version:V1.1
 */
public class Sub_L_Image_Panel extends Sub_Abstract_Panel {
	
	private EditorGridPanel leftPanel;
	private EditorGridPanel rightTopPanel;
	private EditorGridPanel rightBottomPanel;
	
	public Sub_L_Image_Panel(IMessagePage page) {
		super(page, Sub_L_Image_Panel_Util.DEFAULT_NAME);
		this.initialize();
		
		this.setSize(Sub_IV_Panel.DEFAULT_WIDTH , Sub_IV_Panel.DEFAULT_HEIGHT);
		this.setBorder(false);
		this.setBodyBorder(false);
    	this.setLayout(new BorderLayout());
    	
        Panel centerPanelWrappper = new Panel();
        centerPanelWrappper.setWidth(Sub_IV_Panel.DEFAULT_WIDTH);
        centerPanelWrappper.setHeight(Sub_IV_Panel.DEFAULT_HEIGHT*2/3);
        centerPanelWrappper.setLayout(new FitLayout());
        centerPanelWrappper.setBorder(false);
        centerPanelWrappper.setBodyBorder(false);
        centerPanelWrappper.setPaddings(1);
        
        leftPanel.setAutoDestroy(true);
        Panel introPanel = new Panel();
        introPanel.setWidth(Sub_IV_Panel.DEFAULT_WIDTH);
        introPanel.setLayout(new HorizontalLayout(0));
        introPanel.setAutoDestroy(true);
        introPanel.setBodyBorder(false);
        introPanel.setBorder(false);
        centerPanelWrappper.add(introPanel);
        
        introPanel.add(this.leftPanel , new BorderLayoutData(RegionPosition.WEST));
        introPanel.add(this.rightTopPanel , new BorderLayoutData(RegionPosition.CENTER));
        
        BorderLayoutData northLayoutData = new BorderLayoutData(RegionPosition.NORTH);
        northLayoutData.setMinSize(10);
        northLayoutData.setMaxSize(Sub_IV_Panel.DEFAULT_HEIGHT);
        northLayoutData.setSplit(false);
        
        this.add(centerPanelWrappper,northLayoutData);
        this.add(this.rightBottomPanel, new BorderLayoutData(RegionPosition.CENTER));
		this.initLocationDatas();
	}
	
	protected void initLocationDatas() {
		this.curIVDataAccessor().initLocationIvDatas();
	}
	
	public Page_IV_DataAccessor curIVDataAccessor() {
		return (Page_IV_DataAccessor)super.getData();
	}
	
	protected void initialize() {
		this.initLeftPanel();
		this.initRightTopPanel();
		this.initRightBottomPanel();
	}
	
	protected void initLeftPanel(){
		leftPanel = new EditorGridPanel();
		leftPanel.setBodyBorder(true);
		leftPanel.setBorder(true);
		leftPanel.setWidth(Sub_IV_Panel.DEFAULT_WIDTH/2);
		leftPanel.setHeight(Sub_IV_Panel.DEFAULT_HEIGHT*2/3);
		leftPanel.setLayout(new FitLayout());
		leftPanel.setColumnModel(Sub_L_Image_Panel_Util.getALColumnModel());
		leftPanel.setStore(Sub_L_Image_Panel_Util.getALStore(new Object[][]{}));
		leftPanel.addGridCellListener(new GridCellListenerAdapter(){
			public void onCellClick(GridPanel grid, int rowIndex, int colindex, EventObject e) {
				initRightTopPanelDatas(rowIndex,colindex);
		    }
		});
		leftPanel.setEnableHdMenu(false);
	}
	
	protected void initRightTopPanel(){
		rightTopPanel = new EditorGridPanel();
		rightTopPanel.setBodyBorder(true);
		rightTopPanel.setBorder(true);
		rightTopPanel.setLayout(new FitLayout());
		rightTopPanel.setWidth(Sub_IV_Panel.DEFAULT_WIDTH/2);
		rightTopPanel.setHeight(Sub_IV_Panel.DEFAULT_HEIGHT*2/3);
		rightTopPanel.setAutoScroll(true);
		rightTopPanel.setColumnModel(Sub_L_Image_Panel_Util.getRCColumnModel());
		rightTopPanel.setStore(Sub_L_Image_Panel_Util.getRCStore(new Object[][]{}));
		rightTopPanel.addGridCellListener(new GridCellListenerAdapter(){
			public void onCellClick(GridPanel grid, int rowIndex, int colindex, EventObject e) {
				initRightBottomPanelDatas(rowIndex , colindex);
		    }
		});
		rightTopPanel.setEnableHdMenu(false);
	}
	
	protected void initRightBottomPanel(){
		rightBottomPanel = new EditorGridPanel();
		rightBottomPanel.setBodyBorder(true);
		rightBottomPanel.setBorder(true);
		rightBottomPanel.setWidth(Sub_IV_Panel.DEFAULT_WIDTH);
		rightBottomPanel.setHeight(Sub_IV_Panel.DEFAULT_HEIGHT/3);
		rightBottomPanel.setLayout(new FitLayout());
		rightBottomPanel.setAutoScroll(true);
		rightBottomPanel.stripeRows(true);
		rightBottomPanel.setColumnModel(Sub_L_Image_Panel_Util.getInvColumnModel());
		rightBottomPanel.setStore(Sub_L_Image_Panel_Util.getInvStore(new Object[][]{}));
	}
	
	protected void initRightTopPanelDatas(int row,int col){
		if(col == 0) return;
		boolean isNaN = curIVDataAccessor().all_Location_IV().get(row).get(col - 1).getLoc_rate() < 0;
		if(isNaN) return;
		Map<String,Object> params = new HashMap<String,Object>();
		params.put(CT_IV.IV_LINE , row + 1);
		params.put(CT_IV.IV_AISLE , col);
		params.put(CT_IV.IV_ZONE_ID , this.curDataAccessor().getCur_JB_Zone_IV().getLoc_id());
		this.curIVDataAccessor().initLocationIvRCDatas(params);
	}
	
	protected void initRightBottomPanelDatas(int row,int col) {
		if(col == 0) return;
		JB_Location_RC_IV liv = curIVDataAccessor().all_Location_RC_IV().get(row).get(col - 1);
		if(liv.getId() == null || liv.getRate() <=0) return;
		Map<String,Object> params = new HashMap<String,Object>();
		params.put(CT_IV.IV_LOCATION_ID , liv.getId() );
		this.curIVDataAccessor().initInventoryDatas(params);
	}
	
	protected void applyToToolTip() {
		for(int i = 0 ; i < CT_IV.DEFAULT_ROW ; i ++) {
			for(int j = 1 ; j <= CT_IV.DEFAULT_COL ; j ++) {
				JB_Location_RC_IV lriv = this.curIVDataAccessor().all_Location_RC_IV().get(i).get(j - 1);
				if(lriv.getId() == null || lriv.getRate() < 0) continue;
				new ToolTip(Sub_L_Image_Panel_Util.generateToolTip(lriv)).applyTo(this.rightTopPanel.getView().getCell(i,j));
			}
		}
	}
	
	protected void applyToToolTip2(){
		for(int i = 0 ; i < CT_IV.DEFAULT_LINE ; i ++) {
			for(int j = 1 ; j <= CT_IV.DEFAULT_AISLE ; j ++) {
				JB_Location_IV lriv = this.curIVDataAccessor().all_Location_IV().get(i).get(j - 1);
				if(lriv.getLoc_rate() < 0) continue;
				new ToolTip(Sub_L_Image_Panel_Util.generateToolTip(lriv)).applyTo(this.leftPanel.getView().getCell(i,j));
			}
		}
	}
	
	public void doDispath(String message){
		super.doDispath(message);
		if(Page_IV_DataAccessor.MSG_INIT_LOCATION_DATAS.equals(message)){
			leftPanel.reconfigure(Sub_L_Image_Panel_Util.getALStore(
					Sub_L_Image_Panel_Util.convertToAL2Array(this.curIVDataAccessor().all_Location_IV())) , 
					Sub_L_Image_Panel_Util.getALColumnModel());applyToToolTip2();

		}else if(Page_IV_DataAccessor.MSG_INIT_LOCATION_RC_DATAS.equals(message)) {
			rightTopPanel.reconfigure(Sub_L_Image_Panel_Util.getRCStore(
					Sub_L_Image_Panel_Util.convertToRC2Array(this.curIVDataAccessor().all_Location_RC_IV())) , 
					Sub_L_Image_Panel_Util.getRCColumnModel());applyToToolTip();
			
		}else if(Page_IV_DataAccessor.MSG_INIT_INVENTORY_DATAS.equals(message)) {
			rightBottomPanel.reconfigure(Sub_L_Image_Panel_Util.getInvStore(
					Sub_L_Image_Panel_Util.convertToInv2Array(this.curIVDataAccessor().all_Location_Inv())), 
					Sub_L_Image_Panel_Util.getInvColumnModel());
		}
	}
}
