/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2008 L.Meckert - laurent.meckert.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 Lesser General Public License as published by
 * the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.plugin.signerfichier;

import fr.gouv.finances.cp.utils.PropertiesExpansion;
import fr.gouv.finances.dgfip.xemelios.common.Constants;
import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
import org.netbeans.api.wizard.WizardDisplayer;
import org.netbeans.spi.wizard.*;
import java.util.Map;
import java.io.File;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.log4j.Logger;




/**
 *
 * @author laurent.meckert
 */
public class WizardSignerFichier extends AbstractDlgSignerfichier {

    private static final Logger logger = Logger.getLogger(WizardSignerFichier.class);
    private Wizard wizard = null;
    private boolean standaloneMode = false;

    /**
     *
     * @param parent
     * @param modal
     */
    public WizardSignerFichier(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

    }

    public WizardSignerFichier(Properties props) {
        this(null,true);
System.out.println("\n\n\n");
        PropertiesExpansion pp = new PropertiesExpansion();
        for (Enumeration en = props.keys(); en.hasMoreElements();) {
            String k = (String)en.nextElement();
            Object v = props.get(k);
System.out.println(k+"="+v);
            pp.put(k, v);
        }
System.out.println("\n");

        for (Enumeration en = pp.keys(); en.hasMoreElements();) {
            String k = (String)en.nextElement();
            String v = pp.replace(k);
System.out.println(k+"="+v);
            System.setProperty(k, v);
        }
        standaloneMode = true;
    }

    @Override
    protected void preInit() {
        if(standaloneMode) {
            try {
System.out.println("documents-def-dir="+System.getProperty(Constants.SYS_PROP_DOC_DEF_DIR));
                Loader.getDocumentsInfos(System.getProperty(Constants.SYS_PROP_DOC_DEF_DIR));
            } catch(Exception saxEx) {
                // we are in standalone mode, we can quit
                saxEx.printStackTrace();
                System.exit(1);
            }
        }
    }



    public static void main(String[] args) {
        new WizardSignerFichier(null, true).run();
    }


    @Override
    void initOthers() {
//    throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void run() {
        final Class[] pages = new Class[]{
            InputFilePanel.class,
            // CertFilePanel.class,
            //  CertChoicePanel.class,
            CertListPanel.class,
            OutputFilePanel.class
        };
        //initGlassPane();
        initSigner();
        initOthers();
        MyProducer mp = new MyProducer();
        wizard = WizardPage.createWizard("Signature d'un fichier PES", pages, mp);
        WizardDisplayer.showWizard(wizard);
        Actions.getInstance().dispose();

    }

    @Override
    void showSelectedInputFile(File f) {//    throw new UnsupportedOperationException("Not supported yet.");
    }
}
class MyProducer implements WizardPage.WizardResultProducer {

    public Object finish(Map wizardData) throws WizardException {

        String[] items = new String[3];
        items[0] = "Fichier a signer: " + wizardData.get("TextFieldInputFile");
        // items[1] = "Magasin de cles: " + wizardData.get("TextFieldCertFile");
        items[1] = "Certificat utilisé: " + wizardData.get("jListCertificates");
        items[2] = "Fichier signé: " + wizardData.get("TextFieldOutputFile");


        //  return Summary.create(items, null);
        return new MyDeferredWizardResult();
    }

    public boolean cancel(Map settings) {

        return true; // Allow the user to cancel the wizard.
    }


    class MyDeferredWizardResult extends DeferredWizardResult {

        public MyDeferredWizardResult() {
            super(true, true);
        }

        public void start(java.util.Map settings, ResultProgressHandle progress) {
            try {
                Actions.getInstance().doSign();
                String[] items = new String[4];
                items[0] = "Fichier a signer: " + Actions.getInstance().getFileToSign().getAbsolutePath();
                // items[1] = "Magasin de cles: " + Actions.getInstance().getCertFile().getAbsolutePath();
                items[1] = "Certificat utilisé: " + Actions.getInstance().getAliasName();
                items[2] = "Fichier signé: " + Actions.getInstance().getFileToGenerate().getAbsolutePath();
                items[3] = "Résultat: " + Actions.getInstance().getSignStatusString();


                //  return Summary.create(items, null);
//            Actions.getInstance().showStatus();
                //progress.finished(Summary.create(items, null));//Actions.getInstance());
                progress.finished(Summary.create("Fichier correctement signé", null));
            } catch (Exception e) {
                Actions.getInstance().showStatus();
                progress.failed("Echec de la signature", true);
            }
        }
    }
}
