package com.fircosoft.cdb.client.api.application.test;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.StringUtil;
import com.smartgwt.client.widgets.Img;
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.CanvasItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.layout.LayoutSpacer;

public class SearchResultsPopupItem extends DynamicForm{
	
	private static final String FIELD_TITLE = "TITLE";
	private static final String FIELD_DESCRIPTION = "DESCRIPTION";
	private static final String FIELD_LINK = "LINK";
	
	private Record record;
	private String recordSource;
	
	private StaticTextItem title = new StaticTextItem(FIELD_TITLE);
	private StaticTextItem desc = new StaticTextItem(FIELD_DESCRIPTION);
	private StaticTextItem link = new StaticTextItem(FIELD_LINK);
	private PdfIcon pdfIcon;
	private CanvasItem empty;
	
	public SearchResultsPopupItem(/*int height*/){
		setMargin(5);
		
		title.setShowTitle(false);
		title.setTextBoxStyle("sres-item-label");
		desc.setShowTitle(false);
		desc.setTextBoxStyle("sres-item-desc");
		link.setShowTitle(false);
		link.setTextBoxStyle("sres-item-src");
		
		DynamicForm infoForm = new DynamicForm();
		infoForm.setNumCols(1);
		infoForm.setWidth100();
		//infoForm.setOverflow(Overflow.HIDDEN);
		//infoForm.setHeight(height);
		CanvasItem canvasItem = new CanvasItem();
		canvasItem.setShowTitle(false);
		canvasItem.setCanvas(infoForm);
		infoForm.setItems(title,desc,link);
		infoForm.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				onCenterPanelClicked();
			}
		});
		
		pdfIcon = new PdfIcon();
		pdfIcon.setRowSpan(3);
		
		empty = new CanvasItem();
		empty.setShowTitle(false);
		empty.setCanvas(new LayoutSpacer());
		empty.setHeight(2);
		empty.setRowSpan(3);
		
		setNumCols(2);
		setItems(pdfIcon, empty, canvasItem);
		
//		title.setWrap(false);
//		title.setClipValue(true);
//		desc.setWrap(false);
//		desc.setClipValue(true);
//		link.setWrap(false);
//		link.setClipValue(true);
		
		setWidth100();
		//setOverflow(Overflow.HIDDEN);
		//setHeight(height);
	}
	
	public void initialize(Record record){
		this.record = record;
		
		title.setValue(record.getAttribute("label"));
		
		recordSource = record.getAttribute("url");
		if (recordSource == null || recordSource.isEmpty())
			recordSource = "<" + "noSource" + ">";
		link.setValue(StringUtil.asHTML(recordSource));
		
		String desc = record.getAttribute("description");
		if ((desc == null) || desc.isEmpty())
			desc = "<" + "$noDescription" + ">";
		this.desc.setValue(StringUtil.asHTML(desc));
		
		if (isPdfConvertible()) {
			pdfIcon.initialize(record);
			pdfIcon.setVisible(true);
			empty.setVisible(false);
			setColWidths("24","*");
		}else{
			pdfIcon.setVisible(false);
			empty.setVisible(true);
			setColWidths("0","*");
		}
		
	}
	
	private boolean isPdfConvertible() {
		return false;
	}
	
	private void onCenterPanelClicked() {

		String fircoSrcPref = "firco://";
		if (recordSource != null && !recordSource.isEmpty() && !recordSource.startsWith("<")) {
			if (recordSource.startsWith("http://")) {
				Window.open(recordSource, "_blank", null);
			} else if (recordSource.startsWith(fircoSrcPref)) {
				String source = recordSource.substring(recordSource.indexOf("://") + 3, recordSource.length());
				String sourceType = source.substring(0, source.indexOf("/"));
				String sourceVal = source.substring(source.indexOf("/") + 1, source.length());
				String sourceName = sourceVal.split("/")[0];
				String sourceId = sourceVal.split("/")[1];

				if (sourceType.equals("ds")) {
					openRecord(sourceName, sourceId);
				}
			}
		}
	}
	
	private void openRecord(String sourceName, String sourceId) {
		System.out.println("Open record");
	}
	
	public class PdfIcon extends CanvasItem{
		
		private Record connectorRecord;
		private Img icon;
		
		public PdfIcon(){
			icon = new Img("pdf.png", 24, 24);
			icon.setStyleName("sres-item-pdf");
			setVAlign(VerticalAlignment.TOP);
			setShowTitle(false);
			
			icon.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					downloadPdf();
				}
			});
			
			this.setCanvas(icon);
		}
		
		public void initialize(Record record){
			this.connectorRecord = record;
		}
		
		private void downloadPdf() {
			String url = GWT.getModuleBaseURL();
	       	Window.open(url + "report?t=" + connectorRecord.getAttribute("id"), "_blank", null);
		}
	}
}
