package net.catcert.psa.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

import schema.core._0._1.dss.tc.names.oasis.InternationalStringType;
import schema.core._0._1.dss.tc.names.oasis.ResultDocument.Result;

import dss_directsign.profiles._0._1.psa.catcert.CompleteSignatureRSPNSDocument.CompleteSignatureRSPNS;
import dss_directsign.profiles._0._1.psa.catcert.DocumentHashDocument.DocumentHash;
import dss_directsign.profiles._0._1.psa.catcert.DocumentOrDocumentHashType;
import dss_directsign.profiles._0._1.psa.catcert.DocumentType;
import dss_directsign.profiles._0._1.psa.catcert.MultiCompleteSignatureRSPNSDocument.MultiCompleteSignatureRSPNS;
import dss_directsign.profiles._0._1.psa.catcert.SignedDocumentIdsDocument.SignedDocumentIds;


/**
 * Classe per tractament de la resposta de tipus CompleteSignatureResponse.
 * @author suport tècnic CATCert
 */
public class CompleteSignatureResponseUtils {

	/**
	 * Pintar resposta de completat de multisignatura de PSA.
	 * @param multiResponse resposta de completat de multisignatura
	 */
	public static void pintaMultiCompleteSignatureRespostes(MultiCompleteSignatureRSPNS multiResponse){

		//respostes
		CompleteSignatureRSPNS[] responsesList = multiResponse.getCompleteSignatureRSPNSArray();
		for(int i=0;i<responsesList.length;i++){

			CompleteSignatureRSPNS response =responsesList[i];

			//resultat de l'operació sol·licitada
			System.out.println("* Result:");
			Result result = response.getResult();
			String major = result.getResultMajor();
			String minor = result.getResultMinor();
			InternationalStringType message = result.getResultMessage();
			System.out.println("Major: "+major);
			System.out.println("Minor: "+minor);
			if(message!=null)
				System.out.println("Message: "+message.getStringValue());

			//identificador del context de signatura iniciat, necessari per les següents peticions associades al mateix context
			String procedureContextOid = response.getProcedureContextOID();
			System.out.println("* Procedure Context ID: "+procedureContextOid);

			//identificador del context de signatura
			String signatureActOid = response.getSignatureActOID();
			System.out.println("* Signature Act ID: "+signatureActOid);

			//document signat
			DocumentOrDocumentHashType outDocs = response.getOutputDocuments();
			if(outDocs!=null){
				DocumentType[] outDocsList = outDocs.getDocumentArray();
				DocumentHash[] outDocsHashList = outDocs.getDocumentHashArray();
				if(outDocsList!=null){
					for(int j=0;j<outDocsList.length;j++){
						DocumentType doc = outDocsList[j];
						System.out.println("Doc ID: "+doc.getID());
						System.out.println("Doc filename: "+doc.getFilename());
					}
				} else if(outDocsHashList!=null){
					for(int j=0;j<outDocsHashList.length;j++){
						DocumentHash docHash = outDocsHashList[j];
						System.out.println("Doc Hash ID: "+docHash.getID());
						System.out.println("Doc Hash digest: "+docHash.getDigestValue());
						System.out.println("Doc Hash algorithm: "+docHash.getDigestMethod().getAlgorithm());
					}
				}

			}

			SignedDocumentIds signDocIds = response.getSignedDocumentIds();
			if(signDocIds!=null){
				String[] signDocIdsList = signDocIds.getSignedDocumentIdArray();
				for(int j=0;j<signDocIdsList.length;j++){
					System.out.println("Doc ID: "+signDocIdsList[j]);
				}
			}

		}

	}


	/**
	 * Guardar els documents signats a disc.
	 * @param multiResponse resposta de completat de multisignatura
	 * @throws IOException
	 */
	public static void saveSignedDocs(MultiCompleteSignatureRSPNS multiResponse) throws IOException{
		//respostes
		CompleteSignatureRSPNS[] responsesList = multiResponse.getCompleteSignatureRSPNSArray();

		for(int i=0;i<responsesList.length;i++){

			CompleteSignatureRSPNS response = responsesList[i];

			//document signat
			DocumentOrDocumentHashType outDocs = response.getOutputDocuments();
			if(outDocs!=null){
				DocumentType[] outDocsList = outDocs.getDocumentArray();
				DocumentHash[] outDocsHashList = outDocs.getDocumentHashArray();
				System.out.println("Documents signats:");
				if(outDocsList!=null){
					for(int j=0;j<outDocsList.length;j++){
					DocumentType doc = outDocsList[j];
						String filePath = "C:/temp/"+doc.getFilename() + ".tmp";
						System.out.println(filePath);
						Utils.writeDataToFile(filePath, doc.getData());
					}

				} else if(outDocsHashList!=null){
					for(int j=0;j<outDocsHashList.length;j++){
						DocumentHash docHash = outDocsHashList[j];
						byte[] data = docHash.getDigestValue();
						String filePath = "C:/temp/"+docHash.getID()+".tmp";
						System.out.println(filePath);
						Utils.writeDataToFile(filePath, data);
					}
				}

			}

		}
	}

}
