/*
 *
 *  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.config;

import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.mobile.impl.ios.util.IOSUtils;
import org.wso2.carbon.utils.CarbonUtils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;


public class IOSPluginConfigurations {

    private static final String MOBILE_DEVICE_PLUGIN_DIRECTORY = "mobile";
    private static final String IOS_DB_CONFIG_XML_NAME = "ios-dbconfig.xml";
    private static final String DEVICE_MGT_PLUGIN_CONFIGS_DIRECTORY = "device-mgt-plugin-configs";
    public static final String IOS_DB_CONFIG_PATH = CarbonUtils.getEtcCarbonConfigDirPath() + File.separator +
            DEVICE_MGT_PLUGIN_CONFIGS_DIRECTORY + File.separator + MOBILE_DEVICE_PLUGIN_DIRECTORY + File.separator +
            IOS_DB_CONFIG_XML_NAME;

    private IOSDataSourceConfigurations dataSourceConfigurations;
    private static IOSPluginConfigurations iosPluginConfigurations;
    private IOSDataSourceConfigurations iosDataSourceConfigurations;

    public IOSDataSourceConfigurations getDataSourceConfigurations() {
        return dataSourceConfigurations;
    }

    public void setDataSourceConfigurations(IOSDataSourceConfigurations dataSourceConfigurations) {
        this.dataSourceConfigurations = dataSourceConfigurations;
    }

    public static IOSPluginConfigurations getInstance() {
        if (iosPluginConfigurations == null) {
            synchronized (IOSPluginConfigurations.class) {
                if (iosPluginConfigurations == null) {
                    iosPluginConfigurations = new IOSPluginConfigurations();
                }
            }
        }
        return iosPluginConfigurations;
    }

    public synchronized void initConfig() throws DeviceManagementException {
        try {
            File mobileDeviceMgtConfig = new File(IOS_DB_CONFIG_PATH);
            Document doc = IOSUtils.convertToDocument(mobileDeviceMgtConfig);
            JAXBContext iosDeviceMgtContext = JAXBContext.newInstance(IOSDataSourceConfigurations.class);
            Unmarshaller unmarshaller = iosDeviceMgtContext.createUnmarshaller();
            this.iosDataSourceConfigurations = (IOSDataSourceConfigurations) unmarshaller.unmarshal(doc);
        } catch (Exception e) {
            throw new DeviceManagementException(
                    "Error occurred while initializing Mobile Device Management config", e);
        }
    }
    public IOSDataSourceConfigurations getIosDataSourceConfigurations() {
        return iosDataSourceConfigurations;
    }
}
