/*
 *   Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *   WSO2 Inc. licenses this file to you under the Apache License,
 *   Version 2.0 (the "License"); you may not use this file except
 *   in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */
package org.wso2.carbon.device.mgt.mobile.impl.ios;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceManager;
import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig;
import org.wso2.carbon.device.mgt.common.InitialOperationConfig;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.ProvisioningConfig;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager;
import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber;
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
import org.wso2.carbon.device.mgt.ios.apns.exception.APNSException;
import org.wso2.carbon.device.mgt.ios.core.publisher.TokenPersistence;
import org.wso2.carbon.device.mgt.ios.core.util.CertificateConfig;
import org.wso2.carbon.device.mgt.mobile.impl.ios.internal.IOSDeviceMgtDataHolder;
import org.wso2.carbon.device.mgt.mobile.impl.ios.util.IOSPluginConstants;

import java.util.HashMap;
import java.util.List;

public class IOSDeviceManagementService implements DeviceManagementService {

    private DeviceManager deviceManager;
    private static final Log log = LogFactory.getLog(IOSDeviceManagementService.class);
    private static final String PROVIDER_TENANT_DOMAIN = "carbon.super";

    public String getType() {
        return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_IOS;
    }

    @Override
    public OperationMonitoringTaskConfig getOperationMonitoringConfig() {
        return null;
    }

    public void init() throws DeviceManagementException {
        this.deviceManager = new IOSDeviceManager();
    }

    public DeviceManager getDeviceManager() {
        return deviceManager;
    }

    public ApplicationManager getApplicationManager() {
        return null;
    }

    public ProvisioningConfig getProvisioningConfig() {
        return new ProvisioningConfig(PROVIDER_TENANT_DOMAIN, true);
    }

    public PushNotificationConfig getPushNotificationConfig() {
        return new PushNotificationConfig(IOSPluginConstants.PUSH_NOTIFICATION_TYPE, false, new HashMap<String, String>
                ());
    }

    @Override
    public PolicyMonitoringManager getPolicyMonitoringManager() {
        return new IOSPolicyMonitoringService();
    }

    @Override
    public InitialOperationConfig getInitialOperationConfig() {
        return null;
    }

    @Override
    public PullNotificationSubscriber getPullNotificationSubscriber() {
        return null;
    }

    @Override
    public DeviceStatusTaskPluginConfig getDeviceStatusTaskPluginConfig() {
        return null;
    }

    public void notifyOperationToDevices(Operation operation, List<DeviceIdentifier> deviceIdentifiers)
            throws DeviceManagementException {

        Device device = null;
        for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) {
            try {
                device = deviceManager.getDevice(deviceIdentifier);
            } catch (DeviceManagementException e) {
                log.error("Error occurred while fetch the device for device identifier:" + deviceIdentifier.getId(), e);
            }

            try {
                IOSDeviceMgtDataHolder.getInstance().getPushNotificationService().sendMDMNotification(
                        CertificateConfig.getMDMPem(), CertificateConfig.getMDMPassword(),
                        CertificateConfig.isProductionMDM(),
                        TokenPersistence.getPropertyValue(device.getProperties(), TokenPersistence.MDM_TOKEN),
                        TokenPersistence.getPropertyValue(device.getProperties(), TokenPersistence.MAGIC_TOKEN));
            } catch (APNSException e) {
                log.error("APNS Exception while sending install app operation:" + operation.toString() + ":device:"
                                  + device.toString());
            }
        }
    }

}
