/*
 * Copyright (c) 2016, 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.ios.util;

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.DeviceManagementException;
import org.wso2.carbon.device.mgt.ios.api.utils.util.Message;
import org.wso2.carbon.device.mgt.ios.apns.exception.APNSException;
import org.wso2.carbon.device.mgt.ios.apns.service.PushNotificationService;
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.ios.exception.ResourceNotFoundException;

import javax.ws.rs.core.MediaType;
import java.util.List;

public class PushNotificationSenderUtils {

    private static final Log log = LogFactory.getLog(PushNotificationSenderUtils.class);

    public void contactAPNS(List<DeviceIdentifier> listDeviceIdentifier, Message message)
            throws APNSException {

        for (DeviceIdentifier deviceIdentifier : listDeviceIdentifier) {
            contactAPNS(deviceIdentifier, message);
        }
    }

    public void contactAPNS(DeviceIdentifier deviceIdentifier, Message message)
            throws APNSException {

        Device device = null;

        try {
            device = IOSServiceUtils.getDeviceManagementService().getDevice(deviceIdentifier);

            if (device == null || device.getDeviceIdentifier() == null || device.getDeviceIdentifier().isEmpty()) {
                message.setErrorMessage("Device not found for requested device id");
                throw new ResourceNotFoundException(message, MediaType.APPLICATION_JSON_TYPE);
            }
            TokenPersistence tokenPersistence = new TokenPersistence();
            PushNotificationService pushNotificationService =
                    IOSServiceUtils.getPushNotificationService();
            pushNotificationService.sendMDMNotification(CertificateConfig.getMDMPem(),
                                                        CertificateConfig.getMDMPassword(),
                                                        CertificateConfig.isProductionMDM(),
                                                        tokenPersistence.getPropertyValue(device.getProperties(),
                                                                                          TokenPersistence.MDM_TOKEN),
                                                        tokenPersistence.getPropertyValue(device.getProperties(),
                                                                                          TokenPersistence.MAGIC_TOKEN)
            );
        } catch (DeviceManagementException e) {
            log.error("Error occurred when retrieving device information", e);
        }
    }
}
