/**
 * <copyright> Copyright 2006 by Know-Center, Graz, Austria </copyright>
 * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a
 * joint initiative of the Federal Chancellery Austria and Graz University of
 * Technology.
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * http://www.osor.eu/eupl/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 *
 * This product combines work with different licenses. See the "NOTICE" text
 * file for details on the various modules and licenses.
 * The "NOTICE" text file is part of the distribution. Any derivative works
 * that you distribute must include a readable copy of the "NOTICE" text file.
 */
package at.gv.egiz.pdfas.commandline;

import at.gv.egiz.pdfas.exceptions.ErrorCode;
import at.gv.egiz.pdfas.framework.ConnectorFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import at.knowcenter.wag.egov.egiz.PdfAS;
import at.knowcenter.wag.egov.egiz.PdfASID;
import at.knowcenter.wag.egov.egiz.exceptions.ConnectorException;
import at.knowcenter.wag.egov.egiz.framework.SignatorFactory;
import at.knowcenter.wag.egov.egiz.sig.sigid.HotfixIdFormatter;

/**
 * Encapsulates the logic of choosing the correct connector for the commandline
 * that can later be created using the ConnectorFactory.
 * 
 * @author wprinz
 */
public class CommandlineConnectorChooser
{
  /**
   * The log.
   */
  private static Log log = LogFactory.getLog(CommandlineConnectorChooser.class);

  protected static final String BKU = "bku"; //$NON-NLS-1$

  protected static final String MOA = "moa"; //$NON-NLS-1$
  
  public static boolean needsSigId (String connectorId)
  {
    return !ConnectorFactory.isMOA(connectorId);
  }

  public static String chooseCommandlineConnectorForSign(String connectorType) throws ConnectorException
  {
    log.debug("Choosing Connector for commandline signation...");

    log.debug("connector type = " + connectorType);

    if (connectorType.equals(BKU))
    {
      log.debug("sig_app is BKU ==> DetachedMultipartBKUConnector"); //$NON-NLS-1$

      return ConnectorFactory.DETACHED_MULTIPART_BKU_CONNECTOR;
    }
    if (connectorType.equals(MOA))
    {
      // TODO MOA detached signing is not allowed at the commandline

    	// TR: wurde erweitert um detached MOA-Signaturen
    	//      log.warn("Detached MOA is not supported on the commandline. -> choosing Base64 temporarily.");
    	//      return ConnectorFactory.ENVELOPING_BASE64_MOA_CONNECTOR;
    	
      return ConnectorFactory.DETACHED_LOCREF_MOA_CONNECTOR;
    }

    throw new ConnectorException(300, "Unknown connector type '" + connectorType + "' specified.");
  }

  public static String chooseCommandlineConnectorForVerify(String connectorType, PdfASID sig_kz, String sig_id, String profile) throws ConnectorException
  {
    log.debug("Choosing Connector for Commandline verification...");

    log.debug("connector type = " + connectorType);
    log.debug("sig_kz = " + sig_kz); //$NON-NLS-1$
    log.debug("sig_id = " + sig_id); //$NON-NLS-1$

    if (sig_kz == null)
    {
      log.debug("sig_kz is null -> choosing an old enveloped base64 connector"); //$NON-NLS-1$

      return chooseEnvelopingBase64ConnectorOld(connectorType);
    }

    log.debug("sig_kz is not null -> one of the newer signatures");

    if (sig_kz.getVersion().equals(SignatorFactory.VERSION_1_0_0))
    {
      log.debug("Version is 1.0.0 -> Base64 Signatur (old or Hotfix).");

      if (sig_id == null)
      {
        log.debug("sig_id is null, which means that it is a MOA signature -> choose a hotfix base64 connector (thus it is moa - it doesn't matter).");

        return chooseEnvelopingBase64ConnectorHotfix(connectorType);
      }

      String[] sig_id_parts = sig_id.split("@");
      if (sig_id_parts.length == 2)
      {
        log.debug("sig_id has 2 @-separated parts -> choosing old base64 connector");

        return chooseEnvelopingBase64ConnectorOld(connectorType);
      }
      if (sig_id_parts[0].equals(HotfixIdFormatter.SIG_ID_PREFIX))
      {
        log.debug("sig_id prefix is hotfix -> choosing hotfix base64 connector");

        return chooseEnvelopingBase64ConnectorHotfix(connectorType);
      }

      throw new ConnectorException(300, "The SIG_KZ version is 1.0.0, but SIG_ID is neither MOA nor Old base64 nor Hotfix base64 ???'");
    }
    if (sig_kz.getVersion().equals(SignatorFactory.VERSION_1_1_0))
    {
      log.debug("Version is 1.1.0 -> chose a detached connector.");

      return chooseDetachedMultipartConnector(connectorType);
    }
    if (sig_kz.getVersion().equals(SignatorFactory.VERSION_1_2_0))
    {
      log.debug("Version is 1.2.0 -> chose a detached connector.");

      return chooseDetachedMultipartConnector(connectorType);
    }
    throw new ConnectorException(ErrorCode.UNSUPPORTED_SIGNATURE, "The SIG_KZ version '" + sig_kz.getVersion() + "' is unknown. Please get a new version of PDF-AS. Your version is: " + PdfAS.PDFAS_VERSION);
  }

  protected static String chooseEnvelopingBase64ConnectorOld(String sig_app) throws ConnectorException
  {
    if (sig_app.equals(BKU))
    {
      log.debug("sig_app is BKU ==> OldEnvelopingBase64BKUConnector"); //$NON-NLS-1$

      return ConnectorFactory.OLD_ENVELOPING_BASE64_BKU_CONNECTOR;
    }
    if (sig_app.equals(MOA))
    {
      log.debug("sig_app is MOA ==> EnvelopingBase64MOAConnector"); //$NON-NLS-1$

      return ConnectorFactory.ENVELOPING_BASE64_MOA_CONNECTOR;
    }
    throw new ConnectorException(310, "Unknown sig_app '" + sig_app + "'."); //$NON-NLS-1$ //$NON-NLS-2$

  }

  protected static String chooseEnvelopingBase64ConnectorHotfix(String sig_app) throws ConnectorException
  {
    if (sig_app.equals(BKU))
    {
      log.debug("sig_app is BKU ==> EnvelopingBase64BKUConnector"); //$NON-NLS-1$

      return ConnectorFactory.ENVELOPING_BASE64_BKU_CONNECTOR;
    }
    if (sig_app.equals(MOA))
    {
      log.debug("sig_app is MOA ==> EnvelopingBase64MOAConnector"); //$NON-NLS-1$

      return ConnectorFactory.ENVELOPING_BASE64_MOA_CONNECTOR;
    }
    throw new ConnectorException(310, "Unknown sig_app '" + sig_app + "'."); //$NON-NLS-1$ //$NON-NLS-2$
  }

  protected static String chooseDetachedMultipartConnector(String sig_app) throws ConnectorException
  {
    if (sig_app.equals(BKU))
    {
      log.debug("sig_app is BKU ==> DetachedMultipartBKUConnector"); //$NON-NLS-1$

      return ConnectorFactory.DETACHED_MULTIPART_BKU_CONNECTOR;
    }
    if (sig_app.equals(MOA))
    {
      log.debug("sig_app is MOA ==> DetachedMOAConnector"); //$NON-NLS-1$

//      String msg = "A Detached signature cannot be verified with the MOA connector (yet)."; //$NON-NLS-1$
//      log.error(msg);
//      throw new ConnectorException(ErrorCode.DETACHED_SIGNATURE_NOT_SUPPORTED, msg);
      return ConnectorFactory.DETACHED_LOCREF_MOA_CONNECTOR;
    }
    throw new ConnectorException(310, "Unknown sig_app '" + sig_app + "'."); //$NON-NLS-1$ //$NON-NLS-2$
  }
}