package org.ellectra.gui.managebeans;

import java.util.ArrayList;
import java.util.List;
import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.xml.ws.WebServiceRef;
import org.ellectra.closeevaluation.CloseEvaluationService;
import org.ellectra.gui.utils.JSFUtils;
import org.ellectra.wsevaluation.EvaluateTenderService;
import org.ellectra.wsproofvalues.ManageCfpValuesService;

public class evaluateproofdoc {

    @WebServiceRef(wsdlLocation = "http://localhost:18181/CloseEvaluationService/CloseEvaluationPort?wsdl")
    private CloseEvaluationService service_2;
    @WebServiceRef(wsdlLocation = "http://localhost:18181/EvaluateTenderService/EvaluateTenderPort?wsdl")
    private EvaluateTenderService service_1;
    @WebServiceRef(wsdlLocation = "http://localhost:18181/ManageCfpValuesService/ManageCfpValuesPort?wsdl")
    private ManageCfpValuesService service;
    String tender;
    String c1;
    String c2;
    String c3;
    String c4;
    String c5;
    String c6;
    String dUserloginname;
    String dUserloginsignature;
    String dUserloginOrganization;
    String dUserloginrole;
    int dUserloginid;
    int mytenderid;
    int mycalloftender;
    private UIData psData = null;
    private UIData efcData = null;
    private UIData tcData = null;
    private UIData otherData = null;
    private ArrayList<cfparticipation> psConditions = new ArrayList();
    private ArrayList<cfparticipation> efcConditions = new ArrayList();
    private ArrayList<cfparticipation> tcConditions = new ArrayList();
    private ArrayList<cfparticipation> otherConditions = new ArrayList();
    
    private String thisTenderer;
    

    public String getTender() {
        return tender;
    }

    public void setTender(String tender) {
        this.tender = tender;
    }

    public String getC1() {
        return c1;
    }

    public void setC1(String c1) {
        this.c1 = c1;
    }

    public String getC2() {
        return c2;
    }

    public void setC2(String c2) {
        this.c2 = c2;
    }

    public String getC3() {
        return c3;
    }

    public void setC3(String c3) {
        this.c3 = c3;
    }

    public String getC4() {
        return c4;
    }

    public void setC4(String c4) {
        this.c4 = c4;
    }

    public String getC5() {
        return c5;
    }

    public void setC5(String c5) {
        this.c5 = c5;
    }

    public String getC6() {
        return c6;
    }

    public void setC6(String c6) {
        this.c6 = c6;
    }

    public void GetSessionVariables() {

        dUserloginname = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINUSER");
        dUserloginsignature = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINSIGNATURE");
        dUserloginOrganization = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINORGANIZATION");
        dUserloginrole = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINROLE");
        String szTemp = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINUSERID");
        dUserloginid = Integer.valueOf(szTemp).intValue();
        szTemp = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "TENDERIDI");
        mytenderid = Integer.valueOf(szTemp).intValue();
        szTemp = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "CALLTENDERID");
        mycalloftender = Integer.valueOf(szTemp).intValue();
        //
        //

    }

    public void populatelist() {
        GetSessionVariables();
        System.err.print("starting populate list for proof docs evaluation");
        psConditions.clear();
        efcConditions.clear();
        tcConditions.clear();
        otherConditions.clear();
        try { // Call Web Service Operation

            org.ellectra.wsproofvalues.ManageCfpValuesPortType port = service.getManageCfpValuesPort();
            // TODO initialize WS operation arguments here
            org.ellectra.wsproofvalues.ManageCfpValuesRequest part1 = new org.ellectra.wsproofvalues.ManageCfpValuesRequest();

            part1.setAction("FR20_CfpValues_Read");
            part1.setActorSignature(dUserloginsignature);
            part1.setCallForTenderID(mycalloftender);
            part1.setTenderID(mytenderid);
            System.err.print("Sig - call - tender: " + dUserloginsignature + " " + mycalloftender + " " + mytenderid);
            org.ellectra.wsproofvalues.ManageCfpValuesResponse result = port.manageCfpValuesOperation(part1);
            List<org.ellectra.wsproofvalues.CfpEvaluationType> ddd = result.getCfpCriterion();

            System.err.print("Cfps size: " + ddd.size());
            for (int t = 0; t < ddd.size(); t++) {
                org.ellectra.wsproofvalues.CfpEvaluationType ditem = new org.ellectra.wsproofvalues.CfpEvaluationType();
                ditem = ddd.get(t);

                System.err.print("type: " + ditem.getConditionType());
                if (ditem.getConditionType().equalsIgnoreCase("PERSONAL_SITUATION")) {
                    System.err.print("ps");
                    cfparticipation dd1 = new cfparticipation();
                    dd1.setCondition(ditem.getCondition());
                    dd1.setId(ditem.getCfpID());
                    dd1.setNumber(ditem.getCfpID());
                    if (result.isValuesExist()) {//!ditem.getScore().equalsIgnoreCase("NULL")) {

                        if (ditem.getScore().equalsIgnoreCase("No")) {
                            dd1.setScore("1");
                        } else if (ditem.getScore().equalsIgnoreCase("Yes")) {
                            dd1.setScore("2");
                        } else if (ditem.getScore().equalsIgnoreCase("Null")) {
                            dd1.setScore("0");
                        }
                        dd1.setValueid(ditem.getCfpValueID());
                    } else {
                        dd1.setScore("0");
                    }
                    psConditions.add(dd1);
                }
                if (ditem.getConditionType().equalsIgnoreCase("ECONOMIC_FINANCIAL_CAPACITY")) {
                    cfparticipation dd1 = new cfparticipation();
                    dd1.setCondition(ditem.getCondition());
                    dd1.setId(ditem.getCfpID());
                    dd1.setNumber(ditem.getCfpID());
                    if (result.isValuesExist()) {
                        if (ditem.getScore().equalsIgnoreCase("No")) {
                            dd1.setScore("1");
                        } else if (ditem.getScore().equalsIgnoreCase("Yes")) {
                            dd1.setScore("2");
                        } else if (ditem.getScore().equalsIgnoreCase("Null")) {
                            dd1.setScore("0");
                        }
                        dd1.setValueid(ditem.getCfpValueID());
                    } else {
                        dd1.setScore("0");
                    }
                    efcConditions.add(dd1);
                }

                if (ditem.getConditionType().equalsIgnoreCase("TECHNICAL_CAPACITY")) {
                    cfparticipation dd1 = new cfparticipation();
                    dd1.setCondition(ditem.getCondition());
                    dd1.setId(ditem.getCfpID());
                    dd1.setNumber(ditem.getCfpID());
                    if (result.isValuesExist()) {
                        if (ditem.getScore().equalsIgnoreCase("No")) {
                            dd1.setScore("1");
                        } else if (ditem.getScore().equalsIgnoreCase("Yes")) {
                            dd1.setScore("2");
                        } else if (ditem.getScore().equalsIgnoreCase("Null")) {
                            dd1.setScore("0");
                        }
                        dd1.setValueid(ditem.getCfpValueID());
                    } else {
                        dd1.setScore("0");
                    }
                    tcConditions.add(dd1);
                }

                if (ditem.getConditionType().equalsIgnoreCase("OTHER")) {
                    cfparticipation dd1 = new cfparticipation();
                    dd1.setCondition(ditem.getCondition());
                    dd1.setId(ditem.getCfpID());
                    dd1.setNumber(ditem.getCfpID());
                    if (result.isValuesExist()) {
                        if (ditem.getScore().equalsIgnoreCase("No")) {
                            dd1.setScore("1");
                        } else if (ditem.getScore().equalsIgnoreCase("Yes")) {
                            dd1.setScore("2");
                        } else if (ditem.getScore().equalsIgnoreCase("Null")) {
                            dd1.setScore("0");
                        }
                        dd1.setValueid(ditem.getCfpValueID());
                    } else {
                        dd1.setScore("0");
                    }
                    otherConditions.add(dd1);
                }


            }

        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }


    }

    public String Evaluateproffdocsfortender() {
        String dValue = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ddddevaluatetenderidproof");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "TENDERIDI", dValue);
        
        String mappedTenderOrgValue = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("tenderOrg");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "TENDERORG", mappedTenderOrgValue);
        
        populatelist();
        return "evaluateproofdoc";
    }
    
   public String getThisTenderer() {
        String currentTenderer = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "TENDERORG");
        thisTenderer = currentTenderer;
        
        return thisTenderer;
    }

    public void setThisTenderer(String thisTenderer) {
        this.thisTenderer = thisTenderer;
    }

    /**
     * Use this method to store values for each Condition for Participation evaluated for the given tender of given call.
     * @return
     */
    public String performEvaluation() {
        GetSessionVariables();

        try { // Call Web Service Operation

            org.ellectra.wsevaluation.EvaluateTenderPortType port = service_1.getEvaluateTenderPort();
            // TODO initialize WS operation arguments here
            org.ellectra.wsevaluation.EvaluateTenderRequest part1 = new org.ellectra.wsevaluation.EvaluateTenderRequest();
            part1.setAction("notused");
            part1.setActorSignature(dUserloginsignature);
            part1.setCallForTenderID(mycalloftender);
            part1.setTenderID(mytenderid);
            part1.setEvaluationPhase("PHASE_I");

            List<org.ellectra.wsevaluation.CfpEvaluationType> dlocalist = part1.getCfpCriterion();
            setEvaluationRequest(psConditions, dlocalist);
            setEvaluationRequest(efcConditions, dlocalist);
            setEvaluationRequest(tcConditions, dlocalist);
            setEvaluationRequest(otherConditions, dlocalist);


            // TODO process result here
            org.ellectra.wsevaluation.EvaluateTenderResponse result = port.evaluateTenderOperation(part1);
            System.out.println("EVALUATEPROOFDOC: BPEL Returned message: " + result.getReturnMessage());
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }

        return "ok";
    }

    /**
     * Internal procedure
     * @param conditions
     * @param dlocalist
     */
    public void setEvaluationRequest(ArrayList<cfparticipation> conditions, List<org.ellectra.wsevaluation.CfpEvaluationType> dlocalist) {

        for (int i = 0; i < conditions.size(); i++) {
            cfparticipation ditem = conditions.get(i);
            org.ellectra.wsevaluation.CfpEvaluationType dd2 = new org.ellectra.wsevaluation.CfpEvaluationType();

            dd2.setCfpID(ditem.getId());

            String valueid = String.valueOf(ditem.getValueid());
            if (valueid != null) {
                dd2.setCfpValueID(ditem.getValueid());
            }
            if (ditem.getScore().equals("0")) {
                dd2.setScore("NULL");
            } else if (ditem.getScore().equals("1")) {
                dd2.setScore("NO");
            } else if (ditem.getScore().equals("2")) {
                dd2.setScore("YES");
            }
            dlocalist.add(dd2);
        }
    }

    public String closeEvaluation() {
        GetSessionVariables();
        
        String returnto = "";
        try { // Call Web Service Operation

            org.ellectra.closeevaluation.CloseEvaluationPortType port = service_2.getCloseEvaluationPort();
            // TODO initialize WS operation arguments here
            org.ellectra.closeevaluation.CloseEvaluationRequest part1 = new org.ellectra.closeevaluation.CloseEvaluationRequest();
            part1.setActorSignature(dUserloginsignature);//notUsed
            part1.setCallForTenderID(mycalloftender);//notUsed
            part1.setPhaseToClose("PHASE_I");
            
            //ok
            //ok
            // TODO process result here
            org.ellectra.closeevaluation.CloseEvaluationResponse result = port.closeEvaluationOperation(part1);
            if (result.isReturn()) {
                returnto="viewaccepted";
            }
            else
                returnto="samepage";
            
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }
        return returnto;
    }

    public UIData getPsData() {
        return psData;
    }

    public void setPsData(UIData psData) {
        this.psData = psData;
    }

    public UIData getEfcData() {
        return efcData;
    }

    public void setEfcData(UIData efcData) {
        this.efcData = efcData;
    }

    public UIData getTcData() {
        return tcData;
    }

    public void setTcData(UIData tcData) {
        this.tcData = tcData;
    }

    public UIData getOtherData() {
        return otherData;
    }

    public void setOtherData(UIData otherData) {
        this.otherData = otherData;
    }

    public ArrayList<cfparticipation> getPsConditions() {
        return psConditions;
    }

    public void setPsConditions(ArrayList<cfparticipation> psConditions) {
        this.psConditions = psConditions;
    }

    public ArrayList<cfparticipation> getEfcConditions() {
        return efcConditions;
    }

    public void setEfcConditions(ArrayList<cfparticipation> efcConditions) {
        this.efcConditions = efcConditions;
    }

    public ArrayList<cfparticipation> getTcConditions() {
        return tcConditions;
    }

    public void setTcConditions(ArrayList<cfparticipation> tcConditions) {
        this.tcConditions = tcConditions;
    }

    public ArrayList<cfparticipation> getOtherConditions() {
        return otherConditions;
    }

    public void setOtherConditions(ArrayList<cfparticipation> otherConditions) {
        this.otherConditions = otherConditions;
    }

 
    
    
}
