package org.gcube.portlets.widgets.workspacesharingwidget.client.view.sharing.multisuggest;

import java.util.ArrayList;
import java.util.List;

import org.gcube.portlets.widgets.workspacesharingwidget.client.ConstantsSharing;
import org.gcube.portlets.widgets.workspacesharingwidget.client.resources.Resources;
import org.gcube.portlets.widgets.workspacesharingwidget.shared.InfoContactModel;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.dnd.GridDragSource;
import com.extjs.gxt.ui.client.dnd.GridDropTarget;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
  
public class MultiDragContact extends Dialog {  
  
  private GridDropTarget dropSource;  
  private GridDropTarget dropTarget; 
  private ListStore<InfoContactModel> storeSource = new ListStore<InfoContactModel>();
  private ListStore<InfoContactModel> storeTarget = new ListStore<InfoContactModel>();
  private TextArea textAreaAlreadyShared;
  private Grid<InfoContactModel> gridAllContacts;
  private Grid<InfoContactModel> gridShareWith;
 
  public MultiDragContact() {
	setStyleAttribute("margin", "10px");   
    setSize(563, 542);
    setHeading("Group dragging contacts");
    setIcon(Resources.getIconUsers());
    setModal(true);
    setScrollMode(Scroll.AUTOY);
    setBodyStyle("padding: 9px; background: none");
    setResizable(true);
    setButtonAlign(HorizontalAlignment.CENTER); 
    setButtons(Dialog.OKCANCEL);
    
    ContentPanel cpAlreadyShared = new ContentPanel(); 
    cpAlreadyShared.setSize(530, 60);  
    cpAlreadyShared.setHeaderVisible(false);
    
    cpAlreadyShared.setLayout(new FitLayout());
    
    VerticalPanel vpShared = new VerticalPanel();
    vpShared.setVerticalAlign(VerticalAlignment.MIDDLE);
    vpShared.setHorizontalAlign(HorizontalAlignment.CENTER);
    vpShared.setStyleAttribute("padding", "5px");
    vpShared.setLayout(new FitLayout());
    HorizontalPanel hpSharedContacts = new HorizontalPanel();
    hpSharedContacts.setHorizontalAlign(HorizontalAlignment.CENTER);
    hpSharedContacts.setVerticalAlign(VerticalAlignment.MIDDLE);
    textAreaAlreadyShared = new TextArea();
//    textField.setFieldLabel("Already shared with");
//    textField.setHeight(30);
    textAreaAlreadyShared.setWidth(401);
    textAreaAlreadyShared.setHeight(43);
    cpAlreadyShared.setStyleAttribute("padding-bottom", "5px");
    textAreaAlreadyShared.setReadOnly(true);
    cpAlreadyShared.add(textAreaAlreadyShared);
    
    Label label = new Label("Already shared with");
    label.setStyleAttribute("padding-right", "10px");

    hpSharedContacts.add(label);
    hpSharedContacts.add(textAreaAlreadyShared);
    vpShared.add(hpSharedContacts);
    cpAlreadyShared.add(vpShared);
    add(cpAlreadyShared);
    
    
    ContentPanel cp = new ContentPanel();  
    cp.setSize(530, 370);  
    cp.setHeaderVisible(false);
    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  

    ToolBar toolBar = new ToolBar();  
    Button buttonHelp = new Button();
    buttonHelp.setIcon(Resources.getIconInfo());

    buttonHelp.addSelectionListener(new SelectionListener<ButtonEvent>() {
		
		@Override
		public void componentSelected(ButtonEvent ce) {
			MessageBox.info("Group dragging action", "Drag one or more contacts from the left (All Contacts) to the right (Share with) to add users in your sharing list.", null);
			
		}
	});
    
    toolBar.add(buttonHelp);  
    setTopComponent(toolBar); 

    VerticalPanel vp = new VerticalPanel();
    vp.setHorizontalAlign(HorizontalAlignment.CENTER);
    vp.add(new Label("All Contacts"));
    gridAllContacts = new Grid<InfoContactModel>(storeSource, createColumnModel());
    
    storeSource.setDefaultSort(InfoContactModel.FULLNAME, SortDir.ASC);
    storeSource.sort(InfoContactModel.FULLNAME, SortDir.ASC);
    
    storeTarget.setDefaultSort(InfoContactModel.FULLNAME, SortDir.ASC);
    storeTarget.sort(InfoContactModel.FULLNAME, SortDir.ASC);
    
    
	StoreFilterField<InfoContactModel> filter = new StoreFilterField<InfoContactModel>() {

		@Override
		protected boolean doSelect(Store<InfoContactModel> store,
				InfoContactModel parent, InfoContactModel record,
				String property, String filter) {

			String name = record.getName();
			name = name.toLowerCase();
			if (name.contains(filter.toLowerCase())) {
				return true;
			}
			return false;
		}

	};
      
//    filter.setFieldLabel("Filter Contacts");
    filter.setWidth(247);
    filter.setEmptyText("Filter All Contacts");
    HorizontalPanel hp = new HorizontalPanel();
    hp.setStyleAttribute("padding-top", "5px");
    hp.setStyleAttribute("padding-bottom", "5px");
    hp.add(filter);
    filter.bind(storeSource); 
    
    gridAllContacts.setSize(250,310);
    gridAllContacts.setBorders(false);  
    gridAllContacts.setAutoExpandColumn(InfoContactModel.FULLNAME);  
    gridAllContacts.setBorders(true);  
    
    vp.add(hp);
    vp.add(gridAllContacts);


      
    RowData rowData = new RowData(.5, 1);  
    rowData.setMargins(new Margins(6));  
    cp.add(vp, rowData);   
  
    vp = new VerticalPanel();
    vp.setHorizontalAlign(HorizontalAlignment.CENTER);
    vp.add(new Label("Share with..."));
    gridShareWith = new Grid<InfoContactModel>(storeTarget, createColumnModel()); 
    
	StoreFilterField<InfoContactModel> filter2 = new StoreFilterField<InfoContactModel>() {

		@Override
		protected boolean doSelect(Store<InfoContactModel> store,
				InfoContactModel parent, InfoContactModel record,
				String property, String filter) {

			String name = record.getName();
			name = name.toLowerCase();
			if (name.contains(filter.toLowerCase())) {
				return true;
			}
			return false;
		}

	};
      
//    filter.setFieldLabel("Filter Contacts");
    filter2.setWidth(247);
    filter2.setEmptyText("Filter Share with");

    hp = new HorizontalPanel();
    hp.setStyleAttribute("padding-top", "5px");
    hp.setStyleAttribute("padding-bottom", "5px");
    hp.add(filter2);
    filter2.bind(storeTarget); 
    
    
    gridShareWith.setSize(250, 310);
    gridShareWith.setBorders(false);  
    gridShareWith.setAutoExpandColumn(InfoContactModel.FULLNAME);  
    gridShareWith.setBorders(true);
    vp.add(hp);
    vp.add(gridShareWith);
    
    rowData = new RowData(.5, 1);  
    rowData.setMargins(new Margins(6, 6, 6, 0));  
    cp.add(vp, rowData);  
  
    new GridDragSource(gridAllContacts);  
    new GridDragSource(gridShareWith);  
  
    dropSource = new GridDropTarget(gridAllContacts);  
    dropSource.setAllowSelfAsSource(false);  
  
    dropTarget = new GridDropTarget(gridShareWith);  
    dropTarget.setAllowSelfAsSource(false);  
  
    add(cp);  

    this.getButtonById(Dialog.CANCEL).addSelectionListener(new SelectionListener<ButtonEvent>() {

		@Override
		public void componentSelected(ButtonEvent ce) {
			hide();
		}
	});
    
    
    this.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener<ButtonEvent>() {

		@Override
		public void componentSelected(ButtonEvent ce) {
				hide();
		}
	});   
  }  
  
  private ColumnModel createColumnModel() {  
    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
  
    ColumnConfig column = new ColumnConfig();  
    column.setId(InfoContactModel.FULLNAME);  
    column.setHeader("Name");  
    column.setWidth(200);  
    configs.add(column);  
  
//    column = new ColumnConfig();  
//    column.setId("symbol");  
//    column.setHeader("Symbol");  
//    column.setWidth(100);  
//    configs.add(column);  
  
    return new ColumnModel(configs);  
  }  
  
  public void addSourceContacts(List<InfoContactModel> listContact){
	  
	  gridAllContacts.mask("", ConstantsSharing.LOADINGSTYLE);
	  if(listContact!=null && listContact.size()>0){
		  
//		  for (InfoContactModel infoContactModel : listContact) {
//			
//			  if(infoContactModel.getName()!=null && !infoContactModel.getName().isEmpty())
//				  storeSource.add(infoContactModel);  
//		}
		  
		  storeSource.add(listContact);
	  }
	  gridAllContacts.unmask();
  }
  
  public void addAlreadySharedContacts(List<InfoContactModel> listContact){
	  
	  gridShareWith.mask("", ConstantsSharing.LOADINGSTYLE);
	  if(listContact!=null && listContact.size()>0){
		  
		  String alreadyShared = "";
		  for (int i=0; i<listContact.size()-1; i++) 
			  alreadyShared+=listContact.get(i).getName()+", ";
			
		  alreadyShared+=listContact.get(listContact.size()-1).getName();
		  
		  textAreaAlreadyShared.setValue(alreadyShared);
	  }
	  gridShareWith.unmask();
		 
  }
  
  public void addTargetContacts(List<InfoContactModel> listContact){
	  if(listContact!=null && listContact.size()>0)
		  storeTarget.add(listContact);  
  }
  
  public void addTargetContact(InfoContactModel contact){
	  if(contact!=null)
		  storeTarget.add(contact);  
  }
  
  public List<InfoContactModel> getTargetListContact(){
	  return storeTarget.getModels();
  }
  
}  
