/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.erar.widget;

import fr.gouv.finances.dgfip.xemelios.common.config.CritereModel;
import fr.gouv.finances.dgfip.xemelios.common.config.ElementModel;
import fr.gouv.finances.dgfip.xemelios.common.config.HiddenModel;
import fr.gouv.finances.dgfip.xemelios.common.config.WidgetModel;
import fr.gouv.finances.cp.xemelios.ui.CriteriaViewer;
import fr.gouv.finances.cp.xemelios.ui.MainWindow;
import fr.gouv.finances.cp.xemelios.ui.SearchWindow;
import fr.gouv.finances.cp.xemelios.ui.resulttable.EtatResultTableModel;
import fr.gouv.finances.cp.xemelios.widgets.AbstractWidget;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author chm
 */
public class DebtorViewer extends AbstractWidget {
    
    /** Creates a new instance of AgentViewer */
    public DebtorViewer(WidgetModel wm, ElementModel em) {
        super(wm,em);
    }

    public void run(EtatResultTableModel etrm,int row) {
        String debNomResultatId = null;
        String debPrenomResultatId = null;
        String debAdr1ResultatId = null;
        String debAdr2ResultatId = null;
        String debAdr3ResultatId = null;
        String debCPResultatId = null;
        String debVilleResultatId = null;

        String debNomCritereId = null;
        String debPrenomCritereId = null;
        String debAdr1CritereId = null;
        String debAdr2CritereId = null;
        String debAdr3CritereId = null;
        String debCPCritereId = null;
        String debVilleCritereId = null;

        String debNomInputId = null;
        String debPrenomInputId = null;
        String debAdr1InputId = null;
        String debAdr2InputId = null;
        String debAdr3InputId = null;
        String debCPInputId = null;
        String debVilleInputId = null;

        String elementCibleId = null;
        try {
            debNomResultatId = getWidgetModel().getParam("deb-nom-res-id").getValue();
            debPrenomResultatId = getWidgetModel().getParam("deb-prenom-res-id").getValue();
            debAdr1ResultatId = getWidgetModel().getParam("deb-adr1-res-id").getValue();
            debAdr2ResultatId = getWidgetModel().getParam("deb-adr2-res-id").getValue();
            debAdr3ResultatId = getWidgetModel().getParam("deb-adr3-res-id").getValue();
            debCPResultatId = getWidgetModel().getParam("deb-CP-res-id").getValue();
            debVilleResultatId = getWidgetModel().getParam("deb-ville-res-id").getValue();

            debNomCritereId = getWidgetModel().getParam("deb-nom-crit-id").getValue();
            debPrenomCritereId = getWidgetModel().getParam("deb-prenom-crit-id").getValue();
            debAdr1CritereId = getWidgetModel().getParam("deb-adr1-crit-id").getValue();
            debAdr2CritereId = getWidgetModel().getParam("deb-adr2-crit-id").getValue();
            debAdr3CritereId = getWidgetModel().getParam("deb-adr3-crit-id").getValue();
            debCPCritereId = getWidgetModel().getParam("deb-CP-crit-id").getValue();
            debVilleCritereId = getWidgetModel().getParam("deb-ville-crit-id").getValue();

            debNomInputId = getWidgetModel().getParam("deb-nom-input-id").getValue();
            debPrenomInputId = getWidgetModel().getParam("deb-prenom-input-id").getValue();
            debAdr1InputId = getWidgetModel().getParam("deb-adr1-input-id").getValue();
            debAdr2InputId = getWidgetModel().getParam("deb-adr2-input-id").getValue();
            debAdr3InputId = getWidgetModel().getParam("deb-adr3-input-id").getValue();
            debCPInputId = getWidgetModel().getParam("deb-CP-input-id").getValue();
            debVilleInputId = getWidgetModel().getParam("deb-ville-input-id").getValue();

            elementCibleId = getWidgetModel().getParam("elementId").getValue();
        } catch(NullPointerException npEx) {}
        StringBuilder sb = new StringBuilder();

        if(debNomResultatId==null) sb.append("deb-nom-res-id param has null value or is missing\n");
        if(debPrenomResultatId==null) sb.append("deb-prenom-res-id param has null value or is missing\n");
        if(debAdr1ResultatId==null) sb.append("deb-adr1-res-id param has null value or is missing\n");
        if(debAdr2ResultatId==null) sb.append("deb-adr2-res-id param has null value or is missing\n");
        if(debAdr3ResultatId==null) sb.append("deb-adr3-res-id param has null value or is missing\n");
        if(debCPResultatId==null) sb.append("deb-CP-res-id param has null value or is missing\n");
        if(debVilleResultatId==null) sb.append("deb-ville-res-id param has null value or is missing\n");

        if(debNomCritereId==null) sb.append("deb-nom-crit-id param has null value or is missing\n");
        if(debPrenomCritereId==null) sb.append("deb-prenom-crit-id param has null value or is missing\n");
        if(debAdr1CritereId==null) sb.append("deb-adr1-crit-id param has null value or is missing\n");
        if(debAdr2CritereId==null) sb.append("deb-adr2-crit-id param has null value or is missing\n");
        if(debAdr3CritereId==null) sb.append("deb-adr3-crit-id param has null value or is missing\n");
        if(debCPCritereId==null) sb.append("deb-CP-crit-id param has null value or is missing\n");
        if(debVilleCritereId==null) sb.append("deb-ville-crit-id param has null value or is missing\n");

        if(debNomInputId==null) sb.append("deb-nom-input-id param has null value or is missing\n");
        if(debPrenomInputId==null) sb.append("deb-prenom-input-id param has null value or is missing\n");
        if(debAdr1InputId==null) sb.append("deb-adr1-input-id param has null value or is missing\n");
        if(debAdr2InputId==null) sb.append("deb-adr2-input-id param has null value or is missing\n");
        if(debAdr3InputId==null) sb.append("deb-adr3-input-id param has null value or is missing\n");
        if(debCPInputId==null) sb.append("deb-CP-input-id param has null value or is missing\n");
        if(debVilleInputId==null) sb.append("deb-ville-input-id param has null value or is missing\n");

        if(elementCibleId==null) sb.append("elementId param has null value or is missing\n");
        SearchWindow sw = null;
        if(sb.length()==0) {
            int nbChamps = etrm.getListeChamps().size();
            Vector<HiddenModel> champs = etrm.getListeResultat().getHiddens();
            ElementModel ligneERARElement = getElementModel().getParent().getElementById(elementCibleId);
            sw = MainWindow.instance.displaySearchWindow(ligneERARElement);
            sw.setCollectivite(etrm.getCollectivite());
            sw.setBudget(etrm.getBudget());
            sw.clearAll();
            sb.append(addCriteria2(sw,debNomCritereId,debNomInputId,debNomResultatId,champs,nbChamps, etrm, row,ligneERARElement));
            sb.append(addCriteria2(sw,debPrenomCritereId,debPrenomInputId,debPrenomResultatId,champs,nbChamps, etrm, row,ligneERARElement));
            sb.append(addCriteria2(sw,debAdr1CritereId,debAdr1InputId,debAdr1ResultatId,champs,nbChamps, etrm, row,ligneERARElement));
            sb.append(addCriteria2(sw,debAdr2CritereId,debAdr2InputId,debAdr2ResultatId,champs,nbChamps, etrm, row,ligneERARElement));
            sb.append(addCriteria2(sw,debAdr3CritereId,debAdr3InputId,debAdr3ResultatId,champs,nbChamps, etrm, row,ligneERARElement));
            sb.append(addCriteria2(sw,debCPCritereId,debCPInputId,debCPResultatId,champs,nbChamps, etrm, row,ligneERARElement));
            sb.append(addCriteria2(sw,debVilleCritereId,debVilleInputId,debVilleResultatId,champs,nbChamps, etrm, row,ligneERARElement));
        }
        if(sb.length()>0) {
            JOptionPane.showMessageDialog(MainWindow.instance,sb.toString(),"Erreur",JOptionPane.ERROR_MESSAGE);
        } else {
            sw.setVisible(true);
            sw.search();
        }
    }

    protected String addCriteria2(SearchWindow sw,String critere, String input, String result, Vector<HiddenModel> champs,int nbChamps, EtatResultTableModel etrm, int row, ElementModel em) {
        String ret = null;
        try {
            addCriteria(sw,critere,input,result,champs,nbChamps,etrm,row,em);
        } catch(Exception ex) {
            ret = ex.getMessage();
        }
        if(ret!=null) ret = ret.concat("\n");
        else ret="";
        return ret;
    }    
    protected void addCriteria(SearchWindow sw,String critere, String input, String result, Vector<HiddenModel> champs,int nbChamps, EtatResultTableModel etrm, int row, ElementModel em) throws Exception {
        int pos = -1;
        for(int i=0;i<champs.size();i++) {
            if(champs.get(i).getName().equals(result)) {
                pos = i;
                break;
            }
        }
        if(pos== -1) throw new Exception("hidden "+result+" was not found in lrm");
        String value = (String)etrm.getValueAt(row,pos+nbChamps);   // les hidden sont a la fin
        if(value!=null && value.length()>0) {
            CritereModel cm = em.getCritere(critere);
            CriteriaViewer cv = sw.addLine();
            cv.changeCritereModel(cm);
            String opName = input.replaceFirst("VALEUR","OPERATEUR");
            cv.setInputValue(opName,"xem:StringEquals");
            cv.setInputValue(input,value);
        }
    }
}
