package net.catcert.psa.util;

import java.util.ArrayList;
import java.util.List;

import org.bouncycastle.util.encoders.Base64;
import org.etsi.uri._01903.v1_2_2.ObjectIdentifierType;
import org.etsi.uri._2038.v1_1_1.CommitmentType;

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.CommitmentRolesDocument.CommitmentRoles;
import dss_directsign.profiles._0._1.psa.catcert.CommitmentsAndRolesRSPNSDocument.CommitmentsAndRolesRSPNS;
import dss_directsign.profiles._0._1.psa.catcert.HashesRSPNSDocument.HashesRSPNS;
import dss_directsign.profiles._0._1.psa.catcert.MultiSignatureAtSignatureManagerRSPNSDocument.MultiSignatureAtSignatureManagerRSPNS;
import dss_directsign.profiles._0._1.psa.catcert.SignatureAtSignatureManagerRSPNSDocument.SignatureAtSignatureManagerRSPNS;


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


	/**
	 * Obtenció dels identificadors dels actes de signatura a partir de la resposta de creació de 
	 * multisignatura de PSA.
	 * @param multiResponse resposta de creació de multisignatura
	 * @return llista dels identificadors dels actes de signatura
	 */
	public static List getActOids(MultiSignatureAtSignatureManagerRSPNS multiResponse){
		List actOids = new ArrayList();
		//respostes
		SignatureAtSignatureManagerRSPNS[] signatureAtSignatureManagerRSPNSArray = multiResponse.getSignatureAtSignatureManagerRSPNSArray();
		for(int i=0;i<signatureAtSignatureManagerRSPNSArray.length;i++){
			SignatureAtSignatureManagerRSPNS response = signatureAtSignatureManagerRSPNSArray[i];
			actOids.add(response.getSignatureActOID());
		}
		return actOids;
	}


	/**
	 * Obtenció dels compromisos a partir de la resposta de creació de multisignatura de PSA.
	 * @param multiResponse resposta de creació de multisignatura
	 * @return llistat de compromisos
	 */
	public static List getCommitments(MultiSignatureAtSignatureManagerRSPNS multiResponse){

		//llista de compromisos
		List comms = new ArrayList();

		//respostes
		SignatureAtSignatureManagerRSPNS[] responseList = multiResponse.getSignatureAtSignatureManagerRSPNSArray();
		for(int i=0;i<responseList.length;i++){
			SignatureAtSignatureManagerRSPNS response = responseList[i];
			CommitmentsAndRolesRSPNS commsAndRolesRSPNS = response.getCommitmentsAndRolesRSPNS();
			if(commsAndRolesRSPNS!=null){
				CommitmentRoles[] commonsAndRoles = commsAndRolesRSPNS.getCommitmentRolesArray();
				for(int j=0;j<commonsAndRoles.length;j++){
					CommitmentRoles commRole = commonsAndRoles[j];
					CommitmentType comm = commRole.getCommitment();
					comms.add(comm);
				}
			}
		}
		return comms;  
	}


	/**
	 * Obtenció dels roles a partir de la resposta de creació de multisignatura de PSA.
	 * @param multiResponse  resposta de creació de multisignatura
	 * @return llistat de rols
	 */
	public static List getRoles(MultiSignatureAtSignatureManagerRSPNS multiResponse){

		//llista de roles
		List roles = new ArrayList();

		//respostes
		SignatureAtSignatureManagerRSPNS[] responsesList = multiResponse.getSignatureAtSignatureManagerRSPNSArray();
		for(int i=0;i<responsesList.length;i++){
			SignatureAtSignatureManagerRSPNS response = responsesList[i];
			CommitmentsAndRolesRSPNS commsAndRolesRSPNS = response.getCommitmentsAndRolesRSPNS();
			if(commsAndRolesRSPNS!=null){
				CommitmentRoles[] commsAndRoles = commsAndRolesRSPNS.getCommitmentRolesArray();
				for(int j=0;j<commsAndRoles.length;j++){
					CommitmentRoles commRole = commsAndRoles[j];
					String[] roless = commRole.getRolesArray();
					for(int k=0;k<roless.length;k++){
						roles.add(roless[k]);
					}
				}
			}
		}
		return roles;
	}


	/**
	 * Obtenció dels hashes de les dades a signar, a partir de la resposta d'actualització del ritual 
	 * de multisignatura de PSA.
	 * @param multiResponse resposta d'actualització del ritual de multisignatura
	 * @return llista dels hashes de les dades a signar
	 */
	public static List getHashes(MultiSignatureAtSignatureManagerRSPNS multiResponse){

		//llista de hashes
		List hashes = new ArrayList(); 

		//respostes
		SignatureAtSignatureManagerRSPNS[] responsesList = multiResponse.getSignatureAtSignatureManagerRSPNSArray();
		for(int i=0;i<responsesList.length;i++){
			SignatureAtSignatureManagerRSPNS response = responsesList[i];
			HashesRSPNS hashesResponse = response.getHashesRSPNS();

			if(hashesResponse!=null && hashesResponse.getHashArray()!=null){

				byte[][] hashArray = hashesResponse.getHashArray();
				for(int j=0;j<hashArray.length;j++){
					byte[] hash = hashArray[j];
					hashes.add(hash);
				}
			}
		}
		return hashes;
	}


	/**
	 * Pintar resposta de multisignatura de PSA.
	 * @param multiResponse resposta de multisignatura
	 */
	public static void pintaMultiSignatureAtSignatureManagerRespostes(MultiSignatureAtSignatureManagerRSPNS multiResponse){

		//respostes
		SignatureAtSignatureManagerRSPNS[] responsesList = multiResponse.getSignatureAtSignatureManagerRSPNSArray();
		for(int i=0;i<responsesList.length;i++){
			
			SignatureAtSignatureManagerRSPNS 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);

			CommitmentsAndRolesRSPNS commsAndRolesRSPNS = response.getCommitmentsAndRolesRSPNS();
			if(commsAndRolesRSPNS!=null){
				CommitmentRoles[] commsAndRoles = commsAndRolesRSPNS.getCommitmentRolesArray();
				System.out.println("* Commitments and Roles:");
				for(int j=0;j<commsAndRoles.length;j++){
					CommitmentRoles commRole = commsAndRoles[j];
					CommitmentType comm = commRole.getCommitment();
					ObjectIdentifierType objId = comm.getCommitmentIdentifier();
					System.out.println("Commitment: "+objId.getIdentifier().getStringValue());
					String[] roles = commRole.getRolesArray();
					for(int k=0;k<roles.length;k++){
						System.out.println("Role: "+roles[k]);
					}
				}
			}

			//hashes
			HashesRSPNS hashesResponse = response.getHashesRSPNS();
			if(hashesResponse!=null && hashesResponse.getHashArray()!=null){
				System.out.println("* Hashes:");
				byte[][] hashes = hashesResponse.getHashArray();
				for(int j=0;j<hashes.length;j++){
					System.out.println(Base64.encode(hashes[j]));
				}
			}

		}
	}

}
