/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.codeclassifier.nuts;

import javax.xml.ws.WebServiceRef;
import org.codeclassifier.voc.*;
import org.codeclassifier.voc.*;
import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;



import javax.faces.FacesException;

import javax.faces.component.UIData;
import javax.faces.context.ExternalContext;

import javax.faces.context.FacesContext;

import org.ellectra.gui.utils.JSFUtils;

import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.ellectra.code.reader.list.nutsreader;
import org.ellectra.code.reader.list.vocreader;
import org.ellectra.gui.managebeans.pathSettings;
import org.richfaces.component.UITree;

import org.richfaces.event.NodeSelectedEvent;

import org.richfaces.model.TreeNode;

import org.richfaces.model.TreeNodeImpl;
//import org.test.dd.AuthenAuthoService;

/**
 *
 * @author tdi
 */
public class nutsnodelist {
  //  @WebServiceRef(wsdlLocation = "http://localhost:18181/Authen_AuthoService/Authen_AuthoPort?wsdl")
  //  private AuthenAuthoService service;




    

    private TreeNode rootNode = null;
private ArrayList<nutsselectedlist> allcpvcode=new ArrayList();
    private ArrayList<nutsselectedlist> cpvselectedcode=new ArrayList();
    private ArrayList<nutsselectedlist> cpvselectedcodelistbox=new ArrayList();
    private ArrayList<nutsselectedlist> cpvsearchfoundcode=new ArrayList();
private List cpvselectedcode1 = Collections.EMPTY_LIST;
private List searchfoundcodes = Collections.EMPTY_LIST;
    private String nodeTitle;
    private UIData cpvdata = null;
    private String selectedoncecode;
    private String searchforcpvcodewithcpvcode;
    private String selectedcodefromsearch;
    
    private void loadTree()
    {
        rootNode = new TreeNodeImpl();
        
        TreeNodeImpl addimp = new TreeNodeImpl();
        addimp.setData("main CPV");
        rootNode.addChild(1, addimp);
        
        TreeNodeImpl addimp2 = new TreeNodeImpl();
        addimp2.setData("SUB CPV");
        addimp.addChild(2, addimp2);
        
        TreeNodeImpl addimp3 = new TreeNodeImpl();
        addimp3.setData("CPV");
        addimp2.addChild(3, addimp3);
        
        nutsreader dcpv= new nutsreader();
        pathSettings dset=new pathSettings();
        String szPathcsv =dset.GetSelectorsDirectory();
        dcpv.ReadNutsCodesFromFile(szPathcsv+"nuts6.csv", "EN");
        
        // the max elements found
        int imax=dcpv.GetMaxSize()-1;
        TreeNodeImpl add0=null;
        TreeNodeImpl add1=null;
        TreeNodeImpl add2=null;
        TreeNodeImpl add3=null;
        
        //TreeNodeImpl add2=null;
        
        for (int dd=0;dd<imax;dd++)
        {
            
            String szCode=dcpv.GetCodeFor(dd,1);
            String szdesc=dcpv.GetCodeFor(dd,2);
            String szl1=dcpv.GetCodeFor(dd,3);
            String szl2=dcpv.GetCodeFor(dd,4);
            String szl3=dcpv.GetCodeFor(dd,5);
            
            nutsselectedlist e = new nutsselectedlist();
            if (szdesc.length()>0)
                e.setSzcode(szCode+"-"+szdesc);
            if (szl1.length()>0)
                e.setSzcode(szCode+"-"+szl1);
            if (szl2.length()>0)
                e.setSzcode(szCode+"-"+szl2);
            if (szl3.length()>0)
                e.setSzcode(szCode+"-"+szl3);
            
            allcpvcode.add(e);
            
            System.err.print(szCode);
            if (szdesc.length()>0)
            {
                add0 = new TreeNodeImpl();
                add0.setData(szCode+"-"+szdesc);
                rootNode.addChild(dd+1, add0);
            }
            if (szl1.length()>0)
            {
                add1 = new TreeNodeImpl();
                add1.setData(szCode+"-"+szl1);
                add0.addChild(dd+1, add1);
            }
            if (szl2.length()>0)
            {
                add2 = new TreeNodeImpl();
                add2.setData(szCode+"-"+szl2);
                add1.addChild(dd+1, add2);
            }
            if (szl3.length()>0)
            {
                add3 = new TreeNodeImpl();
                add3.setData(szCode+"-"+szl3);
                add2.addChild(dd+1, add3);
            }
            
            
        }
        
    }
    

    
   public void removeFromList()
   {
       for (int dd=0;dd<cpvselectedcode.size();dd++)
       {
           nutsselectedlist ditem=cpvselectedcode.get(dd);
           if (ditem.getSzcode().equalsIgnoreCase(selectedoncecode))
           {
               cpvselectedcode.remove(dd);
               //cpvselectedcodelistbox.remove(dd);
           }
        //ditem.setSzcode(nodeTitle);
        //cpvselectedcode.add(ditem);
       }
       //cpvselectedcode.remove(1);
   }
   
    public TreeNode getTreeNode() {

        if (rootNode == null) {

            loadTree();

        }

        

        return rootNode;

    }



    public void processSelection(NodeSelectedEvent event) {

        UITree tree = (UITree) event.getComponent();

        nodeTitle = (String) tree.getRowData();
        
        
        nutsselectedlist ditem=new nutsselectedlist();
        ditem.setSzcode(nodeTitle);
        cpvselectedcode.add(ditem);
    }

    

    public String getNodeTitle() {

        return nodeTitle;

    }



    public void setNodeTitle(String nodeTitle) {

        this.nodeTitle = nodeTitle;

    }

    public

    ArrayList<nutsselectedlist> getCpvselectedcode() {
        return cpvselectedcode;
    }

    public void setCpvselectedcode(ArrayList<nutsselectedlist> cpvselectedcode) {
        this.cpvselectedcode = cpvselectedcode;
    }

    public UIData getCpvdata() {
        return cpvdata;
    }

    public void setCpvdata(UIData cpvdata) {
        this.cpvdata = cpvdata;
    }

    public List getCpvselectedcode1() {
        List orgs = new ArrayList();
        //here remove description
        //SelectItem item2 = new SelectItem("","");
        //    orgs.add(item2);
        for(int i=0;i<cpvselectedcode.size();i++)
        {
            nutsselectedlist ditem=cpvselectedcode.get(i);
            //org.ellectra.wsws.OrgType dele=dInitalOrgList.get(i);
            //String orgid = Integer.toString(dele.getId());
             //int j = ditem.getSzcode().indexOf("-", 0);

             //String F1 = ditem.getSzcode().substring(0, j);
             //String F2 = ditem.getSzcode().substring(j + 1, ditem.getSzcode().length());
             //int j2 = F2.indexOf("-", 0);
             //String F3 = F2.substring(0, j2);
             //String FinalString=F1+"-"+F3;
             //cpvselectedlist ditem22 = new cpvselectedlist();
             //ditem22.setSzcode(FinalString);
                  //   cpvselectedcodelistbox.add(ditem22);
            SelectItem item = new SelectItem(ditem.getSzcode(),ditem.getSzcode());
            orgs.add(item);
            
        }    
        return orgs;
        //return cpvselectedcode1;
    }

    public void setCpvselectedcode1(List cpvselectedcode1) {
        this.cpvselectedcode1 = cpvselectedcode1;
    }

    public String getSelectedoncecode() {
        return selectedoncecode;
    }

    public void setSelectedoncecode(String selectedoncecode) {
        this.selectedoncecode = selectedoncecode;
    }

    public String getSearchforcpvcodewithcpvcode() {
        return searchforcpvcodewithcpvcode;
    }

    public void setSearchforcpvcodewithcpvcode(String searchforcpvcodewithcpvcode) {
        this.searchforcpvcodewithcpvcode = searchforcpvcodewithcpvcode;
    }
        public void searchforcodevcl(ValueChangeEvent e)throws AbortProcessingException {


        searchforcpvcodewithcpvcode = e.getNewValue().toString();
        }
    public void search()
    {
        //searchforcpvcodewithcpvcode
        //private ArrayList<cpvselectedlist> cpvsearchfoundcode=new ArrayList();
        for (int ds=0;ds<allcpvcode.size();ds++)
        {   
            nutsselectedlist ditem2= new nutsselectedlist();
            ditem2=allcpvcode.get(ds);
            if (ditem2.getSzcode().contains(searchforcpvcodewithcpvcode))
            {
            nutsselectedlist ditem= new nutsselectedlist();
            ditem.setSzcode(ditem2.getSzcode());
            cpvsearchfoundcode.add(ditem);
            }
        }
    }
    
    public void addOnTheList()
    {
    //selectedcodefromsearch
         nutsselectedlist ditem= new nutsselectedlist();
        ditem.setSzcode(selectedcodefromsearch);
        cpvselectedcode.add(ditem);
    }
    
    public String getSelectedcodefromsearch() {
        return selectedcodefromsearch;
    }

    public void setSelectedcodefromsearch(String selectedcodefromsearch) {
        this.selectedcodefromsearch = selectedcodefromsearch;
    }

    public List getSearchfoundcodes() {
        List orgs = new ArrayList();
         //SelectItem item2 = new SelectItem("","");
           // orgs.add(item2);
        for(int i=0;i<cpvsearchfoundcode.size();i++)
        {
            nutsselectedlist ditem=cpvsearchfoundcode.get(i);
            //org.ellectra.wsws.OrgType dele=dInitalOrgList.get(i);
            //String orgid = Integer.toString(dele.getId());
            SelectItem item = new SelectItem(ditem.getSzcode(),ditem.getSzcode());
            orgs.add(item);
            
        }    
        return orgs;
        //private ArrayList<cpvselectedlist> cpvsearchfoundcode=new ArrayList();
        //private List cpvselectedcode1 = Collections.EMPTY_LIST;
        //return searchfoundcodes;
    }

    public void setSearchfoundcodes(List searchfoundcodes) {
        this.searchfoundcodes = searchfoundcodes;
    }

     public String MainCodeForCreateTender()
    {
        String AllCodes="";
        for (int dds=0;dds<cpvselectedcode.size();dds++)
        {
            nutsselectedlist ditem= cpvselectedcode.get(dds);
            AllCodes=AllCodes+","+ditem.getSzcode();
        }
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "NUTSFORCREATETENDER", AllCodes);
        return "CallOfTenderFromNUTS";
    }
     
     public String MainCodeForCreateTender2()
    {
        String AllCodes="";
        for (int dds=0;dds<cpvselectedcode.size();dds++)
        {
            nutsselectedlist ditem= cpvselectedcode.get(dds);
            AllCodes=AllCodes+","+ditem.getSzcode();
        }
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "NUTSFORPIN", AllCodes);
        return "GotoPIN";
    }
     
     
//public void test()
//{
//   
//try { // Call Web Service Operation
//   org.test.dd.AuthenAuthoPortType port = service.getAuthenAuthoPort();
//   	 // TODO initialize WS operation arguments here
//	org.test.dd.UcRequest part1 = new org.test.dd.UcRequest();
//   // TODO process result here
//   org.test.dd.UcResponse result = port.authenAuthoOperation(part1);
//   System.out.println("Result = "+result);
//} catch (Exception ex) {
//   // TODO handle custom exceptions here
//}
// 
//}
}
