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

import java.util.List;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Window;
import com.gwtext.client.core.Ext;
import com.gwtext.client.core.TextAlign;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.grid.CellMetadata;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.Renderer;
import com.dev.swms.client.ui.constant.CT_IV;
import com.dev.swms.client.ui.javabean.JB_Location_IV;
import com.dev.swms.client.ui.javabean.JB_Location_IV_List;
import com.dev.swms.client.ui.javabean.JB_Location_Inventory;
import com.dev.swms.client.ui.javabean.JB_Location_RC_IV;
import com.dev.swms.client.ui.javabean.JB_Location_RC_IV_List;
import com.dev.energy.client.utils.LocaleUtils;

/**
 *@author  dev
 *@version $Project:swms $Date$Version:V1.1
 */
public class Sub_L_Image_Panel_Util {
	
	public static final int DEFAULT_WIDTH = Window.getClientWidth() - 100 ;
	public static final int DEFAULT_HEIGHT = Window.getClientHeight() - 100;
	public static final String DEFAULT_NAME = "l_image_panel";
	public static final int DEFAULT_PROGRESS_WIDTH = 60;
	public static final int DEFAULT_INVENTORY_COLUMN_WIDTH = 90;
	public static final int DEFAULT_INVENTORY_ROW_HEIGHT = 20;
	public static ColumnModel AL_MODEL  , RC_MODEL , INV_MODEL;
	public static String[] RC_COLUMN_DEFINES , AL_COLUMN_DEFINES ;
	
	private static void initALColumnDefine(){
		AL_COLUMN_DEFINES = new String[CT_IV.DEFAULT_AISLE + 1];
		AL_COLUMN_DEFINES[0] = "line";
		for(int i = 1 ; i <= CT_IV.DEFAULT_AISLE ; i++) {
			AL_COLUMN_DEFINES[i] = "aisle_" + i;
		}
	}
	
	private static void initRCColumnDefine(){
		RC_COLUMN_DEFINES = new String[CT_IV.DEFAULT_COL + 1];
		RC_COLUMN_DEFINES[0] = "row";
		for(int i = 1 ; i <= CT_IV.DEFAULT_COL ; i++) {
			RC_COLUMN_DEFINES[i] = "col_" + i;
		}
	}
	
	private static void initRCColumnConfig(){
		if(RC_COLUMN_DEFINES == null) initRCColumnDefine();
		ColumnConfig[] rc_configs = new ColumnConfig[CT_IV.DEFAULT_COL + 1] ;
		rc_configs[0] = new ColumnConfig("层\\列" , RC_COLUMN_DEFINES[0],40);
		rc_configs[0].setResizable(false);
		for(int i = 1 ; i <= CT_IV.DEFAULT_COL ; i++) {
			rc_configs[i] = new ColumnConfig(LocaleUtils.getText(RC_COLUMN_DEFINES[i]) , RC_COLUMN_DEFINES[i] , DEFAULT_PROGRESS_WIDTH);
			rc_configs[i].setRenderer(new Renderer(){
				public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {
					return generateProgressHTML(value,cellMetadata);
				}
			});
			rc_configs[i].setAlign(TextAlign.LEFT);
			rc_configs[i].setResizable(false);
		}
		RC_MODEL = new ColumnModel(rc_configs);
	}
	
	private static void initALColumnConfig(){
		if(AL_COLUMN_DEFINES == null) initALColumnDefine();
		ColumnConfig[] al_configs= new ColumnConfig[CT_IV.DEFAULT_AISLE + 1] ;
		al_configs[0] = new ColumnConfig("排\\道" , AL_COLUMN_DEFINES[0],40);
		al_configs[0].setResizable(false);
		for(int i = 1 ; i <= CT_IV.DEFAULT_AISLE ; i++) {
			al_configs[i] = new ColumnConfig(LocaleUtils.getText(AL_COLUMN_DEFINES[i]) , AL_COLUMN_DEFINES[i] , DEFAULT_PROGRESS_WIDTH);
			al_configs[i].setRenderer(new Renderer(){
				public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {
					return generateProgressHTML(value,cellMetadata);
				}
			});
			al_configs[i].setAlign(TextAlign.LEFT);
			al_configs[i].setResizable(false);
		}
		AL_MODEL = new ColumnModel(al_configs);
	}
	
	private static void initInvColumnConfig(){
		String[] properties = JB_Location_Inventory.propertyToArray();
		ColumnConfig[] configs= new ColumnConfig[properties.length] ;
		for(int i = 0 ; i < properties.length ; i++) {
			configs[i] = new ColumnConfig(JB_Location_Inventory.localizedToArray()[i] , properties[i] , DEFAULT_INVENTORY_COLUMN_WIDTH);
			configs[i].setRenderer(new Renderer(){
				public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {
					cellMetadata.setHtmlAttribute("style='height:18px;border-left:1px solid #d0d0d0;'");
					return value.toString();
				}
			});
		}
		INV_MODEL = new ColumnModel(configs);
	}
	
	public static ColumnModel getInvColumnModel(){
		 if(INV_MODEL == null){
			 initInvColumnConfig();
		 }
		 return INV_MODEL;
	}
	
	private static String generateProgressHTML(Object value ,  CellMetadata cellMetadata) {
		int realWidth = Ext.isIE()?DEFAULT_PROGRESS_WIDTH : DEFAULT_PROGRESS_WIDTH - 10;
		int realHeght = Ext.isIE()?DEFAULT_INVENTORY_ROW_HEIGHT : DEFAULT_INVENTORY_ROW_HEIGHT - 2;
		if(value == null || "".equals(value)){
			return "";
		}
		Double realValue = Double.valueOf(value.toString());
		//不存在库位,背景为灰色
		if(realValue < 0) {
			cellMetadata.setHtmlAttribute("style='height:" + realHeght + "px;background:#d0d0d0;BORDER-LEFT: #d0d0d0 1px solid;'");
			return "";
		}
		//存在库位，但是容积率为0,背景为白色
		if(realValue == 0) {
			cellMetadata.setHtmlAttribute("style='BORDER-LEFT: #d0d0d0 1px solid;" + " height:" + realHeght + "px;width:" + realWidth + "px'");
			return "";
		}
		realHeght = Ext.isIE()? realHeght - 1 : (realHeght - 2);
		realValue = realValue >= 1? realWidth : realWidth*realValue;
		cellMetadata.setHtmlAttribute(
					"style='BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #d0ecff 1px solid; " +
					"BORDER-LEFT: #d0ecff 1px solid; COLOR: #ffff00; BORDER-BOTTOM: #000000 1px solid; BACKGROUND-COLOR: #0099ff;" +
					"height:" + realHeght + "px;width:" + 
					NumberFormat.getFormat(CT_IV.DEFAULT_INTEGER_FORMAT).format(realValue) + "px'");
		return "";
	}
	
	
	public static ColumnModel getRCColumnModel(){
		if(RC_MODEL == null) {
			initRCColumnConfig();
		}
		return RC_MODEL;
	}
	
	public static ColumnModel getALColumnModel(){
		if(AL_MODEL == null) {
			initALColumnConfig();
		}
		return AL_MODEL;
	}
	
	public static Store getALStore(Object[][] objss){
		SimpleStore store = new SimpleStore(AL_COLUMN_DEFINES,objss);
		store.load();
		return store;
	}
	
	public static Store getRCStore(Object[][] objss) {
		SimpleStore store = new SimpleStore(RC_COLUMN_DEFINES,objss);
		store.load();
		return store;
	}
	
	public static Store getInvStore(Object[][] objss) {
		SimpleStore store = new SimpleStore(JB_Location_Inventory.propertyToArray(),objss);
		store.load();
		return store;
	}
	
	public static Object[][] convertToAL2Array(List<JB_Location_IV_List> livs) {
		Object[][] objss = new Object[CT_IV.DEFAULT_LINE][];
		for(int line = 0; line < CT_IV.DEFAULT_LINE ; line++) {
			objss[line] = livs.get(line).toArray();
		}
		return objss;
	}
	
	public static Object[][] convertToRC2Array(List<JB_Location_RC_IV_List> lrivs) {
		Object[][] objss = new Object[CT_IV.DEFAULT_ROW][];
		for(int row = 0; row < CT_IV.DEFAULT_ROW ; row++) {
			objss[row] = lrivs.get(row).toArray();
		}
		return objss;
	}
	
	public static Object[][] convertToInv2Array(List<JB_Location_Inventory> lis) {
		Object[][] objss = new Object[lis.size()][];
		for(int row = 0; row < lis.size() ; row++) {
			objss[row] = lis.get(row).toArray();
		}
		return objss;
	}
	
	public static String generateToolTip(JB_Location_IV iv) {
		String disValue = "<table width='100%'>" +
		  	"<tr><td style='text-aglin:left;'>库存容积：" + NumberFormat.getFormat(CT_IV.DEFAULT_PERCENT_FORMAT).format(iv.getLoc_rate()) + "</td></tr>" +
		  	"<tr><td style='text-aglin:left;'>库位道：" + iv.getLoc_aisle() + "</td></tr>" +
		  	"<tr><td style='text-aglin:left;'>库位排：" + iv.getLoc_line() + "</td></tr></table>";
		return disValue;
	}
	
	public static String generateToolTip(JB_Location_RC_IV lriv) {
		String[] realValues = lriv.toTipArray();
		String disValue = "<table width='100%'>" +
		 	"<tr><td style='text-aglin:left;'>库存容积：" + NumberFormat.getFormat(CT_IV.DEFAULT_PERCENT_FORMAT).format(lriv.getRate()) + "</td></tr>" +
			"<tr><td style='text-aglin:left;'>库位编码：" + realValues[0] + "</td></tr>" +
			"<tr><td style='text-aglin:left;'>长x宽x高：" + realValues[1] + "x" + realValues[2] + "x" + realValues[3] + "</td></tr>" +
			"<tr><td style='text-aglin:left;'>件数x重量x体积x托数：" + realValues[4] + "x" + realValues[5] + "x" + realValues[6] + "x" + realValues[7] + "</td></tr>" +
			"<tr><td style='text-aglin:left;'>额定库满度：" + realValues[8]+"</td></tr></table>";
		return disValue;
	}
}
