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

import org.wso2.carbon.device.mgt.ios.payload.beans.GenericDeviceOperation;
import org.wso2.carbon.device.mgt.ios.payload.beans.InstalledRestrictions;
import org.wso2.carbon.device.mgt.ios.payload.beans.RemoveProfile;
import org.wso2.carbon.device.mgt.ios.payload.exception.PListException;
import org.wso2.carbon.device.mgt.ios.payload.plist.PListGenerator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeviceOperations {

    private static final String DEVICE_LOCK = "DeviceLock";
    private static final String DEVICE_INFO = "DeviceInformation";
    private static final String INSTALLED_APPLICATION_LIST = "InstalledApplicationList";
    private static final String CLEAR_PASSCODE = "ClearPasscode";
    private static final String PROFILE_LIST = "ProfileList";
    private static final String REMOVE_PROFILE = "RemoveProfile";
    private static final String RESTRICTIONS = "Restrictions";
    private static final String QUERIES = "Queries";
    private static final String PROFILE_RESTRICTIONS = "ProfileRestrictions";
    private static final String ERASE_DEVICE = "EraseDevice";

    public enum QueryParameter {

        UDID,
        Languages,
        Locales,
        DeviceID,
        OrganizationInfo,
        LastCloudBackupDate,
        iTunesStoreAccountIsActive,
        iTunesStoreAccountHash,
        DeviceName,
        OSVersion,
        BuildVersion,
        ModelName,
        Model,
        ProductName,
        SerialNumber,
        DeviceCapacity,
        AvailableDeviceCapacity,
        BatteryLevel,
        CellularTechnology,
        IMEI,
        MEID,
        ModemFirmwareVersion,
        IsSupervised,
        IsDeviceLocatorServiceEnabled,
        IsActivationLockEnabled,
        IsDoNotDisturbInEffect,
        EASDeviceIdentifier,
        IsCloudBackupEnabled,
        ICCID,
        BluetoothMAC,
        WiFiMAC,
        EthernetMACs,
        CurrentCarrierNetwork,
        SIMCarrierNetwork,
        SubscriberCarrierNetwork,
        CarrierSettingsVersion,
        PhoneNumber,
        VoiceRoamingEnabled,
        DataRoamingEnabled,
        IsRoaming,
        PersonalHotspotEnabled,
        SubscriberMCC,
        SubscriberMNC,
        CurrentMCC,
        CurrentMNC;

        public static String[] getNames() {
            QueryParameter[] queryParameter = values();
            String[] names = new String[queryParameter.length];

            int i = 0;
            for (QueryParameter queryParameters : queryParameter) {
                names[i++] = queryParameters.name();
            }

            return names;
        }

    }

    public String createDeviceLockPayload(GenericDeviceOperation genericDeviceOperation)
            throws PListException {

        Map<String, Object> commandMap = new HashMap<String, Object>();
        commandMap.put(OperationPayloadConstants.REQUEST_TYPE, DEVICE_LOCK);

        Map<String, Object> payloadMap = new HashMap<String, Object>();
        payloadMap.put(OperationPayloadConstants.COMMAND, commandMap);
        payloadMap.put(OperationPayloadConstants.COMMAND_UUID, genericDeviceOperation.getUUID());

        return new PListGenerator().generatePayload(payloadMap);
    }

    public String createApplicationListPayload(GenericDeviceOperation genericDeviceOperation)
            throws PListException {

        Map<String, Object> commandMap = new HashMap<String, Object>();
        commandMap.put(OperationPayloadConstants.REQUEST_TYPE, INSTALLED_APPLICATION_LIST);

        Map<String, Object> payloadMap = new HashMap<String, Object>();
        payloadMap.put(OperationPayloadConstants.COMMAND, commandMap);
        payloadMap.put(OperationPayloadConstants.COMMAND_UUID, genericDeviceOperation.getUUID());

        return new PListGenerator().generatePayload(payloadMap);
    }

    public String createProfileListPayload(GenericDeviceOperation genericDeviceOperation)
            throws PListException {

        Map<String, Object> commandMap = new HashMap<String, Object>();
        commandMap.put(OperationPayloadConstants.REQUEST_TYPE, PROFILE_LIST);

        Map<String, Object> payloadMap = new HashMap<String, Object>();
        payloadMap.put(OperationPayloadConstants.COMMAND, commandMap);
        payloadMap.put(OperationPayloadConstants.COMMAND_UUID, genericDeviceOperation.getUUID());

        return new PListGenerator().generatePayload(payloadMap);
    }

    public String createRemoveProfilePayload(RemoveProfile removeProfile)
            throws PListException {

        Map<String, Object> commandMap = new HashMap<String, Object>();
        commandMap.put(OperationPayloadConstants.REQUEST_TYPE, REMOVE_PROFILE);
        commandMap.put(OperationPayloadConstants.IDENTIFIER, removeProfile.getIdentifier());

        Map<String, Object> payloadMap = new HashMap<String, Object>();
        payloadMap.put(OperationPayloadConstants.COMMAND, commandMap);
        payloadMap.put(OperationPayloadConstants.COMMAND_UUID, removeProfile.getUUID());

        return new PListGenerator().generatePayload(payloadMap);
    }

    public String createClearPasscodePayload(GenericDeviceOperation genericDeviceOperation, String unlockToken)
            throws PListException {

        Map<String, Object> commandMap = new HashMap<String, Object>();
        commandMap.put(OperationPayloadConstants.REQUEST_TYPE, CLEAR_PASSCODE);
        commandMap.put(OperationPayloadConstants.UNLOCK_TOKEN, unlockToken.getBytes());

        Map<String, Object> payloadMap = new HashMap<String, Object>();
        payloadMap.put(OperationPayloadConstants.COMMAND, commandMap);
        payloadMap.put(OperationPayloadConstants.COMMAND_UUID, genericDeviceOperation.getUUID());

        return new PListGenerator().generatePayload(payloadMap);
    }

    public String createDeviceInformationPayload(GenericDeviceOperation genericDeviceOperation)
            throws PListException {

        Map<String, Object> commandMap = new HashMap<String, Object>();
        commandMap.put(OperationPayloadConstants.REQUEST_TYPE, DEVICE_INFO);

        List<String> queryParameterList = new ArrayList<String>();
        Collections.addAll(queryParameterList, QueryParameter.getNames());

        commandMap.put(QUERIES, queryParameterList);

        Map<String, Object> payloadMap = new HashMap<String, Object>();
        payloadMap.put(OperationPayloadConstants.COMMAND, commandMap);
        payloadMap.put(OperationPayloadConstants.COMMAND_UUID, genericDeviceOperation.getUUID());

        return new PListGenerator().generatePayload(payloadMap);
    }

    public String createRequestRestrictionPayload(InstalledRestrictions installedRestrictions) throws PListException {

        Map<String, Object> commandMap = new HashMap<String, Object>();
        commandMap.put(OperationPayloadConstants.REQUEST_TYPE, RESTRICTIONS);
        commandMap.put(PROFILE_RESTRICTIONS, installedRestrictions.getProfileRestrictions());

        Map<String, Object> payloadMap = new HashMap<String, Object>();
        payloadMap.put(OperationPayloadConstants.COMMAND, commandMap);
        payloadMap.put(OperationPayloadConstants.COMMAND_UUID, installedRestrictions.getUUID());

        return new PListGenerator().generatePayload(payloadMap);
    }

    public String createDataWipe(GenericDeviceOperation genericDeviceOperation)
            throws PListException {

        Map<String, Object> commandMap = new HashMap<String, Object>();
        commandMap.put(OperationPayloadConstants.REQUEST_TYPE, ERASE_DEVICE);

        Map<String, Object> payloadMap = new HashMap<String, Object>();
        payloadMap.put(OperationPayloadConstants.COMMAND, commandMap);
        payloadMap.put(OperationPayloadConstants.COMMAND_UUID, genericDeviceOperation.getUUID());

        return new PListGenerator().generatePayload(payloadMap);
    }
}
