package stations;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

import org.richfaces.component.UITree;
import org.richfaces.event.TreeSelectionChangeEvent;
import org.richfaces.event.TreeSelectionChangeListener;

import javax.swing.tree.TreeNode;

import dater.dater;
import dater.tableItem;
import my_tree.OptionTreeNode;


@ManagedBean(name = "stations")

@SessionScoped  
@ViewScoped
public class Stations implements Serializable, TreeSelectionChangeListener,
ActionListener{
	/**
	 * 
	 */
	@Resource(mappedName="java:/my_connect") 
	private javax.sql.DataSource ds00;
	//îí â /jsf/WebContent/WEB-INF/my_connect-ds.xml
	
	
	
	
	public javax.sql.DataSource getDs00() {
		return ds00;
	}

	private static final long serialVersionUID = 1L;

	public List<OptionTreeNode> stationRoots=null; 
	public OptionTreeNode stationRoot= null; 
	//private OptionTreeNode stationNodes = new OptionTreeNode(); 
	
	
	
	 private OptionTreeNode currentSelection = null;
	 
	public List<OptionTreeNode> getStationRoots() {
		return stationRoots;
	}

	public void setStationRoots(List<OptionTreeNode> stationRoots) {
		this.stationRoots = stationRoots;
	}

	
	
	
	
	public OptionTreeNode getCurrentSelection() {
		System.out.println("getCurrentSelection()");
		return currentSelection;
	}

	public void setCurrentSelection(OptionTreeNode currentSelection) {
		this.currentSelection = currentSelection;
	}

	public OptionTreeNode getStationRoot() {
	//	h();
		return stationRoot;
	}

	public void setStationRoot(OptionTreeNode stationRoot) {
		this.stationRoot = stationRoot;
	}

	private String sss="12300*";
	private String sss0="12300*";
	
	@ManagedProperty(value = "#{userBean.arl}")//@ViewScoped in userBean
	List<tableItem>  arl;
	public List<tableItem> getArl() {
		return this.arl;
	}

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

	
	public String getSss() {
		return sss;
	}

	public void setSss(String sss) {
		this.sss = sss;
	}
	
	public String getSss0() {
		return sss0;
	}

	public void setSss0(String sss0) {
		System.out.println("setSss0="+sss0);
		this.sss0 = sss0;
	}

	
	
 
	//--------------------------------------------------------
//--------------------------------------------------------
	public String say(){return sss;}
	
	public Stations()
	{
		
	}

	private String[] kickRadioFeed = { 
		"Hall & Oates - Kiss On My List",
	    "David Bowie - Let's Dance", 
	    "Lyn Collins - Think (About It)",
	    "Kim Carnes - Bette Davis Eyes",
	    "KC & the Sunshine Band - Give It Up" };

	    
	@PostConstruct
	public void h()
	{
	stationRoot = new OptionTreeNode("**",null);
	stationRoot.setName("KickRadio");
	this.stationRoots=new ArrayList<OptionTreeNode>();
	this.stationRoots.add(stationRoot);

	for (int i = 0; i < kickRadioFeed.length; i++)
	{
	OptionTreeNode child = new OptionTreeNode(kickRadioFeed[i],stationRoot);
	child.setName(kickRadioFeed[i]);
	stationRoot.addChild( child);
	}
	
	currentSelection=stationRoot;
	
	
	}
	//----------------------------
	public void selectionChanged(TreeSelectionChangeEvent selectionChangeEvent)
	{
		System.out.println("selectionChanged");
		List<Object> selection = new ArrayList<Object>(selectionChangeEvent.getNewSelection());
        Object currentSelectionKey = selection.get(0);
        UITree tree = (UITree) selectionChangeEvent.getSource();
 
        Object storedKey = tree.getRowKey();
        tree.setRowKey(currentSelectionKey);
        currentSelection = (OptionTreeNode) tree.getRowData();
        tree.setRowKey(storedKey);
        ((OptionTreeNode)currentSelection).addChild(new OptionTreeNode("child",currentSelection));
	    sss=  ((OptionTreeNode)currentSelection).getName()/*+dater.getStr(ds00);*/;
	    //System.out.println("bilo "+ ((OptionTreeNode)currentSelection).loading);
	    //((OptionTreeNode)currentSelection).loading="paneltree";
	    //System.out.println("stalo "+ ((OptionTreeNode)currentSelection).loading);
	}
	
	public void onbeforeselectionchange()
	{
		System.out.println("onbeforeselectionchange");
	}
	
	public void onClick()
	{
		//OptionTreeNode child = new OptionTreeNode();
		//child.setName("**-->");
		//stationRoot.addChild(0, child);
	}

	@Override
	public void processTreeSelectionChange(TreeSelectionChangeEvent event)
			throws AbortProcessingException {
		System.out.println("processTreeSelectionChange");
		UITree tree = (UITree)event.getSource();
		List<Object> selection = new ArrayList<Object>(event.getNewSelection());
	    Object currentSelectionKey = selection.get(0);
	    OptionTreeNode child = (OptionTreeNode)currentSelectionKey;
	    child.setName("**-->");
	    OptionTreeNode currentSelection = (OptionTreeNode) tree.getRowData();
		currentSelection.addChild(child);
	}

	@Override
	public void processAction(ActionEvent arg0) throws AbortProcessingException {
		// TODO Auto-generated method stub

//		<h:commandButton value="Invoke listener method" onclick="#{stations.clickBut}">
//		<a4j:actionListener listener="#{stations.processAction}"/>
//		<f:ajax render="cs"/>
//		<f:ajax render="messages0"/>
//		</h:commandButton>
		
		HtmlCommandButton htmCB=(HtmlCommandButton)arg0.getSource();
		
		System.out.println("arg0.getPhaseId().getOrdinal() "+arg0.getPhaseId());
		System.out.println("processAction "+htmCB.getValue()+" "+arg0);
		
		 Map<String,Object> map=FacesContext.getCurrentInstance().
         getExternalContext().getSessionMap();

		 for(Map.Entry i:map.entrySet())
		 {
			// System.out.println("key="+i.getKey()+" val= "+i.getValue());
			 if(i.getKey().equals("stations"))
			 {
				 System.out.println("i.getKey().equals(\"stations\")");
				 Stations st=(Stations) i.getValue();
			     Date d=new Date();
			     SimpleDateFormat df=new SimpleDateFormat("dd.MM.yyyy HH.mm.ss S");
				 st.setSss0(df.format(d)+"+"+df.format(d));
			 }
		 }
		 
		 
		 
	
		     Date d=new Date();
		     SimpleDateFormat df=new SimpleDateFormat("dd.MM.yyyy HH.mm.ss S");
		     
		     
		     //System.out.println("ds00="+ds00);
		     sss=df.format(d);//+dater.getStr(ds00);
		
	}
	
	public void clickBut()
	{
	System.out.println("clickBut()");
//		Map<String,Object> map=FacesContext.getCurrentInstance().
//		         getExternalContext().getSessionMap();
//
//				 for(Map.Entry i:map.entrySet())
//				 {
//					// System.out.println("key="+i.getKey()+" val= "+i.getValue());
//					 if(i.getKey().equals("stations"))
//					 {
//						 System.out.println("i.getKey().equals(\"stations\")");
//						 Stations st=(Stations) i.getValue();
//					     Date d=new Date();
//					     SimpleDateFormat df=new SimpleDateFormat("dd.MM.yyyy HH.mm.ss S");
//						 st.setSss0(df.format(d)+"+"+df.format(d));
//					 }
//				 }
//				 
//				 
//				 
//			
//				     Date d=new Date();
//				     SimpleDateFormat df=new SimpleDateFormat("dd.MM.yyyy HH.mm.ss S");
//				     
//				     
//				     //System.out.println("ds00="+ds00);
//				     sss=df.format(d);//+dater.getStr(ds00);	
	}
	
	public void clickBut2()
	{
		 System.out.println("clickBut2()");
	}
	
	
	
}

