package org.gcube.portlets.user.workspace.client.view.panels;

import java.util.List;

import org.gcube.portlets.user.workspace.client.AppController;
import org.gcube.portlets.user.workspace.client.ConstantsExplorer;
import org.gcube.portlets.user.workspace.client.ConstantsPortlet;
import org.gcube.portlets.user.workspace.client.event.SaveSmartFolderEvent;
import org.gcube.portlets.user.workspace.client.event.ScopeChangeEvent;
import org.gcube.portlets.user.workspace.client.event.SearchTextEvent;
import org.gcube.portlets.user.workspace.client.model.ScopeModel;
import org.gcube.portlets.user.workspace.client.view.GxtComboBox;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
 * @author Massimiliano Assante ISTI-CNR
 * 
 * @version 1.0 May 14th 2012
 */
public class GxtSeachAndFilterPanel extends LayoutContainer {

	private GxtComboBox comboBoxGxt = null;
	private ComboBox<ScopeModel> cbViewScope = null;
	private HTML txtViewScope = new HTML("<nobr>"+ConstantsPortlet.VIEWSPACE+":</nobr>");
	private final TextField<String> textSearch = new TextField<String>();
	private Button bSearch = new Button(ConstantsPortlet.SEARCH);
	private Button bCancel = new Button(ConstantsPortlet.CANCEL);
	private Button bSave = new Button(ConstantsPortlet.SAVE);
	private TextField<String> textFull = new TextField<String>();
	private DateField fromDate = new DateField();
	private DateField toDate = new DateField();
	private SimpleComboBox<String> cbNameFilter = null;
	private HorizontalPanel toolbarPathPanel = null;
	private boolean isSearchActive = false;
	private VerticalPanel cp = new VerticalPanel();

	HorizontalPanel hp = new HorizontalPanel();
	HorizontalPanel hp2 = new HorizontalPanel();
	
	HorizontalPanel hpMain = new HorizontalPanel();

	public GxtSeachAndFilterPanel(HorizontalPanel toolbarContainer) {

		txtViewScope.getElement().getStyle().setColor("#15428B");
		txtViewScope.getElement().getStyle().setFontSize(12, Unit.PX);

		setLayout(new FitLayout());
		setBorders(true);
		setId("SearchAndFilter");

		this.comboBoxGxt = new GxtComboBox();
		this.cbViewScope = this.comboBoxGxt.getComboViewScope();
		this.cbNameFilter = this.comboBoxGxt.getComboStringFilter();

		this.cbViewScope.setWidth(300);
		this.cbViewScope.setAutoWidth(true);

		this.toolbarPathPanel = toolbarContainer;
		cp.add(this.toolbarPathPanel);



		hp.setStyleAttribute("padding", "2px");

		this.cbViewScope.setStyleAttribute("margin-left", "5px");
		this.cbViewScope.setStyleAttribute("margin-right", "100px");

		seVisibleButtonsCancelSave(false);

		textSearch.setAllowBlank(true);
		textSearch.setEmptyText(ConstantsPortlet.SEARCHINWORSPACE);
		textSearch.setWidth(325);

		textSearch.setRegex("^[a-zA-Z0-9]+[ a-zA-Z0-9_().-]*"); //alphanumeric
		textSearch.getMessages().setRegexText(ConstantsExplorer.MESSAGE_SEARCH_FORCE_APHANUMERIC);
		textSearch.setAutoValidate(true);
		hp.add(textSearch);
		
		bSearch.setStyleName("wizardButton");
		bSearch.getElement().getStyle().setMarginLeft(3, Unit.PX);
		bCancel.setStyleName("wizardButton");
		bCancel.getElement().getStyle().setMarginLeft(3, Unit.PX);
		bSave.setStyleName("wizardButton");
		bSave.getElement().getStyle().setMarginLeft(3, Unit.PX);
		
		hp.add(bSearch);
	//	hp.add(bCancel);
		hp.add(bSave);
		
		bSearch.setWidth("70px");
		bSave.setWidth("70px");

		hp2.setHorizontalAlign(HorizontalAlignment.RIGHT);
		hp2.setVerticalAlign(VerticalAlignment.MIDDLE);
		hp2.add(txtViewScope);
		hp2.add(cbViewScope);
		
		cbViewScope.setStyleAttribute("margin-top", "2px");

	
		hpMain.add(hp);
		hpMain.add(hp2);
		cp.add(hpMain);
		
		this.addListeners();

		add(cp);
			
		updateSize();
		Window.addResizeHandler(new ResizeHandler() {
			@Override
			public void onResize(ResizeEvent event) {
				updateSize();
			}
		});		 
		
		layout();
	}

	/**
	 * Update window size
	 */
	public void updateSize() {
		RootPanel workspace = RootPanel.get(ConstantsPortlet.WORKSPACEDIV);
		int leftBorder = workspace.getAbsoluteLeft();
		int rootWidth = Window.getClientWidth() - 2* leftBorder; //- rightScrollBar;
		hp.setWidth(rootWidth - 400);
	}
	
	public void searchText(String value){
		textSearch.setValue(value);
		seVisibleButtonsCancelSave(true);
		AppController.getEventBus().fireEvent(new SearchTextEvent(value));
	}

	private void addListeners(){

		bSearch.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				if(textSearch.getValue()!=null && !textSearch.getValue().isEmpty() && textSearch.isValid()){
//					seVisibleButtonsCancelSave(true);
//					AppController.getEventBus().fireEvent(new SearchTextEvent(textSearch.getValue()));			
					searchText(textSearch.getValue());
				}
			}
		});

		KeyListener keyListener = new KeyListener() {
			public void componentKeyUp(ComponentEvent event) {
				if (event.getKeyCode() == 13) { // KEY_ENTER
//					seVisibleButtonsCancelSave(true);
//					AppController.getEventBus().fireEvent(new SearchTextEvent(textSearch.getValue()));
					searchText(textSearch.getValue());
				}
			}
		};

		textSearch.addKeyListener(keyListener);
//
//		bCancel.addClickHandler(new ClickHandler() {			
//			@Override
//			public void onClick(ClickEvent event) {
//				if(textSearch.getValue()!=null && !textSearch.getValue().isEmpty() && textSearch.isValid()){
//					searchCancel();
//					AppController.getEventBus().fireEvent(new SearchTextEvent(null));
//				}
//			}
//		});
		
		bSave.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				if(textSearch.getValue()!=null && !textSearch.getValue().isEmpty() && textSearch.isValid()){
					if (textSearch.getValue() != null && textSearch.getValue().length()>0) {
						AppController.getEventBus().fireEvent(new SaveSmartFolderEvent(null, textSearch.getValue(), null));
					}
				}
			}
		});
	
		this.cbViewScope.addSelectionChangedListener(new SelectionChangedListener<ScopeModel>() {

			@Override
			public void selectionChanged(SelectionChangedEvent<ScopeModel> se) {

				ScopeModel scope = se.getSelectedItem();

				AppController.getEventBus().fireEvent(new ScopeChangeEvent(scope.getId()));

			}
		});

	}

	public void resetFields(){
		this.textSearch.reset();
		this.textFull.reset();
		this.fromDate.reset();
		this.toDate.reset();
		this.cbNameFilter.reset();

	}

	public void seVisibleButtonsCancelSave(boolean flag){

		this.bCancel.setVisible(flag);
		this.bSave.setVisible(flag);
	}

	public void searchCancel(){
		resetFields();
		seVisibleButtonsCancelSave(false);
		//		AppController.getEventBus().fireEvent(new SearchTextEvent(null));
	}

	public boolean isSearchActive(){
		return this.isSearchActive;
	}

	public void setListScope(List<ScopeModel> listScope){

		this.comboBoxGxt.setListScope(listScope);
	}

	public void selectScopeByIndex(int index){

		this.cbViewScope.setValue(cbViewScope.getStore().getAt(index)); 
	}

	public void setSearchActive(boolean isSearchActive) {
		System.out.println("#################SET SEARCH ACTIVE in GRID " + isSearchActive);
		this.isSearchActive = isSearchActive;
	}

	public void setVisibleButtonSave(boolean bool){
		this.bSave.setVisible(bool);
	}

	public void setEmptyText(String emptyText){
		textSearch.setEmptyText(emptyText);
	}

}