/*
 * 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 org.netbeans.api.wizard.WizardDisplayer;
import org.netbeans.spi.wizard.*;
import java.util.Map;
import java.io.File;
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;

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

    }

    @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);
            }
        }
    }
}
