package org.wso2.carbon.device.mgt.ios.core.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.ios.core.exception.TenantConfigurationException;

import java.io.InputStream;

public class CertificateConfig {

    public static final String PRODUCTION_MODE_CODE = "1";
    private static final Log log = LogFactory.getLog(CertificateConfig.class);

    public static InputStream getMDMPem() throws DeviceManagementException {
        TenantUtils tenantUtils;
        try {
            tenantUtils = new TenantUtils();
        } catch (DeviceManagementException e) {
            String errorMsg = "Error occurred while fetching the device management service when accessing tenant " +
                              "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        } catch (TenantConfigurationException e) {
            String errorMsg = "Error occurred while fetching tenant " +
                              "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        }
        InputStream pemData = null;

        if (tenantUtils != null) {
            pemData = tenantUtils.getTenantMDMCertificate();
        }
        return pemData;
    }

    public static String getMDMPassword() throws DeviceManagementException {
        TenantUtils tenantUtils;
        try {
            tenantUtils = new TenantUtils();
        } catch (DeviceManagementException e) {
            String errorMsg = "Error occurred while fetching the device management service when accessing tenant " +
                              "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        } catch (TenantConfigurationException e) {
            String errorMsg = "Error occurred while fetching tenant " +
                              "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        }
        return tenantUtils.getTenantMDMCertificatePassword();
    }

    public static boolean isProductionMDM() throws DeviceManagementException {
        return (PRODUCTION_MODE_CODE.equals(getMDMMode()));
    }

    private static String getMDMMode() throws DeviceManagementException {
        TenantUtils tenantUtils;
        try {
            tenantUtils = new TenantUtils();
        } catch (DeviceManagementException e) {
            String errorMsg = "Error occurred while fetching the device management service when accessing tenant " +
                    "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        } catch (TenantConfigurationException e) {
            String errorMsg = "Error occurred while fetching tenant " +
                    "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        }
        return tenantUtils.getMDMMode();
    }

    public static InputStream getAPNSPem() throws DeviceManagementException {
        TenantUtils tenantUtils;
        try {
            tenantUtils = new TenantUtils();
        } catch (DeviceManagementException e) {
            String errorMsg = "Error occurred while fetching the device management service when accessing tenant " +
                              "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        } catch (TenantConfigurationException e) {
            String errorMsg = "Error occurred while fetching tenant " +
                              "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        }
        InputStream pemData = null;

        if (tenantUtils != null) {
            pemData = tenantUtils.getTenantAPNSCertificate();
        }
        return pemData;
    }

    public static String getAPNSPassword() throws DeviceManagementException {
        TenantUtils tenantUtils;
        try {
            tenantUtils = new TenantUtils();
        } catch (DeviceManagementException e) {
            String errorMsg = "Error occurred while fetching the device management service when accessing tenant " +
                              "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        } catch (TenantConfigurationException e) {
            String errorMsg = "Error occurred while fetching tenant " +
                              "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        }
        return tenantUtils.getTenantAPNSCertificatePassword();
    }

    public static boolean isProductionAPNS() throws DeviceManagementException {
        return (PRODUCTION_MODE_CODE.equals(getAPNSMode()));
    }

    private static String getAPNSMode() throws DeviceManagementException {
        TenantUtils tenantUtils;
        try {
            tenantUtils = new TenantUtils();
        } catch (DeviceManagementException e) {
            String errorMsg = "Error occurred while fetching the device management service when accessing tenant " +
                    "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        } catch (TenantConfigurationException e) {
            String errorMsg = "Error occurred while fetching tenant " +
                    "configurations";
            log.error(errorMsg, e);
            throw new DeviceManagementException(errorMsg, e);
        }
        return tenantUtils.getAPNSMode();
    }

}

