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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.xml.ws.WebServiceRef;
import org.ellectra.gui.utils.JSFUtils;
import org.ellectra.wsviewtenderers.ViewTenderersService;

/**
 *
 * @author tdi
 */
public class tenderees {
    @WebServiceRef(wsdlLocation = "http://localhost:18181/ViewTenderersService/ViewTenderersPort?wsdl")
    private ViewTenderersService service;

    private ArrayList<tendereeslist> tenders = new ArrayList();
    private UIData utenders = null;
    private ArrayList<tendereeslist> accepted = new ArrayList();
    private UIData uaccepted = null;
    private ArrayList<tendereeslist> rejected = new ArrayList();
    private UIData urejected = null;
    
    private boolean closeDisabled = true;
    String myselectedphase;
    
    private String thisCallTitle;
    
    private boolean btnEvalTechRendered;
    private boolean btnEvalFinRendered;
    private boolean btnEvalRankRendered;
    
    private boolean txtProofRendered;
    private boolean txtTechRendered;
    private boolean txtFinRendered;
    
    private String callWinner;
    
    public boolean isTxtProofRendered() {
        return txtProofRendered;
    }

    public void setTxtProofRendered(boolean txtProofRendered) {
        this.txtProofRendered = txtProofRendered;
    }

    public boolean isTxtTechRendered() {
        return txtTechRendered;
    }

    public void setTxtTechRendered(boolean txtTechRendered) {
        this.txtTechRendered = txtTechRendered;
    }

    public boolean isTxtFinRendered() {
        return txtFinRendered;
    }

    public void setTxtFinRendered(boolean txtFinRendered) {
        this.txtFinRendered = txtFinRendered;
    }

    
    
//    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 populate(String phase) {
        
        tenders.clear();
        accepted.clear();
        rejected.clear();
        
        String dUserloginsignature = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINSIGNATURE");
        String szTemp = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "CALLTENDERID");
        int mycalloftender = Integer.valueOf(szTemp).intValue();
        
        try { // Call Web Service Operation

            org.ellectra.wsviewtenderers.ViewTenderersService service = new org.ellectra.wsviewtenderers.ViewTenderersService();
            org.ellectra.wsviewtenderers.ViewTenderersPortType port = service.getViewTenderersPort();
            // TODO initialize WS operation arguments here
            org.ellectra.wsviewtenderers.ViewTenderersRequest part1 = new org.ellectra.wsviewtenderers.ViewTenderersRequest();
            part1.setActorSignature(dUserloginsignature);
            part1.setCallForTenderID(mycalloftender);
            part1.setEvaluationPhase(phase);
            
            org.ellectra.wsviewtenderers.ViewTenderersResponse result = port.viewTenderersOperation(part1);
            List<org.ellectra.wsviewtenderers.ViewTenderType> dd = result.getTender();

            if (result.isPhaseEvalCanClose()) {
                setCloseDisabled(false);
            } else {
                setCloseDisabled(true);
            }
            for (int iddd = 0; iddd < dd.size(); iddd++) {

                org.ellectra.wsviewtenderers.ViewTenderType dws = new org.ellectra.wsviewtenderers.ViewTenderType();
                dws = dd.get(iddd);
                
                System.err.print("TENDEREES: Adding: " + dws.getTenderer() + ", tenders size: " + dd.size() + ", current: " + iddd);
                //<tendereeslist> tenders
                tendereeslist ditem = new tendereeslist();
                ditem.setReject(dws.isRejected());
                ditem.setStatus(dws.isEvaluationStatus());
                ditem.setTender(dws.getTenderer());
                ditem.setTid(dws.getTenderID());
                
                if (dws.isEvaluationStatus())
                    ditem.setStatustext("Complete");
                else 
                    ditem.setStatustext("Incomplete");
                
                if (dws.getRejectedStage() != null){
                    if (dws.getRejectedStage().equals("PHASE_I"))
                        ditem.setRejstage("Proof");
                    else if (dws.getRejectedStage().equals("PHASE_II"))
                        ditem.setRejstage("Technical");
                    else if (dws.getRejectedStage().equals("PHASE_III"))
                        ditem.setRejstage("Financial");
                }
                
                //rejected or accept
                if (dws.isRejected()){
                    ditem.setStatustextstatus("Rejected - "+ ditem.getRejstage());
                    rejected.add(ditem);
                }
                else
                {
                    ditem.setStatustextstatus("Accepted");
                    System.err.println("Score is: " + dws.getScore());
                    if (dws.getScore() != null)
                        ditem.setTotalscore(dws.getScore());
                    else ditem.setTotalscore(0);
                    accepted.add(ditem);
                    
                    decrypttech decryt= new decrypttech();
                    if (phase.equalsIgnoreCase("PHASE_I"))
                        decryt.DecryptTechDocs(String.valueOf(dws.getTenderID()), 1);
                    if (phase.equalsIgnoreCase("PHASE_II"))
                        decryt.DecryptTechDocs(String.valueOf(dws.getTenderID()), 2);
                }    
                tenders.add(ditem);
            //ditem
            }
            if (phase.equalsIgnoreCase("PHASE_III"))
                Collections.sort(accepted, sortScores);
            
            
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }

    }
    
    private static final Comparator sortScores = new Comparator() {
        public int compare(Object o1, Object o2) {
            Double d1 = ((tendereeslist)o1).getTotalscore();
            Double d2 = ((tendereeslist)o2).getTotalscore();
            return d2.compareTo(d1);
        }
    };
      
    
    public void getParameters() {
        String dValue = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "CALLTENDERID", dValue);
        
        String mappedCallTitleValue = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("callTitle");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "CALLTENDERTITLE", mappedCallTitleValue);
    }
            
    public String ShowAllTenders1() {
        getParameters();
        myselectedphase = "PHASE_I";
        txtProofRendered = true;
        setBtnEvalTechRendered(true);
        setBtnEvalFinRendered(false);
        setBtnEvalRankRendered(false);
        return "evaluatePhase1";
    }
    
    public String ShowAllTenders2() {
        getParameters();
        myselectedphase = "PHASE_II";
        txtProofRendered = true;
        txtTechRendered = true;
        setBtnEvalTechRendered(false);
        setBtnEvalFinRendered(true);
        setBtnEvalRankRendered(false);
        return "evaluatePhase2";
    }
    
    public String ShowAllTenders3() {
        getParameters();
        myselectedphase = "PHASE_III";
        txtProofRendered = true;
        txtTechRendered = true;
        txtFinRendered = true;
        setBtnEvalTechRendered(false);
        setBtnEvalFinRendered(false);
        //setBtnEvalRankRendered(true);
        return "evaluatePhase3";
    }
    
    public String ShowRanking() {
        getParameters();
        myselectedphase = "PHASE_III";
        //txtProofRendered = true;
        //txtTechRendered = true;
        //txtFinRendered = true;
        //setBtnEvalTechRendered(false);
        //setBtnEvalFinRendered(false);
        //setBtnEvalRankRendered(true);
        populate(myselectedphase);
        return "viewranking";
    }
    
//    public String ShowStatus1() {
//        getParameters();
//        myselectedphase = "PHASE_I";
//        return "viewaccepted";
//    }
//   
//    public String ShowStatus2() {
//        getParameters();
//        myselectedphase = "PHASE_II";
//        return "viewaccepted";
//    }
//
//    public String ShowStatus3() {
//        getParameters();
//        myselectedphase = "PHASE_III";
//        return "viewranking";
//    }

    public ArrayList<tendereeslist> getTenders() {
        populate(myselectedphase);
        return tenders;
    }

    public void setTenders(ArrayList<tendereeslist> tenders) {
        this.tenders = tenders;
    }

    public UIData getUtenders() {
        return utenders;
    }

    public void setUtenders(UIData utenders) {
        this.utenders = utenders;
    }

    public boolean isCloseDisabled() {
        return closeDisabled;
    }

    public void setCloseDisabled(boolean closeDisabled) {
        this.closeDisabled = closeDisabled;
    }

    public ArrayList<tendereeslist> getAccepted() {
        Collections.sort(accepted, sortScores);
        if (!accepted.isEmpty()) {
            tendereeslist winner = accepted.get(0);
            callWinner = winner.getTender();
        }
        
        return accepted;
    }

    public void setAccepted(ArrayList<tendereeslist> accepted) {
        this.accepted = accepted;
    }

    public UIData getUaccepted() {
        return uaccepted;
    }

    public void setUaccepted(UIData uaccepted) {
        this.uaccepted = uaccepted;
    }

    public ArrayList<tendereeslist> getRejected() {
        return rejected;
    }

    public void setRejected(ArrayList<tendereeslist> rejected) {
        this.rejected = rejected;
    }

    public UIData getUrejected() {
        return urejected;
    }

    public void setUrejected(UIData urejected) {
        this.urejected = urejected;
    }

    public String getThisCallTitle() {
        String currentCallTitle = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "CALLTENDERTITLE");
        thisCallTitle = currentCallTitle;
        
        return thisCallTitle;
    }

    public void setThisCallTitle(String thisCallTitle) {
        this.thisCallTitle = thisCallTitle;
    }

    public boolean isBtnEvalTechRendered() {
        return btnEvalTechRendered;
    }

    public void setBtnEvalTechRendered(boolean btnEvalTechRendered) {
        this.btnEvalTechRendered = btnEvalTechRendered;
    }

    public boolean isBtnEvalFinRendered() {
        return btnEvalFinRendered;
    }

    public void setBtnEvalFinRendered(boolean btnEvalFinRendered) {
        this.btnEvalFinRendered = btnEvalFinRendered;
    }

    public boolean isBtnEvalRankRendered() {
        return btnEvalRankRendered;
    }

    public void setBtnEvalRankRendered(boolean btnEvalRankRendered) {
        this.btnEvalRankRendered = btnEvalRankRendered;
    }

    public String getCallWinner() {
        return callWinner;
    }

    public void setCallWinner(String callWinner) {
        this.callWinner = callWinner;
    }
    
    
}
