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

import org.wso2.carbon.device.mgt.ios.payload.beans.CalDAV;
import org.wso2.carbon.device.mgt.ios.payload.beans.CalendarSubscription;
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.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CalendarOperations {

    private static final String CAL_DAV_ACCOUNT_DESCRIPTION = "CalDAVAccountDescription";
    private static final String CAL_DAV_HOST_NAME = "CalDAVHostName";
    private static final String CAL_DAV_USERNAME = "CalDAVUsername";
    private static final String CAL_DAV_PASSWORD = "CalDAVPassword";
    private static final String CAL_DAV_USE_SSL = "CalDAVUseSSL";
    private static final String CAL_DAV_PORT = "CalDAVPort";
    private static final String CAL_DAV_PRINCIPAL_URL = "CalDAVPrincipalURL";
    private static final String SUB_CAL_ACCOUNT_DESCRIPTION = "SubCalAccountDescription";
    private static final String SUB_CAL_ACCOUNT_HOST_NAME = "SubCalAccountHostName";
    private static final String SUB_CAL_ACCOUNT_USERNAME = "SubCalAccountUsername";
    private static final String SUB_CAL_ACCOUNT_PASSWORD = "SubCalAccountPassword";
    private static final String SUB_CAL_ACCOUNT_USE_SSL = "SubCalAccountUseSSL";
    private static final String CAL_DAV_PAYLOAD_DISPLAY_NAME = "CalDAV Configurations";
    private static final String CAL_DAV_PAYLOAD_DESCRIPTION = "CalDAV Configurations for %s";
    private static final String SUB_CAL_PAYLOAD_DISPLAY_NAME = "Calendar Subscription Configurations";
    private static final String SUB_CAL_PAYLOAD_DESCRIPTION = "Calendar Subscription Configurations for %s";

    public PayloadOperationWrapper createCalDAVPayload(CalDAV calDAV) throws PListException,
            ProfileConfigurationException {

        PayloadProperties payloadProperties = generateCalDAVCommonElements(calDAV,
                OperationUtils.getTenantNameFromOperationID(), OperationUtils.isPayloadRemoveDisallowedForTenant());

        calDAV.validateRequest();

        Map<String, Object> commandMap = calDAV.getCommandMap();
        commandMap.put(CAL_DAV_ACCOUNT_DESCRIPTION, calDAV.getAccountDescription());
        commandMap.put(CAL_DAV_HOST_NAME, calDAV.getHostName());
        commandMap.put(CAL_DAV_USERNAME, calDAV.getUsername());
        commandMap.put(CAL_DAV_PASSWORD, calDAV.getPassword());
        commandMap.put(CAL_DAV_USE_SSL, calDAV.getUseSSL());
        commandMap.put(CAL_DAV_PORT, calDAV.getPort());
        commandMap.put(CAL_DAV_PRINCIPAL_URL, calDAV.getPrincipalURL());

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

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

        return payloadOperationWrapper;
    }

    public PayloadOperationWrapper createCalenderSubscriptionPayload(CalendarSubscription calendarSubscription)
            throws PListException, ProfileConfigurationException {

        PayloadProperties payloadProperties = generateCalendarSubscriptionCommonElements(calendarSubscription,
                OperationUtils.getTenantNameFromOperationID(), OperationUtils.isPayloadRemoveDisallowedForTenant());

        calendarSubscription.validateRequest();

        Map<String, Object> commandMap = calendarSubscription.getCommandMap();
        commandMap.put(SUB_CAL_ACCOUNT_DESCRIPTION, calendarSubscription.getAccountDescription());
        commandMap.put(SUB_CAL_ACCOUNT_HOST_NAME, calendarSubscription.getHostName());
        commandMap.put(SUB_CAL_ACCOUNT_USERNAME, calendarSubscription.getUsername());
        commandMap.put(SUB_CAL_ACCOUNT_PASSWORD, calendarSubscription.getPassword());
        commandMap.put(SUB_CAL_ACCOUNT_USE_SSL, calendarSubscription.getUseSSL());

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

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

        return payloadOperationWrapper;
    }

    private PayloadProperties generateCalDAVCommonElements(CalDAV calDAV, String tenantName,
                                              boolean payloadRemoveDisallowed) {

        PayloadProperties payloadProperties = new PayloadProperties();
        payloadProperties.setTenantName(tenantName);
        payloadProperties.setPayloadType(PayloadOperation.PayloadType.CALDAV.getPayloadType());
        payloadProperties.setPayloadIdentifier(calDAV.getUUID());
        payloadProperties.setPayloadDisplayName(CAL_DAV_PAYLOAD_DISPLAY_NAME);
        payloadProperties.setPayloadDescription(String.format(CAL_DAV_PAYLOAD_DESCRIPTION, tenantName));
        payloadProperties.setPayloadRemoveDisallowed(payloadRemoveDisallowed);

        calDAV.populateCommandPayloadElements(payloadProperties);

        return payloadProperties;
    }

    private PayloadProperties generateCalendarSubscriptionCommonElements(CalendarSubscription calendarSubscription,
                                                            String tenantName, boolean payloadRemoveDisallowed) {

        PayloadProperties payloadProperties = new PayloadProperties();
        payloadProperties.setTenantName(tenantName);
        payloadProperties.setPayloadType(PayloadOperation.PayloadType.CALENDAR_SUBSCRIPTION.getPayloadType());
        payloadProperties.setPayloadIdentifier(calendarSubscription.getUUID());
        payloadProperties.setPayloadDisplayName(SUB_CAL_PAYLOAD_DISPLAY_NAME);
        payloadProperties.setPayloadDescription(String.format(SUB_CAL_PAYLOAD_DESCRIPTION, tenantName));
        payloadProperties.setPayloadRemoveDisallowed(payloadRemoveDisallowed);

        calendarSubscription.populateCommandPayloadElements(payloadProperties);

        return payloadProperties;
    }

}
