package com.example.myapp.client.modal;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceDateTimeField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.util.SC;
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.form.DynamicForm;
import com.smartgwt.client.widgets.form.ValuesManager;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.DateTimeItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
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 PromoAddKeycodes {

	public static void setupKeycodes(ValuesManager formVM) {
		final Window winModal = new Window();  
        winModal.setWidth("80%");  
        winModal.setHeight("80%");
        winModal.setCanDragResize(true);
        winModal.setTitle("Keycode 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();
        
        String formCol1 = "50%";
		String formCol2 = "50%";
		int fldWidth = 300;
		
        final DynamicForm formKeycodeModal = new DynamicForm();
		formKeycodeModal.setID("formKeycodeModal");
		formKeycodeModal.setGroupTitle("Keycode Creation");
		formKeycodeModal.setHeight("50%");
		formKeycodeModal.setIsGroup(true);
		formKeycodeModal.setNumCols(2);
		formKeycodeModal.setColWidths(formCol1, formCol2);
		
        //Create 3 Fields on a Form: 1, TextField (Keycode), 2, DateTimeFieldItem, 3, DateTimeFieldItem
        final TextItem txtKeycodeVal = new TextItem("keycode", "Enter Keycode Value");
        //Start DateTime
        final DateTimeItem startKeycodeDate = new DateTimeItem("startdt", "Keycode Start");
		
		//End DateTime		
        final DateTimeItem endKeycodeDate = new DateTimeItem("enddt", "Keycode End");
		
        final ButtonItem biCreateKeycode = new ButtonItem("btnCreateKeycode", "Create Keycode");
		
		
		formKeycodeModal.setFields(txtKeycodeVal, startKeycodeDate, endKeycodeDate, biCreateKeycode);
		formKeycodeModal.setVisible(true);
		
		//Selections
		SectionStack secStkKeycode = new SectionStack();
		secStkKeycode.setWidth("100%");  
		secStkKeycode.setHeight("40%");  
        SectionStackSection secKeycode = new SectionStackSection("Selected");  
        secKeycode.setCanCollapse(false);  
        secKeycode.setExpanded(true);  
        
		final ListGrid keycodesLGW = new ListGrid();  
		keycodesLGW.setAlternateRecordStyles(true);
		keycodesLGW.setWidth("100%");  
		keycodesLGW.setHeight("38%");  
		keycodesLGW.setLeft(350);  
		keycodesLGW.setShowAllRecords(true);  
		keycodesLGW.setEmptyMessage("Rows Added Here");  

		DataSource ds = new DataSource();
		DataSourceTextField keyTxtFld = new DataSourceTextField("keycode", "Keycode");
		keyTxtFld.setPrimaryKey(true);
		DataSourceDateTimeField startDTFld = new DataSourceDateTimeField("startdt", "Start Date");
		DataSourceDateTimeField endDTFld = new DataSourceDateTimeField("enddt", "End Date");
		ds.setFields(keyTxtFld, startDTFld, endDTFld);
        ds.setClientOnly(true);
        
        keycodesLGW.setDataSource(ds);
        keycodesLGW.setAutoFetchData(true);

		setupRtMenu(keycodesLGW);
		
		secKeycode.setItems(keycodesLGW);  
		secStkKeycode.setSections(secKeycode);  
			
        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) {
				winModal.destroy();
			}
		});
        
        btnDiscard.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
			
			@Override
			public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
				winModal.destroy();
			}
		});
        
        biCreateKeycode.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				//Get existing records and add a new one then set it back on our table
				
				ListGridRecord tmp = new ListGridRecord();
				Object code = txtKeycodeVal.getValue();
				if(code != null) {
					tmp.setAttribute("keycode", code);
					Object start = startKeycodeDate.getValue();
					if(start != null) {
						tmp.setAttribute("startdt", start);
					}
					Object end = endKeycodeDate.getValue();
					if(end != null) {
						tmp.setAttribute("enddt", end);
					}
					keycodesLGW.addData(tmp);
				}
				else {
					SC.say("You must enter a keycode value");
				}
				
			}
		});

        vL.addMember(hL1);
        hL1.addMember(btnSave);
        hL1.addMember(btnDiscard);

        vL.addMember(spacer);
        vL.addMember(blankSpacer);
        vL.addMember(hL2);
        
        VLayout ltVL = new VLayout();
        ltVL.setWidth("30%");
        ltVL.setHeight100();
        ltVL.setStyleName("rttbls");
        ltVL.setBorder("0px solid red;");
        		
        ltVL.addMember(formKeycodeModal);
        hL2.addMember(ltVL);
        
        VLayout rtVL = new VLayout();
        rtVL.setWidth("30%");
        rtVL.setHeight100();
        rtVL.setStyleName("rttbls");
        rtVL.setBorder("0px solid red;");
        
        rtVL.addMember(secStkKeycode);
        
        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();
	}
	
	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();
	}
}
