package org.wso2.carbon.device.mgt.ios.payload.operation;

import org.apache.commons.beanutils.PropertyUtils;
import org.wso2.carbon.device.mgt.ios.payload.beans.PasscodePolicy;
import org.wso2.carbon.device.mgt.ios.payload.beans.PayloadOperation;
import org.wso2.carbon.device.mgt.ios.payload.dto.PayloadOperationWrapper;
import org.wso2.carbon.device.mgt.ios.payload.dto.PayloadProperties;
import org.wso2.carbon.device.mgt.ios.payload.exception.PListException;
import org.wso2.carbon.device.mgt.ios.payload.exception.ProfileConfigurationException;
import org.wso2.carbon.device.mgt.ios.payload.util.OperationUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PasscodeOperations {

    private static final String PASSCODE_POLICY_PAYLOAD_DISPLAY_NAME = "Passcode Policy Configurations";
    private static final String PASSCODE_POLICY_PAYLOAD_DESCRIPTION = "Passcode Policy Configurations for %s";

    public PayloadOperationWrapper createPasscodePolicyPayload(PasscodePolicy passcodePolicy) throws PListException,
            ProfileConfigurationException {

        PayloadProperties payloadProperties = generatePasscodePolicyCommonElements(passcodePolicy,
                OperationUtils.getTenantNameFromOperationID(), OperationUtils.isPayloadRemoveDisallowedForTenant());

        passcodePolicy.validateRequest();

        Map<String, Object> commandMap = passcodePolicy.getCommandMap();

        Field arrField[] = passcodePolicy.getClass().getDeclaredFields();

        for (Field field : arrField) {
            try {
                commandMap.put(field.getName(), PropertyUtils.getProperty(passcodePolicy, field.getName()));
            } catch (NoSuchMethodException e) {
                throw new PListException("Error generating PList for PasscodePolicy payload", e);
            } catch (InvocationTargetException e) {
                throw new PListException("Error generating PList for PasscodePolicy payload", e);
            } catch (IllegalAccessException e) {
                throw new PListException("Error generating PList for PasscodePolicy payload", e);
            }
        }

        List<Object> payloadContent = new ArrayList<Object>();
        payloadContent.add(commandMap);

        PayloadOperationWrapper payloadOperationWrapper = new PayloadOperationWrapper();
        payloadOperationWrapper.setPayload(payloadContent);
        payloadOperationWrapper.setPayloadProperties(payloadProperties);

        return payloadOperationWrapper;
    }

    private PayloadProperties generatePasscodePolicyCommonElements(PasscodePolicy passcodePolicy,
                                                                   String tenantName, boolean payloadRemoveDisallowed) {

        PayloadProperties payloadProperties = new PayloadProperties();
        payloadProperties.setTenantName(tenantName);
        payloadProperties.setPayloadType(PayloadOperation.PayloadType.PASSCODE_POLICY.getPayloadType());
        payloadProperties.setPayloadIdentifier(passcodePolicy.getUUID());
        payloadProperties.setPayloadDisplayName(PASSCODE_POLICY_PAYLOAD_DISPLAY_NAME);
        payloadProperties.setPayloadDescription(String.format(PASSCODE_POLICY_PAYLOAD_DESCRIPTION, tenantName));
        payloadProperties.setPayloadRemoveDisallowed(payloadRemoveDisallowed);

        passcodePolicy.populateCommandPayloadElements(payloadProperties);

        return payloadProperties;
    }
}
