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

import org.wso2.carbon.device.mgt.ios.payload.beans.AirPlay;
import org.wso2.carbon.device.mgt.ios.payload.beans.AirPlayCredentials;
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.HashMap;
import java.util.List;
import java.util.Map;

public class AirOperations {

    private static final String PAYLOAD_DISPLAY_NAME = "AirPlay Configurations";
    private static final String PAYLOAD_DESCRIPTION = "AirPlay Configurations for %s";
    private static final String PASSWORDS = "Passwords";
    private static final String PASSWORD = "Password";
    private static final String DEVICE_NAME = "DeviceName";
    private static final String DEVICE_ID = "DeviceID";
    private static final String WHITELIST = "Whitelist";

    public PayloadOperationWrapper createAirPlayPayload(AirPlay airPlay) throws PListException,
            ProfileConfigurationException {

        PayloadProperties payloadProperties = generateCommonElements(airPlay,
                OperationUtils.getTenantNameFromOperationID(), OperationUtils.isPayloadRemoveDisallowedForTenant());

        airPlay.validateRequest();

        Map<String, Object> airPlayMap = airPlay.getCommandMap();
        List<Map<String, Object>> airPlayCredentialList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> destinationList = new ArrayList<Map<String, Object>>();

        for (AirPlayCredentials airPlayCredentials : airPlay.getAirPlayCredentials()) {
            Map<String, Object> airPlayEntry = new HashMap<String, Object>();
            airPlayEntry.put(DEVICE_NAME, airPlayCredentials.getDeviceName());
            airPlayEntry.put(PASSWORD, airPlayCredentials.getPassword());
            airPlayCredentialList.add(airPlayEntry);
        }

        airPlayMap.put(PASSWORDS, airPlayCredentialList);

        for (String destination : airPlay.getAirPlayDestinations()) {
            Map<String, Object> destinationEntry = new HashMap<String, Object>();
            destinationEntry.put(DEVICE_ID, destination);
            destinationList.add(destinationEntry);
        }

        airPlayMap.put(WHITELIST, destinationList);

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

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

        return payloadOperationWrapper;
    }

    private PayloadProperties generateCommonElements(AirPlay airPlay, String tenantName,
                                        boolean payloadRemoveDisallowed) {

        PayloadProperties payloadProperties = new PayloadProperties();
        payloadProperties.setTenantName(tenantName);
        payloadProperties.setPayloadType(PayloadOperation.PayloadType.AIR_PLAY.getPayloadType());
        payloadProperties.setPayloadIdentifier(airPlay.getUUID());
        payloadProperties.setPayloadDisplayName(PAYLOAD_DISPLAY_NAME);
        payloadProperties.setPayloadDescription(String.format(PAYLOAD_DESCRIPTION, tenantName));
        payloadProperties.setPayloadRemoveDisallowed(payloadRemoveDisallowed);

        airPlay.populateCommandPayloadElements(payloadProperties);

        return payloadProperties;
    }

}
