/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.codeclassifier.cpv;

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 javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import org.ellectra.code.reader.list.cpvreader;
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.ellectra.gui.utils.JSFUtils;

/**
 *
 * @author tdi
 */
public class cpvnodelist {

    private TreeNode rootNode = null;
    private ArrayList<cpvselectedlist> allcpvcode = new ArrayList();
    private ArrayList<cpvselectedlist> cpvselectedcode = new ArrayList();
    private ArrayList<cpvselectedlist> cpvselectedcodelistbox = new ArrayList();
    private ArrayList<cpvselectedlist> 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);

        cpvreader dcpv = new cpvreader();
        pathSettings dset=new pathSettings();
        String szPathcsv=dset.GetSelectorsDirectory();
        
    
        dcpv.ReadCPVCodesFromFile(szPathcsv+"cpv.csv", "EN");

        // the max elements found
        int imax = dcpv.GetMaxSize() - 1;
        TreeNodeImpl add0 = null;
        TreeNodeImpl add1 = null;
        TreeNodeImpl add2 = null;

        for (int dd = 0; dd < imax; dd++) {

            String szCode = dcpv.GetCodeFor(dd);
            String szdesc = dcpv.GetDescFor(dd);
            cpvselectedlist e = new cpvselectedlist();
            e.setSzcode(szCode + "-" + szdesc);

            allcpvcode.add(e);
            if (szCode.charAt(2) == '0') {

                add0 = new TreeNodeImpl();
                add0.setData(szCode + "-" + szdesc);
                rootNode.addChild(dd + 1, add0);

            } else if (szCode.charAt(3) == '0') {

                add1 = new TreeNodeImpl();
                add1.setData(szCode + "-" + szdesc);
                add0.addChild(dd + 1, add1);

            } else {
                add2 = new TreeNodeImpl();
                add2.setData(szCode + "-" + szdesc);
                add1.addChild(dd + 1, add2);
            }



        }

    }

    public void removeFromList() {
        for (int dd = 0; dd < cpvselectedcode.size(); dd++) {
            cpvselectedlist 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();


        cpvselectedlist ditem = new cpvselectedlist();
        ditem.setSzcode(nodeTitle);
        cpvselectedcode.add(ditem);
    }

    public String getNodeTitle() {

        return nodeTitle;

    }

    public void setNodeTitle(String nodeTitle) {

        this.nodeTitle = nodeTitle;

    }

    public ArrayList<cpvselectedlist> getCpvselectedcode() {
        return cpvselectedcode;
    }

    public void setCpvselectedcode(ArrayList<cpvselectedlist> 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
        for (int i = 0; i < cpvselectedcode.size(); i++) {
            cpvselectedlist 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(), FinalString);
            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++) {
            cpvselectedlist ditem2 = new cpvselectedlist();
            ditem2 = allcpvcode.get(ds);
            if (ditem2.getSzcode().contains(searchforcpvcodewithcpvcode)) {
                cpvselectedlist ditem = new cpvselectedlist();
                ditem.setSzcode(ditem2.getSzcode());
                cpvsearchfoundcode.add(ditem);
            }
        }
    }

    public void addOnTheList() {
        //selectedcodefromsearch
        cpvselectedlist ditem = new cpvselectedlist();
        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();

        for (int i = 0; i < cpvsearchfoundcode.size(); i++) {
            cpvselectedlist 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++) {
            cpvselectedlist ditem = cpvselectedcode.get(dds);
            AllCodes = AllCodes + "," + ditem.getSzcode();
        }
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "MAINCPVFORCREATETENDER", AllCodes);
        return "CallOfTenderFromCPV";
    }

    public String MainCodeForCreateTender2() {
        String AllCodes = "";
        for (int dds = 0; dds < cpvselectedcode.size(); dds++) {
            cpvselectedlist ditem = cpvselectedcode.get(dds);
            AllCodes = AllCodes + "," + ditem.getSzcode();
        }
        System.err.print("goback");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "MAINCPVFORSEARCH", AllCodes);
        return "MAINCPVFORSEARCHBACK";
    }

    public String MainCodeForCreateTender3() {
        String AllCodes = "";
        for (int dds = 0; dds < cpvselectedcode.size(); dds++) {
            cpvselectedlist ditem = cpvselectedcode.get(dds);
            AllCodes = AllCodes + "," + ditem.getSzcode();
        }
        System.err.print("goback");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "MAINCPVFORSEARCH", AllCodes);
        return "MAINCPVFORSEARCHBACKe";
    }

    public String MainCodeForCreateTender4() {
        String AllCodes = "";
        for (int dds = 0; dds < cpvselectedcode.size(); dds++) {
            cpvselectedlist ditem = cpvselectedcode.get(dds);
            AllCodes = AllCodes + "," + ditem.getSzcode();
        }
        System.err.print("goback");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "MAINCPVFORSEARCH", AllCodes);
        return "MAINCPVFORSEARCHBACKo";
    }

    public String MainCodeForCreateTenderpin() {
        String AllCodes = "";
        for (int dds = 0; dds < cpvselectedcode.size(); dds++) {
            cpvselectedlist ditem = cpvselectedcode.get(dds);
            AllCodes = AllCodes + "," + ditem.getSzcode();
        }
        System.err.print("goback");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "MAINCPVFORPIN", AllCodes);
        return "MAINCPVFORSEARCHBACKPIN";
    }

    public String MainCodeForCreateTenderpincncn() {
        String AllCodes = "";
        for (int dds = 0; dds < cpvselectedcode.size(); dds++) {
            cpvselectedlist ditem = cpvselectedcode.get(dds);
            AllCodes = AllCodes + "," + ditem.getSzcode();
        }
        System.err.print("goback");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "MAINCPVFORCN", AllCodes);
        return "MAINCPVFORSEARCHBACKPINCN";
    }
}
