/*
 * 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.widgets.paye;

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 AgentViewer extends AbstractWidget {
    
    /** Creates a new instance of AgentViewer */
    public AgentViewer(WidgetModel wm, ElementModel em) {
        super(wm,em);
    }

    public void run(EtatResultTableModel etrm,int row) {
        String matriculeResultatId = null;
        String matriculeCritereId = null;
        String matriculeInputId = null;
        String elementCibleId = null;
        try {
            matriculeResultatId = getWidgetModel().getParam("matricule-res-id").getValue();
            matriculeCritereId = getWidgetModel().getParam("matricule-crit-id").getValue();
            matriculeInputId = getWidgetModel().getParam("matricule-input-id").getValue();
            elementCibleId = getWidgetModel().getParam("elementId").getValue();
        } catch(NullPointerException npEx) {}
        StringBuilder sb = new StringBuilder();
        if(matriculeResultatId==null) sb.append("matricule-res-id param has null value or is missing\n");
        if(matriculeCritereId==null) sb.append("matricule-crit-id param has null value or is missing\n");
        if(matriculeInputId==null) sb.append("matricule-input-id param has null value or is missing\n");
        if(elementCibleId==null) sb.append("elementId param has null value or is missing\n");
        if(sb.length()==0) {
            Vector<HiddenModel> champs = etrm.getHiddens();
            int pos = -1;
            for(int i=0;i<champs.size();i++) {
                if(champs.get(i).getName().equals(matriculeResultatId)) {
                    pos = i;
                    break;
                }
            }
            if(pos== -1) sb.append("champ "+matriculeResultatId+" was not found in lrm");
            else {
                String matricule = (String)etrm.getValueAt(row,pos+etrm.getListeChamps().size());
                //JOptionPane.showMessageDialog(MainWindow.instance,"Matricule="+matricule);
                ElementModel payeIndivElement = getElementModel().getParent().getElementById(elementCibleId);
                CritereModel cm = payeIndivElement.getCritere(matriculeCritereId);
                
                SearchWindow sw = MainWindow.instance.displaySearchWindow(payeIndivElement);
                sw.setCollectivite(etrm.getCollectivite());
                sw.setBudget(etrm.getBudget());
                sw.clearAll();
                CriteriaViewer cv = sw.addLine();
                cv.changeCritereModel(cm);
                String opName = matriculeInputId.replaceFirst("VALEUR","OPERATEUR");
                cv.setInputValue(opName,"xem:StringEquals");
                cv.setInputValue(matriculeInputId,matricule);
                sw.setVisible(true);
                sw.search();
            }
        }
        if(sb.length()>0) {
            JOptionPane.showMessageDialog(MainWindow.instance,sb.toString(),"Erreur",JOptionPane.ERROR_MESSAGE);
        }
    }
    
}
