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

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
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.wstender.CallForTenders;
import org.ellectra.wstender.UserAccount;
import org.ellectra.wsviewmytends.ViewMyTendersService;

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

    @WebServiceRef(wsdlLocation = "http://localhost:18181/ViewMyTendersService/ViewMyTendersPort?wsdl")
    private ViewMyTendersService service;
    private String dUserloginsignature;
    private String dUserloginname;
    private String dUserloginOrganization;
    private String dUserloginrole;
    private String dUserloginid;
    private int callid;
    private int mytenderid;
    private UIData tenderdata = null;
    private ArrayList<mytenderlist> tenderlist = new ArrayList();
    
    private UIData pdata = null;
    private UIData tdata = null;
    private UIData fdata = null;
    private ArrayList<prooflist> pdoc = new ArrayList();
    private ArrayList<techlist> tdoc = new ArrayList();
    private ArrayList<finalist> fdoc = new ArrayList();
    
    private String infoText;
    private String thisCallTitle;

    
    public void GetSessionVariables() {
        dUserloginsignature = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINSIGNATURE");
        dUserloginname = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINUSER");
        dUserloginOrganization = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINORGANIZATION");
        dUserloginrole = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINROLE");
        dUserloginid = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINUSERID");
        String selectedCall = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "CALLTENDERID");
        callid = Integer.valueOf(selectedCall).intValue();
        String selectedTender = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "TENDERIDI");
        mytenderid = Integer.valueOf(selectedTender).intValue();
    }
    
    public String viewCall() {
        mytenderlist selectedRow = (mytenderlist) this.getTenderdata().getRowData();
        //ictenderselected = Selected.getSrid();
        callid = selectedRow.getCallid();
        mytenderid = selectedRow.getTid();
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "CALLTENDERID", Integer.toString(callid));
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "TENDERIDI", Integer.toString(mytenderid));

        
        java.lang.String Eo = (java.lang.String) org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "LOGINROLEEO");
        
        //viewCallDetails();
        
        
        if (Eo.equalsIgnoreCase("EO"))
            return "showctvieweo";
        else
            return "NULL";
    }
    
    

    public void populate() {
        tenderlist.clear();
        GetSessionVariables();
        
        try {
            org.ellectra.wsviewmytends.ViewMyTendersService service = new org.ellectra.wsviewmytends.ViewMyTendersService();
            org.ellectra.wsviewmytends.ViewMyTendersPortType port = service.getViewMyTendersPort();
            // TODO initialize WS operation arguments here
            org.ellectra.wsviewmytends.ViewMyTendersRequest part1 = new org.ellectra.wsviewmytends.ViewMyTendersRequest();
            part1.setActorSignature(dUserloginsignature);
            System.err.print("called view my tenders");

            // TODO process result here
            org.ellectra.wsviewmytends.ViewMyTendersResponse result = port.viewMyTendersOperation(part1);
            List<org.ellectra.wsviewmytends.MyTenderType> bpelout = result.getMyTenderDetails();

            for (int i = 0; i < bpelout.size(); i++) {
                System.err.print("get in for loop");

                org.ellectra.wsviewmytends.MyTenderType bpelitem = new org.ellectra.wsviewmytends.MyTenderType();
                bpelitem = bpelout.get(i);

                mytenderlist ditem = new mytenderlist();
                //ditem.setReject(dws.isRejected());
                //ditem.setStatus(dws.isEvaluationStatus());
                ditem.setTid(bpelitem.getTenderId());
                ditem.setCallid(bpelitem.getCallForTenderID());
                ditem.setTitle(bpelitem.getCallTitle());

                String DATE_FORMAT2 = "dd-MM-yyyy";
                DateFormat sdf2 = new SimpleDateFormat(DATE_FORMAT2);
                GregorianCalendar xmlClose = bpelitem.getCallCloseDate().toGregorianCalendar();
                String formattedCloseDate = sdf2.format(xmlClose.getTime());
                ditem.setPdate(formattedCloseDate);

                if (bpelitem.isTenderStatus())
                    ditem.setStatus("Complete");
                else
                    ditem.setStatus("Incomplete");
               
                //bpelitem.getCallOjeuRefNum();                
                tenderlist.add(ditem);
            }//for

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

    }

    public ArrayList<mytenderlist> getTenderlist() {
        populate();
        return tenderlist;
    }

    public void setTenderlist(ArrayList<mytenderlist> tenderlist) {
        this.tenderlist = tenderlist;
    }
    
    public String formatFilename(String filepath){
        int lastSlash = filepath.lastIndexOf("/");
        return filepath.substring(lastSlash + 1, filepath.length());
    }
    
    public void getParameters() {
        String mappedTenderValue = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "TENDERIDI", mappedTenderValue);

        String mappedCallValue = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("callid");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "CALLTENDERID", mappedCallValue);
        
        String mappedCallTitleValue = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("callTitle");
        JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "CALLTENDERTITLE", mappedCallTitleValue);
    }
    
   public String getThisCallTitle() {
        String currentCallTitle = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "CALLTENDERTITLE");
        thisCallTitle = currentCallTitle;
        
        return thisCallTitle;
    }
   
    public void setThisCallTitle(String thisCallTitle) {
        this.thisCallTitle = thisCallTitle;
    }
    
    public String checkTenderCompleteness() {
        getParameters();
        GetSessionVariables();
        
        tdoc.clear();
        fdoc.clear();
        System.err.print("get in check tender completeness");
        try { 
            org.ellectra.wsviewmytends.ViewMyTendersPortType port = service.getViewMyTendersPort();
            // TODO initialize WS operation arguments here
            org.ellectra.wsviewmytends.ViewMyTendersRequest part1 = new org.ellectra.wsviewmytends.ViewMyTendersRequest();
            part1.setActorSignature(dUserloginsignature);
            //System.err.print("signature: " + getDUserloginsignature());
            System.err.print("called view my tenders");
            
            org.ellectra.wsviewmytends.ViewMyTendersResponse result = port.viewMyTendersOperation(part1);
            
            
            if (result.isReturn()) {
                List<org.ellectra.wsviewmytends.MyTenderType> bpelout = result.getMyTenderDetails();
                //System.err.print("users are : "+bpelout.size());
                for (int i = 0; i < bpelout.size(); i++) {
                    System.err.print("get in for loop");
                    org.ellectra.wsviewmytends.MyTenderType bpelitem = new org.ellectra.wsviewmytends.MyTenderType();
                    bpelitem = bpelout.get(i);

                    int callForTendersID = bpelitem.getCallForTenderID();
                    int thisCall = callid;
                    //if tender record found for this call
                    if (callForTendersID == thisCall) {
                        boolean tenderCompleted = bpelitem.isTenderStatus();
                        //since Tech and Fina docs must be submitted together, status will be true at any case -- WRONG
                        if (tenderCompleted) {
                           System.err.print("User has already submitted a proposal. Tech an Fin docs for this call already exist");
                           
                           org.ellectra.wsviewmytends.ProposalType bpeltech = new org.ellectra.wsviewmytends.ProposalType();
                           bpeltech = bpelitem.getTechDetails();
                           
                           techlist techdoc = new techlist();
                           String file = formatFilename(bpeltech.getFilename());
                           techdoc.setFilename(file);
                           techdoc.setId(bpeltech.getDocID());
                           if (bpeltech.getDescription() != null)
                                techdoc.setDescription(bpeltech.getDescription());
                           techdoc.setLanguage(bpeltech.getLanguage());
                           //if (bpelfina.getTimestamp() != null)
                                //techdoc.setTimestamp(bpelfina.getTimestamp());
                           
                           tdoc.add(techdoc);
                           
                           org.ellectra.wsviewmytends.ProposalType bpelfina = new org.ellectra.wsviewmytends.ProposalType();
                           bpelfina = bpelitem.getFinaDetails();
                           
                           finalist finadoc = new finalist();
                           file = formatFilename(bpelfina.getFilename());
                           finadoc.setFilename(file);
                           finadoc.setId(bpelfina.getDocID());
                           if (bpelfina.getDescription() != null)
                               finadoc.setDescription(bpelfina.getDescription());
                           finadoc.setLanguage(bpelfina.getLanguage());
                           //if (bpelfina.getTimestamp() != null)
                               //techdoc.setTimestamp(bpelfina.getTimestamp());
                            
                           fdoc.add(finadoc);
                           
                           infoText = "Your submission of Tender documents for this call has been closed!";
                        }
                        else {
                            infoText = "You have not uploaded yet your Tender Documents for this call.";
                        }
                       //btnCreateTenderDisabled = "true"; 
                       System.err.print("get in join to call");
                       break;
                    }//join to call
                    
                }//for
            }
            else {
               System.err.print("there is no tender generally (i.e. for any call) submitted by this user");
            }
            
            //System.out.println("Result = " + result);
        } catch (Exception ex) {
            // TODO handle custom exceptions here
            ex.printStackTrace();
        }
        return "viewTenderDocs";
    }

    
    public String getDUserloginsignature() {
        return dUserloginsignature;
    }

    public void setDUserloginsignature(String dUserloginsignature) {
        this.dUserloginsignature = dUserloginsignature;
    }

    public String getDUserloginname() {
        return dUserloginname;
    }

    public void setDUserloginname(String dUserloginname) {
        this.dUserloginname = dUserloginname;
    }

    public String getDUserloginOrganization() {
        return dUserloginOrganization;
    }

    public void setDUserloginOrganization(String dUserloginOrganization) {
        this.dUserloginOrganization = dUserloginOrganization;
    }

    public String getDUserloginrole() {
        return dUserloginrole;
    }

    public void setDUserloginrole(String dUserloginrole) {
        this.dUserloginrole = dUserloginrole;
    }

    public String getDUserloginid() {
        return dUserloginid;
    }

    public void setDUserloginid(String dUserloginid) {
        this.dUserloginid = dUserloginid;
    }

    public int getCallid() {
        return callid;
    }

    public void setCallid(int callid) {
        this.callid = callid;
    }

    public int getMytenderid() {
        return mytenderid;
    }

    public void setMytenderid(int mytenderid) {
        this.mytenderid = mytenderid;
    }

    public UIData getTenderdata() {
        return tenderdata;
    }

    public void setTenderdata(UIData tenderdata) {
        this.tenderdata = tenderdata;
    }

    public ArrayList<prooflist> getPdoc() {
        return pdoc;
    }

    public void setPdoc(ArrayList<prooflist> pdoc) {
        this.pdoc = pdoc;
    }

    public ArrayList<techlist> getTdoc() {
        return tdoc;
    }

    public void setTdoc(ArrayList<techlist> tdoc) {
        this.tdoc = tdoc;
    }

    public ArrayList<finalist> getFdoc() {
        return fdoc;
    }

    public void setFdoc(ArrayList<finalist> fdoc) {
        this.fdoc = fdoc;
    }

    public UIData getPdata() {
        return pdata;
    }

    public void setPdata(UIData pdata) {
        this.pdata = pdata;
    }

    public UIData getTdata() {
        return tdata;
    }

    public void setTdata(UIData tdata) {
        this.tdata = tdata;
    }

    public UIData getFdata() {
        return fdata;
    }

    public void setFdata(UIData fdata) {
        this.fdata = fdata;
    }

    public String getInfoText() {
        return infoText;
    }

    public void setInfoText(String infoText) {
        this.infoText = infoText;
    }
    
    

}
