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

import org.gcube.portlets.user.workspace.client.AppController;
import org.gcube.portlets.user.workspace.client.ConstantsPortlet;
import org.gcube.portlets.user.workspace.client.ConstantsPortlet.ViewSwitchTypeInResult;
import org.gcube.portlets.user.workspace.client.event.ActiveGroupingView;
import org.gcube.portlets.user.workspace.client.event.AddFolderEvent;
import org.gcube.portlets.user.workspace.client.event.DeleteItemEvent;
import org.gcube.portlets.user.workspace.client.event.DoubleClickElementSelectedEvent;
import org.gcube.portlets.user.workspace.client.event.FileDownloadEvent;
import org.gcube.portlets.user.workspace.client.event.FileDownloadEvent.DownloadType;
import org.gcube.portlets.user.workspace.client.event.FileUploadEvent;
import org.gcube.portlets.user.workspace.client.event.FileUploadEvent.UploadType;
import org.gcube.portlets.user.workspace.client.event.ImagePreviewEvent;
import org.gcube.portlets.user.workspace.client.event.OpenReportsEvent;
import org.gcube.portlets.user.workspace.client.event.RenameItemEvent;
import org.gcube.portlets.user.workspace.client.event.WebDavUrlEvent;
import org.gcube.portlets.user.workspace.client.interfaces.GXTFolderItemTypeEnum;
import org.gcube.portlets.user.workspace.client.model.FileGridModel;
import org.gcube.portlets.user.workspace.client.model.FileModel;
import org.gcube.portlets.user.workspace.client.resources.Resources;
import org.gcube.portlets.user.workspace.client.view.grids.GxtGridFilterGroupPanel;

import com.extjs.gxt.ui.client.Style.ButtonArrowAlign;
import com.extjs.gxt.ui.client.Style.ButtonScale;
import com.extjs.gxt.ui.client.Style.IconAlign;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToggleButton;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;

/**
 * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
 *
 */
public class GxtToolBarItem {

	private Button btnAddFolder = null;
	private Button btnRemoveItem = null;
	private Button btnRenameItem = null;
	private Button btnUploadFile = null;
	private Button btnUploadArchive = null;
	private Button btnDownloadFile = null;
	private Button btnOpen = null;
	private Button btnAccessFromDesktop = null;
	private ToggleButton toggleGroup = new ToggleButton();
	private ToggleButton toggleList = new ToggleButton();
	private ToggleButton toggleIcon = new ToggleButton();
	private GxtGridFilterGroupPanel gridGroupViewContainer;

	private ToolBar toolBar = new ToolBar();
	private Button btnPreview;
	private ToggleButton btnGridView;

	public GxtToolBarItem() {
		initToolBar();
		addSelectionListenersOnToolBar();
	}

	public GxtToolBarItem(GxtGridFilterGroupPanel gridFilterGroupContainer) {
		this();
		this.gridGroupViewContainer = gridFilterGroupContainer;
	}

	private void initToolBar() {

	    btnGridView = new ToggleButton("Category View", Resources.getIconGridView());
	    btnGridView.setScale(ButtonScale.SMALL);  
	    btnGridView.setIconAlign(IconAlign.TOP);  
	    btnGridView.toggle(true);
	    btnGridView.setArrowAlign(ButtonArrowAlign.BOTTOM); 
	    toolBar.add(btnGridView);
		
		toolBar.add(new SeparatorToolItem());
		
		btnAddFolder = new Button(ConstantsPortlet.ADDFOLDER,Resources.getIconAddFolder());
		btnAddFolder.setScale(ButtonScale.SMALL);
		btnAddFolder.setIconAlign(IconAlign.TOP);
		btnAddFolder.setArrowAlign(ButtonArrowAlign.BOTTOM);

		toolBar.add(btnAddFolder);
//		toolBar.add(new SeparatorToolItem());
//		toolBar.add(new SeparatorToolItem());

		btnRemoveItem = new Button(ConstantsPortlet.REMOVEITEM,
				Resources.getIconDeleteItem());
		btnRemoveItem.setScale(ButtonScale.SMALL);
		btnRemoveItem.setIconAlign(IconAlign.TOP);
		btnRemoveItem.setArrowAlign(ButtonArrowAlign.BOTTOM);

		toolBar.add(btnRemoveItem);
//		toolBar.add(new SeparatorToolItem());
//		toolBar.add(new SeparatorToolItem());

		btnRenameItem = new Button(ConstantsPortlet.RENAMEITEM,
				Resources.getIconRenameItem());
		btnRenameItem.setScale(ButtonScale.SMALL);
		btnRenameItem.setIconAlign(IconAlign.TOP);
		btnRenameItem.setArrowAlign(ButtonArrowAlign.BOTTOM);
		toolBar.add(btnRenameItem);

//		toolBar.add(new SeparatorToolItem());
//		toolBar.add(new SeparatorToolItem());

		btnDownloadFile = new Button(ConstantsPortlet.DOWNLOADITEM,Resources.getIconDownload());
		btnDownloadFile.setScale(ButtonScale.SMALL);
		btnDownloadFile.setIconAlign(IconAlign.TOP);
		btnDownloadFile.setArrowAlign(ButtonArrowAlign.BOTTOM);

		toolBar.add(btnDownloadFile);
		
//		toolBar.add(new SeparatorToolItem());
//		toolBar.add(new SeparatorToolItem());

		btnPreview = new Button(ConstantsPortlet.PREVIEW ,Resources.getIconPreview());
		btnPreview.setScale(ButtonScale.SMALL);
		btnPreview.setIconAlign(IconAlign.TOP);
		btnPreview.setArrowAlign(ButtonArrowAlign.BOTTOM);
		toolBar.add(btnPreview);
		
//		toolBar.add(new SeparatorToolItem());
//		toolBar.add(new SeparatorToolItem());
		
		btnOpen = new Button(ConstantsPortlet.OPEN ,Resources.getIconShow());
		btnOpen.setScale(ButtonScale.SMALL);
		btnOpen.setIconAlign(IconAlign.TOP);
		btnOpen.setArrowAlign(ButtonArrowAlign.BOTTOM);
		toolBar.add(btnOpen);
		

//		toolBar.add(new SeparatorToolItem());
		toolBar.add(new SeparatorToolItem());
		
	    btnAccessFromDesktop = new Button(ConstantsPortlet.TITLEACCESSWEBDAV, Resources.getIconWebDav());
	    btnAccessFromDesktop.setScale(ButtonScale.SMALL);  
	    btnAccessFromDesktop.setIconAlign(IconAlign.TOP);  
	    btnAccessFromDesktop.setArrowAlign(ButtonArrowAlign.BOTTOM); 
	    
//	    btnAccessFromDesktop = new Button(ConstantsPortlet.TITLEACCESSWEBDAV);
//	    btnAccessFromDesktop.setScale(ButtonScale.SMALL);  
//	    btnAccessFromDesktop.addStyleName("button-hyperlink");
	    
	    toolBar.add(btnAccessFromDesktop);  

		toolBar.add(new FillToolItem());  

		btnUploadFile = new Button(ConstantsPortlet.UPLOADFILE,
				Resources.getIconFileUpload());
		btnUploadFile.setScale(ButtonScale.SMALL);
		btnUploadFile.setIconAlign(IconAlign.TOP);
		btnUploadFile.setArrowAlign(ButtonArrowAlign.BOTTOM);

		toolBar.add(btnUploadFile);
//		toolBar.add(new SeparatorToolItem());
//		toolBar.add(new SeparatorToolItem());

		btnUploadArchive = new Button(ConstantsPortlet.UPLOADARCHIVE,
				Resources.getIconArchiveUpload());
		btnUploadArchive.setScale(ButtonScale.SMALL);
		btnUploadArchive.setIconAlign(IconAlign.TOP);
		btnUploadArchive.setArrowAlign(ButtonArrowAlign.BOTTOM);

		toolBar.add(btnUploadArchive);
//		toolBar.add(new SeparatorToolItem());
//		toolBar.add(new SeparatorToolItem());
		

		
	    
	    //TODO OLD CODE
		toggleGroup = new ToggleButton();
		toggleGroup.setIcon(Resources.getIconToggleGroup());
		toggleGroup.setToggleGroup("viewgrid");
		toggleGroup.toggle(true);

		toggleIcon = new ToggleButton();
		toggleIcon.setIcon(Resources.getIconToggleIcon());
		toggleIcon.setToggleGroup("viewgrid");

		toggleList = new ToggleButton();
		toggleList.setIcon(Resources.getIconToggleList());
		toggleList.setToggleGroup("viewgrid");

		switchView(ViewSwitchTypeInResult.Group);

//		toolBar.add(new FillToolItem());

		this.activeButtonsOnSelect(null, false);
	}
	
	public void toggleGridViewButton(boolean toogle){
		btnGridView.toggle(toogle);
	}

	private void addSelectionListenersOnToolBar() {

		btnAddFolder.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				
				AppController.getEventBus().fireEvent(
						new AddFolderEvent(gridGroupViewContainer
								.getCurrentFolderView(), null));
			}
		});
		
		btnGridView.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {

				AppController.getEventBus().fireEvent(new ActiveGroupingView(btnGridView.isPressed()));
	
			}
		});

		btnRemoveItem.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {

				final FileGridModel fileGridModel = gridGroupViewContainer.getSelectedItem();

				AppController.getEventBus().fireEvent(
						new DeleteItemEvent(fileGridModel));
			}
		});

		btnRenameItem.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {

				final FileGridModel fileGridModel = gridGroupViewContainer.getSelectedItem();

				AppController.getEventBus().fireEvent(
						new RenameItemEvent(fileGridModel));

			}
		});

		btnUploadFile.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				AppController.getEventBus().fireEvent(
						new FileUploadEvent(null, UploadType.FILE));
			}
		});

		btnUploadArchive.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				AppController.getEventBus().fireEvent(
						new FileUploadEvent(null, UploadType.ARCHIVE));
			}
		});

		btnDownloadFile.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {

				final FileGridModel fileGridModel = gridGroupViewContainer.getSelectedItem();

				AppController.getEventBus().fireEvent(
						new FileDownloadEvent(fileGridModel
								.getIdentifier(), fileGridModel
								.getName(), DownloadType.DOWNLOAD));
			}
		});
		
		btnPreview.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {

				final FileGridModel fileGridModel = gridGroupViewContainer.getSelectedItem();

				AppController.getEventBus().fireEvent(new ImagePreviewEvent(fileGridModel, ce.getClientX(), ce.getClientY()+100));

			}
		});
		
		btnOpen.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {

				final FileGridModel fileGridModel = gridGroupViewContainer.getSelectedItem();
				
				if(fileGridModel!=null){
					if(fileGridModel.isDirectory())
						AppController.getEventBus().fireEvent(new DoubleClickElementSelectedEvent(fileGridModel));
					else if(fileGridModel.getGXTFolderItemType().equals(GXTFolderItemTypeEnum.REPORT) || fileGridModel.getGXTFolderItemType().equals(GXTFolderItemTypeEnum.REPORT_TEMPLATE))
							AppController.getEventBus().fireEvent(new OpenReportsEvent(fileGridModel));
						else
							AppController.getEventBus().fireEvent(new FileDownloadEvent(fileGridModel.getIdentifier(), fileGridModel.getName(), DownloadType.SHOW));
	
					}
			}
		});
		
	    btnAccessFromDesktop.addSelectionListener(new SelectionListener<ButtonEvent>() {
		
			@Override
			public void componentSelected(ButtonEvent ce) {
				
				AppController.getEventBus().fireEvent(new WebDavUrlEvent(null));
			}
		});
		

		toggleGroup.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (toggleGroup.isPressed()) {
					switchView(ViewSwitchTypeInResult.Group);

				}
				// Window.alert("ViewSwitchTypeInResult.Group");
			}
		});

		toggleList.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (toggleList.isPressed()) {
					switchView(ViewSwitchTypeInResult.List);

				}
				// Window.alert("ViewSwitchTypeInResult.List");
			}
		});

		toggleIcon.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (toggleIcon.isPressed()) {
					switchView(ViewSwitchTypeInResult.Icons);
				}
			}
		});
	}

	private void switchView(ViewSwitchTypeInResult type) {

		// if (type.compareTo(ViewSwitchTypeInResult.Icons) == 0) {
		// this.gridFilterViewContainer.setVisible(false);
		// this.gridGroupViewContainer.setVisible(false);
		// this.iconsViewContainer.setVisible(true);
		//
		// } else{
		// if (type.compareTo(ViewSwitchTypeInResult.List) == 0) {
		//
		// this.iconsViewContainer.setVisible(false);
		// this.gridGroupViewContainer.setVisible(false);
		// this.gridFilterViewContainer.setVisible(true);
		// }
		// else{
		// this.iconsViewContainer.setVisible(false);
		// this.gridFilterViewContainer.setVisible(false);
		// this.gridGroupViewContainer.setVisible(true);
		// }
		// }

	}

	public void activeButtonsOnSelect(FileModel target, boolean active) {

		this.btnPreview.disable();
		this.btnOpen.enable();
		
		if(target!=null){
	
			if(active & target.getGXTFolderItemType()!=null){
				
				switch(target.getGXTFolderItemType()){
			
				case EXTERNAL_IMAGE: 
	//				contextMenu.getItemByItemId(ConstantsExplorer.PRW).setVisible(true);
					this.btnPreview.enable();
					break;
				case EXTERNAL_FILE: 
					break;
				case EXTERNAL_PDF_FILE: 
					break;
				case EXTERNAL_URL: 
	//				contextMenu.getItemByItemId(ConstantsExplorer.LNK).setVisible(true);
					break;
				case REPORT_TEMPLATE: 
	//				contextMenu.getItemByItemId(ConstantsExplorer.ORT).setVisible(true); 
					break;
				case REPORT: 
					break;
				case QUERY: 
					break;
				case TIME_SERIES: 
					break;
				case PDF_DOCUMENT: 
					break;
				case IMAGE_DOCUMENT: 
					this.btnPreview.enable();
					break;
				case DOCUMENT: 
					break;
				case URL_DOCUMENT: 
	//				contextMenu.getItemByItemId(ConstantsExplorer.LNK).setVisible(true);
					break;
				case METADATA: 
					break;
				case WORKFLOW_REPORT: 
					break;
				case WORKFLOW_TEMPLATE: 
					break;
				default:
				
				}
			}
		}
		
		activeButtons(active);

	}
	
	
	private void activeButtons(boolean active){
		
		if (!active) {
			this.btnRemoveItem.disable();
			this.btnRenameItem.disable();
			this.btnDownloadFile.disable();
			this.btnOpen.disable();
		} else {
			this.btnRemoveItem.enable();
			this.btnRenameItem.enable();
			this.btnDownloadFile.enable();
			this.btnOpen.enable();

		}
		
	}
	
	public void activeAllButtonWithoutGroupView(boolean active){
		
			btnRemoveItem.setEnabled(active);
			btnRenameItem.setEnabled(active);
			btnDownloadFile.setEnabled(active);
			btnPreview.setEnabled(active);
			btnOpen.setEnabled(active);
	}

	public ToolBar getToolBar() {
		return this.toolBar;
	}

}
