package com.eonegroup.dashboard.client.composites.views.promo.modal;

import java.util.ArrayList;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;

public class TestPromoSetToCatalog {

	public static void selectCatalogs() {
		
		final PromotionStore ps = new PromotionStore();
		
		final Window winModal = new Window();  
        winModal.setWidth("80%");  
        winModal.setHeight("80%");
        winModal.setCanDragResize(true);
        winModal.setTitle("Catalog Promotion Setup...");  
        winModal.setShowMinimizeButton(false);  
        winModal.setIsModal(true);  
        winModal.setShowModalMask(true);  
        winModal.centerInPage();
        
        VLayout vL = new VLayout();
        vL.setBorder("0px solid red");
        vL.setWidth100();
        vL.setHeight100();
        
        HLayout hL1 = new HLayout();
        hL1.setBorder("0px solid green");
        hL1.setPadding(2);
        hL1.setWidth100();
        hL1.setHeight("1%");
        
        HLayout spacer = new HLayout();
        spacer.setBackgroundColor("#0000FF");
        spacer.setWidth100();
        spacer.setHeight(2);
        
        HLayout blankSpacer = new HLayout();
        blankSpacer.setBackgroundColor("#FFFFFF");
        blankSpacer.setWidth100();
        blankSpacer.setHeight(10);
        
        //Add Save Button
        IButton btnSave = new IButton("Save");
        btnSave.setAccessKey("s");
        btnSave.setExtraSpace(10);
        IButton btnDiscard = new IButton("Discard");
        
        HLayout hL2 = new HLayout();
        hL2.setBorder("0px solid gray");
        hL2.setWidth100();
        hL2.setHeight100();
                
        //Create our Fields HERE!!!
        ListGrid catLGW = new ListGrid();
        catLGW.setWidth("35%");  
        catLGW.setHeight("98%");  
		catLGW.setCanEdit(false);
		catLGW.setAutoSaveEdits(false);
		catLGW.setShowFilterEditor(true);
		catLGW.setCanDragRecordsOut(true);
		catLGW.setCanAcceptDroppedRecords(false);
		catLGW.setDragDataAction(DragDataAction.COPY);
		
		DataSource dsMain = new DataSource();
		DataSourceIntegerField catIdMnFld = new DataSourceIntegerField("catid", "Id");
		catIdMnFld.setPrimaryKey(true);
		DataSourceTextField startDTMnFld = new DataSourceTextField("dsc", "Catalog");
		dsMain.setFields(catIdMnFld, startDTMnFld);
		dsMain.setClientOnly(true);
        
		
		ListGridRecord tmp1 = new ListGridRecord();
		tmp1.setAttribute("catid", 1);
		tmp1.setAttribute("dsc", "Catalog1");
		dsMain.addData(tmp1);
		ListGridRecord tmp2 = new ListGridRecord();
		tmp2.setAttribute("catid", 2);
		tmp2.setAttribute("dsc", "Catalog2");
		dsMain.addData(tmp2);
        
		DataSourceField[] flds = dsMain.getFields();
		for (int i = 0; i < flds.length; i++) {
			if(!flds[i].getName().equalsIgnoreCase("catid") && !flds[i].getName().equalsIgnoreCase("dsc")) {
				flds[i].setHidden(true);
			}
			else if(flds[i].getName().equalsIgnoreCase("catid")) {
				flds[i].setTitle("Id");
			}
			else if(flds[i].getName().equalsIgnoreCase("dsc")) {
				flds[i].setTitle("Catalog");
			}
		}
		catLGW.setDataSource(dsMain);
		catLGW.setAutoFetchData(true);
		catLGW.setAlternateRecordStyles(true);
		catLGW.setShowAllRecords(false);
		catLGW.setDataPageSize(10);
		catLGW.setWrapCells(false);
		
		//Qualifier
		SectionStack secStkQual = new SectionStack();  
		secStkQual.setWidth("100%");  
		secStkQual.setHeight("98%");  
        SectionStackSection secQual = new SectionStackSection("Selected");  
        secQual.setCanCollapse(false);  
        secQual.setExpanded(true);
        
		final ListGrid selLGW = new ListGrid();  
		selLGW.setWidth("100%");  
		selLGW.setHeight("95%");  
		selLGW.setLeft(350);  
		selLGW.setShowAllRecords(true);  
		selLGW.setEmptyMessage("Drop Rows Here");  
		selLGW.setCanAcceptDroppedRecords(true);
		selLGW.setPreventDuplicates(true);
		
		DataSource ds = new DataSource();
		DataSourceIntegerField catIdFld = new DataSourceIntegerField("catid", "Id");
		catIdFld.setPrimaryKey(true);
		DataSourceTextField startDTFld = new DataSourceTextField("dsc", "Catalog");
		ds.setFields(catIdFld, startDTFld);
        ds.setClientOnly(true);
        
        selLGW.setDataSource(ds);
        selLGW.setAutoFetchData(true);
		
		setupRtMenu(selLGW);
		
		secQual.setItems(selLGW);  
		secStkQual.setSections(secQual);
		
        winModal.addCloseClickHandler(new CloseClickHandler() {  
            public void onCloseClick(CloseClientEvent event) {  
            	winModal.destroy();
            }  
        });
        
        btnSave.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
			
			@Override
			public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
				
				ArrayList<CatalogPromoState> cats = ps.getCatalogs();
				ListGridRecord[] recs = selLGW.getRecords();
				
				for (ListGridRecord rec : recs) {
					CatalogPromoState tmp = new CatalogPromoState();
					tmp.setCatid(rec.getAttributeAsInt("catid"));
					tmp.setDsc(rec.getAttributeAsString("dsc"));
					
					cats.add(tmp);
				}
				
				winModal.destroy();
			}
		});
        
        btnDiscard.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
			
			@Override
			public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
				winModal.destroy();
			}
		});
        
        vL.addMember(hL1);
        hL1.addMember(btnSave);
        hL1.addMember(btnDiscard);

        vL.addMember(spacer);
        vL.addMember(blankSpacer);
        vL.addMember(hL2);
        
        hL2.addMember(catLGW);
        
        VLayout rtVL = new VLayout();
        rtVL.setWidth("30%");
        rtVL.setHeight100();
        rtVL.setStyleName("rttbls");
        rtVL.setBorder("0px solid red;");
       
        rtVL.addMember(secStkQual);
        
        VLayout notesVL = new VLayout();
        notesVL.setWidth("25%");
        notesVL.setStyleName("helptxt");
        notesVL.setBorder("0px solid green;");
        
        HTMLFlow htmlFlow = new HTMLFlow();
		htmlFlow.setWidth("95%");
		htmlFlow.setHeight100();
		htmlFlow.setStyleName("help");
		String contents = 
					"<b>Notes:</b>";
					
		htmlFlow.setContents(contents);  
		htmlFlow.draw(); 
		notesVL.addMember(htmlFlow);
		
        hL2.addMember(rtVL);
        hL2.addMember(notesVL);
        
        winModal.addItem(vL);  
        winModal.show();
        
        loadGrid(selLGW, ps);
	}
	
	private static void loadGrid(ListGrid lg, PromotionStore ps) {
		ArrayList<CatalogPromoState> cats = ps.getCatalogs();
		if(cats.size() > 0) {
			for (CatalogPromoState cat : cats) {
				ListGridRecord tmp = new ListGridRecord();
				tmp.setAttribute("catid", cat.getCatid());
				tmp.setAttribute("dsc", cat.getDsc());
				lg.getDataSource().addData(tmp);
			}
			
		}
	}
	
	private static void setupRtMenu(final ListGrid lg) {
		Menu rtMenu = new Menu();
		rtMenu.setShowShadow(true);
		rtMenu.setShadowDepth(10);
		
		MenuItem miDelete = new MenuItem("Remove");
		miDelete.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {

			public void onClick(MenuItemClickEvent event) {
				deleteRecord(lg);
			}
		});
		
		rtMenu.addItem(miDelete);
		lg.setContextMenu(rtMenu);
	}
	
	private static void deleteRecord(final ListGrid lg) {
		lg.removeSelectedData();
	}
	
	private static class CatalogPromoState {
		private int catid = -1;
		private String dsc = null;
		public int getCatid() {
			return catid;
		}
		public void setCatid(int catid) {
			this.catid = catid;
		}
		public String getDsc() {
			return dsc;
		}
		public void setDsc(String dsc) {
			this.dsc = dsc;
		}
	}
	private static class PromotionStore {
		ArrayList<CatalogPromoState> cats = null;
		
		public PromotionStore() {
			cats = new ArrayList<CatalogPromoState>();
			CatalogPromoState cps1 = new CatalogPromoState();
			cps1.setCatid(1);
			cps1.setDsc("Catalog1");
			cats.add(cps1);
			CatalogPromoState cps2 = new CatalogPromoState();
			cps2.setCatid(2);
			cps2.setDsc("Catalog2");
			cats.add(cps2);
		}

		public ArrayList<CatalogPromoState> getCatalogs() {
			return cats;
		}
		
	}
}
