package test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.Cookie;
import javax.validation.constraints.Size;



import my_tree.TUser;

import org.ajax4jsf.model.DataVisitor;
import org.ajax4jsf.model.ExtendedDataModel;
import org.ajax4jsf.model.Range;
import org.richfaces.component.SortOrder;
import org.richfaces.component.UIExtendedDataTable;











import dater.tableItem;

@ManagedBean
@SessionScoped
public class UserBean implements Serializable, ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6037198387368232031L;

	public Map<String, SortOrder> sortsOrders;

	 public UserBean() {
	        sortsOrders = new HashMap<String, SortOrder>();
	        //sortPriorities = new ArrayList<String>();
	        //this.selection=	new ArrayList<tableItem>();
	  //      Collection<Object> ok=new ArrayList<Object>();
	  //      ok.add(new tableItem("500","500"));
	  //      this.setSelection(ok);
	       // tableItem("500","500");
	        user=new TUser();
	        ExternalContext k=FacesContext.getCurrentInstance().getExternalContext();
		    //  k.addResponseCookie("kyky","000", null);
	        Map<String, Object> cookies = k.getRequestCookieMap();
	        for(Entry<String, Object> it:cookies.entrySet())
	        {
	        	System.out.println(it.getKey()+" "+it.getValue());
	        }
	        if(null!=cookies.get("username"))
	        {
	        	Cookie cookie=(Cookie)cookies.get("username");        			
	        	user.setName(cookie.getValue());
	        	//user.doLoging();
	        }
	    }
	 
    public List<String> autocomplete(String prefix) {

        ArrayList<String> result = new ArrayList<String>();
        result.add("England");
        result.add("France");
        result.add("Germany");
        result.add("Italy");
        result.add("Spain");

        return result;
    }

    @Size(min=3, max=12,message="Must be between 3 and 12 chars")
    public String name;

    public String state;
    public String adress;
    
    TUser user;

    public TUser getUser() {
    	if(null==user)user=new TUser();
		return user;
	}

	public void setUser(TUser user) {
		this.user = user;
	}

	public String getState() {
        return state;
    }

    public String getName() {
        return name;
    }

	public String getAdress() {
		return adress;
	}

	public void setAdress(String adress) {
		this.adress = adress;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setState(String state) {
		this.state = state;
	}

	@Override
	public void processAction(ActionEvent arg0) throws AbortProcessingException {
		// TODO Auto-generated method stub
	System.out.println("processAction");this.name="*****";
	}
	
	public void selectionListener(AjaxBehaviorEvent event) {
		System.out.println("selectionListener0");
		UIExtendedDataTable dataTable = (UIExtendedDataTable) event.getComponent();
		System.out.println("selectionListener1 "+dataTable.getSelection());
		System.out.println("selection->"+selection);
		if(null!=dataTable.getSelection()&&0<dataTable.getSelection().size())
		{
		System.out.println("selectionListener2");
		//this.selection=(tableItem)dataTable.getSelection().toArray()[0];
        }
		
	}
	
	
	public List<tableItem>  arl;
	public Collection<Object> selection ;
	public tableItem singleItemSel;
	
	
	public synchronized List<tableItem> getArl() {
		if(null==this.arl )
		{
			this.arl=new ArrayList<tableItem>();
			for(int i=0;i<100;i++)
			{
				tableItem it=new tableItem(String.valueOf(i), String.valueOf(i));
				it.setA0(String.valueOf(i));
				this.arl.add(it);
			}
		}
		
		return this.arl;
	}


	public void setArl(List<tableItem> arl) {
		
		this.arl = arl;
	}
		
	




	public Collection<Object> getSelection() {
		return selection;
	}

	public void setSelection(Collection<Object> selection) {
		this.selection = selection;
	}

	public tableItem getSingleItemSel()
	{
		if(null==this.selection||this.selection.isEmpty())return null;// new tableItem("100","200");
		this.singleItemSel=this.arl.get((int) this.selection.toArray()[0]);
		System.out.println(" getSingleItemSel() "+this.singleItemSel);
		name=this.singleItemSel.toString();
		return this.singleItemSel;
	}
	
	public void setSingleItemSel(tableItem singleItemSel) {
		this.singleItemSel = singleItemSel;
	}

	
	public void sort() 
	{
		System.out.println("sort() ");
		if(null==sortsOrders.get("one")){sortsOrders.put("one", SortOrder.ascending);System.out.println("put");}
		
		if(SortOrder.ascending==sortsOrders.get("one"))
			sortsOrders.put("one", SortOrder.descending);
		else 
			sortsOrders.put("one", SortOrder.ascending);
			
		
		System.out.println("=="+sortsOrders.get("one"));
			if(SortOrder.ascending==sortsOrders.get("one"))
			{System.out.println("0,true");
		Collections.sort(this.arl, new TComparator0(0,true));
		return;
		    }
			if(SortOrder.descending==sortsOrders.get("one"))
			{System.out.println("0,false");
		Collections.sort(this.arl, new TComparator0(0,false));
		return;
		    }
        

    }

	public Map<String, SortOrder> getSortsOrders() {
		return sortsOrders;
	}

	public void setSortsOrders(Map<String, SortOrder> sortsOrders) {
		this.sortsOrders = sortsOrders;
	}
    
    

}
class TComparator0 implements Comparator<tableItem>
{
	int column;
	boolean sortType;
	public TComparator0(int column,boolean sortType){this.column=column;this.sortType=sortType;}
	@Override
	public int compare(tableItem o1, tableItem o2) {
		// TODO Auto-generated method stub
		if(0==this.column)
		{
		Integer i0=Integer.valueOf(o1.a0);
		Integer i1=Integer.valueOf(o2.a0);
		if(sortType)return i0.compareTo(i1);else return i1.compareTo(i0);
		}
		if(1==this.column)
		{
		Integer i0=Integer.valueOf(o1.a1);
		Integer i1=Integer.valueOf(o2.a1);
		if(sortType)return i0.compareTo(i1);else return i1.compareTo(i0);
		}
		return 0;
	}
	
	
	
	
	
	
	
	
	
}





