package com.pci.transactions.client;

import java.util.Date;

import com.pci.portal.client.Buttons;
import com.pci.portal.client.ExtendedListGrid;
import com.pci.transactions.client.DetailTab.Formatter;
import com.smartgwt.client.core.Function;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RelativeDate;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.LayoutResizeBarPolicy;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.TimeUnit;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.RelativeDateItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
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.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class ExternalTransactions extends HLayout
{
	private String isoName;

	private ToolStripButton btnSave;
	private ToolStripButton btnRefresh;

	private SelectItem asset;
	private DynamicForm assetForm;

	private DataSource dsData;
	private ExtendedListGrid dataGrid;

	private RelativeDateItem scheduleDate;

	private SectionStackSection transData;
	private ToolStrip tools;

	public ExternalTransactions(String iso, String configISOName)
	{
		this.isoName = iso;

		VLayout holder = new VLayout();
		holder.setSize("100%", "100%");

		holder.addMember(toolBar());

		tools = new ToolStrip();
		tools.setAlign(Alignment.LEFT);
		tools.setWidth("100%");
		tools.addSpacer(10);

		assetForm = new DynamicForm();
		assetForm.setNumCols(3);

		String[] assetOwnerList = new String[] { "AO1", "AO2" };
		asset = new SelectItem("name", "Asset Owner");
		asset.setValueMap(assetOwnerList);
		asset.setDefaultToFirstOption(true);
		asset.setWrapTitle(false);
		assetForm.setFields(new FormItem[] { asset });

		tools.addMember(assetForm);
		tools.addSpacer(10);

		btnSave = Buttons.makeToolButton("Save", Buttons.ButtonType.Save, new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent event)
			{
				dataGrid.saveAllEdits(new Function()
				{
					@Override
					public void execute()
					{
						refreshGrid(false);
					}
				});
			}
		});

		tools.addMember(btnSave);

		btnRefresh = Buttons.makeToolButton("Refresh", Buttons.ButtonType.Refresh, new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent event)
			{
				refreshGrid(false);
			}
		});

		tools.addMember(btnRefresh);

		holder.addMember(tools);

		dsData = DataSource.get("ext_transactions_SPPIM");

		dataGrid = new ExtendedListGrid();
		dataGrid.setCanSelectCells(true);
		dataGrid.setEditByCell(true);
		dataGrid.setGenerateClickOnSpace(true);
		dataGrid.setCanDragSelect(true);
		dataGrid.setCanSelectAll(true);
		dataGrid.setUseCopyPasteShortcuts(true);
		dataGrid.setSize("100%", "20%");
		dataGrid.setShowFilterEditor(true);
		dataGrid.setCanEdit(true);
		dataGrid.setShowAllColumns(true);
		dataGrid.setAutoFetchData(false);
		dataGrid.setAutoSaveEdits(false);
		dataGrid.setDataSource(dsData);
		dataGrid.setUseAllDataSourceFields(true);
		dataGrid.setupCombos();

		SectionStack stack = new SectionStack();
		stack.setSize("100%", "100%");
		stack.setCanTabToHeaders(true);
		stack.setAnimateMembers(true);
		stack.setVisibilityMode(VisibilityMode.MULTIPLE);
		stack.setDefaultResizeBars(LayoutResizeBarPolicy.MIDDLE);
		stack.setOverflow(Overflow.VISIBLE);
		stack.setHeaderHeight(20);

		transData = new SectionStackSection("External Bilaterals");
		transData.setExpanded(true);
		transData.addItem(dataGrid);
		stack.addSection(transData);

		holder.addMember(stack);

		addMember(holder);
	}

	public void refreshGrid(boolean exportData)
	{
		Criteria criteria = new Criteria();
		criteria.addCriteria("operationDate", getSelectedDate());
		criteria.addCriteria("isoName", isoName);
		criteria.addCriteria("assetOwnerName", asset.getValueAsString());

		dataGrid.setCriteria(criteria);
		dataGrid.invalidateCache();
		dataGrid.fetchData(criteria, new DSCallback()
		{
			@Override
			public void execute(DSResponse response, Object rawData, DSRequest request)
			{
				if (response.getStatus() == DSResponse.STATUS_SUCCESS && response.getData() != null & response.getData().length > 0)
				{
					Record[] records = response.getData();
					if (records.length > 0)
					{
						boolean shortDay = true;
						boolean longDay = false;

						if (shortDay)
						{
							dataGrid.hideField("hour2");
						}
						if (longDay)
						{
							dataGrid.showField("hour2s");
						}
						else
						{
							dataGrid.hideField("hour2s");
						}
					}
				}
			}
		});
	}

	private ToolStrip toolBar()
	{
		ToolStrip toolbar = new ToolStrip();
		toolbar.setWidth("100%");

		DynamicForm selectDate = new DynamicForm();
		selectDate.setWrapItemTitles(false);
		selectDate.setWidth(50);
		toolbar.addMember(selectDate);
		scheduleDate = new RelativeDateItem("operationDate", "Operation Date");
		scheduleDate.setDateFormatter(new Formatter());
		scheduleDate.setTimeUnitOptions(TimeUnit.DAY);

		scheduleDate.addChangedHandler(new ChangedHandler()
		{
			@Override
			public void onChanged(ChangedEvent event)
			{
				refreshGrid(false);
			}
		});

		selectDate.setFields(scheduleDate);
		toolbar.addFill();

		return toolbar;
	}

	public Date getSelectedDate()
	{
		Date opDate = null;
		RelativeDate check = scheduleDate.getRelativeDate();
		if (check == null && scheduleDate.getValue() instanceof Date)
		{
			opDate = (Date) scheduleDate.getValue();
		}
		else if (scheduleDate.getRelativeDate() != null)
		{
			opDate = RelativeDateItem.getAbsoluteDate(scheduleDate.getRelativeDate());
		}

		return opDate;
	}
}
