package com.enactor.training.paymentService.service.actions.gcash;

import com.enactor.commonUI.CommonUIDataTypesBase;
import com.enactor.core.application.process.ApplicationProcessException;
import com.enactor.core.application.process.IApplicationProcessData;
import com.enactor.core.application.process.IApplicationProcessOutcome;
import com.enactor.core.json.serializer.IJsonSerializerUtil;
import com.enactor.core.json.serializer.JsonParseException;
import com.enactor.core.json.serializer.JsonSerializerUtil;
import com.enactor.core.signOn.IUserPrincipal;
import com.enactor.core.utilities.Logger;
import com.enactor.coreUI.actions.IUIAction;
import com.enactor.coreUI.actions.UIActionFunctions;
import com.enactor.coreUI.annotations.Input;
import com.enactor.coreUI.annotations.Inputs;
import com.enactor.coreUI.annotations.Outcomes;
import com.enactor.coreUI.annotations.Outputs;
import com.enactor.coreUI.processes.CoreUIOutcomes;
import com.enactor.coreUI.processes.UIProcessException;
import com.enactor.training.paymentService.service.messages.gcash.IResponseMessage;
import com.enactor.training.paymentService.service.messages.gcash.ResponseMessage;


@Inputs({
	@Input(name = ResponseMessage.ENTITY_DATA_TYPE_NAME, type = IResponseMessage.class, required = true),
	@Input(name = CommonUIDataTypesBase.USER_PRINCIPAL_NAME, type = IUserPrincipal.class, required = true)
})
@Outputs({})
@Outcomes({
	CoreUIOutcomes.FAIL_OUTCOME_NAME,
	CoreUIOutcomes.SUCCESS_OUTCOME_NAME
})
public class VerifySignatureAction implements IUIAction {

	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(VerifySignatureAction.class.getName());

	@Override
	public IApplicationProcessOutcome execute(IApplicationProcessData inputData, IApplicationProcessData outputData)
			throws UIProcessException, ApplicationProcessException {
		
		IResponseMessage responseMessage = UIActionFunctions.getRequiredDataItem(inputData, ResponseMessage.ENTITY_DATA_TYPE);
		
		IUserPrincipal userPrincipal = UIActionFunctions.getRequiredDataItem(inputData, CommonUIDataTypesBase.USER_PRINCIPAL);
		
		String keyPath = userPrincipal.getCredentialProperties().get(GCashFunctions.CREDENTIAL_PROPERTY_PUBLIC_KEY);
		
		IJsonSerializerUtil jsonSerializerUtil = JsonSerializerUtil.getInstance(com.enactor.core.json.serializer.JsonSerializerUtil.Format.ENACTOR);

		boolean isValid = false;
		
		try {
			String payloadJson = jsonSerializerUtil.getJSON(responseMessage.getResponse());
			
			if (logger.isDebugEnabled()) {
				logger.log(Logger.LOG_DEBUG, "Response: \n\r" + payloadJson + "\n\r");
				logger.log(Logger.LOG_DEBUG, "Signature:\n\r" + responseMessage.getSignature() + "\n\r");
			}
			
			
		} catch (JsonParseException e) {
			throw new ApplicationProcessException(e);
		}
		
		isValid = responseMessage.getResponse().getBody().getResultInfo().getResultCode().equals("SUCCESS");
		
		return (isValid) ? CoreUIOutcomes.SUCCESS_OUTCOME : CoreUIOutcomes.FAIL_OUTCOME;
	}
	
	protected boolean validateResponse(String payloadJson, IResponseMessage responseMessage, String keyPath) {
		try {
			return GCashFunctions.validateSignatureString(payloadJson, responseMessage.getSignature(), keyPath);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}
