/*
 * 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.mobile.impl.ios.dao.impl;

import org.wso2.carbon.device.mgt.ios.payload.beans.DeviceProfile;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.IOSDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.util.MobileDeviceManagementDAOUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProfileDAO {

    public boolean addProfile(DeviceProfile deviceProfile) throws MobileDeviceManagementDAOException {
        PreparedStatement stmt = null;
        boolean status = false;
        try {
            Connection conn = IOSDAOFactory.getConnection();
            String sql = "INSERT INTO IOS_DEVICE_PROFILE(OPERATION_ID, OPERATION_CODE, PROFILE_ID, " +
                    "DEVICE_IDENTIFICATION, PROPERTIES) VALUES (?, ?, ?, ?, ?)";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, deviceProfile.getOperationId());
            stmt.setString(2, deviceProfile.getOperationCode());
            stmt.setString(3, deviceProfile.getProfileId());
            stmt.setString(4, deviceProfile.getDeviceId());
            stmt.setString(5, deviceProfile.getProperties());
            stmt.executeUpdate();
            status = true;
        } catch (SQLException e) {
            throw new MobileDeviceManagementDAOException("Error occurred while adding the profile", e);
        } finally {
            MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
        return status;
    }

    public DeviceProfile getProfile(String deviceId, String url) throws MobileDeviceManagementDAOException {

        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<String> deviceTypes = new ArrayList<>();
        try {
            Connection conn = IOSDAOFactory.getConnection();
            String sql = "SELECT OPERATION_ID, OPERATION_CODE, PROFILE_ID, DEVICE_IDENTIFICATION," +
                    " PROPERTIES FROM IOS_DEVICE_PROFILE WHERE DEVICE_IDENTIFICATION = ? AND PROPERTIES = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, deviceId);
            stmt.setString(2, url);
            rs = stmt.executeQuery();

            DeviceProfile deviceProfile = new DeviceProfile();
            while (rs.next()) {
                deviceProfile.setOperationCode(rs.getString("OPERATION_CODE"));
                deviceProfile.setProfileId(rs.getString("PROFILE_ID"));
                deviceProfile.setProperties(rs.getString("PROPERTIES"));
                deviceProfile.setOperationId(rs.getString("OPERATION_ID"));
                deviceProfile.setDeviceId(rs.getString("DEVICE_IDENTIFICATION"));
            }
            return deviceProfile;
        } catch (SQLException e) {
            throw new MobileDeviceManagementDAOException("Error occurred while fetching the registered device types", e);
        } finally {
            MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
        }
    }
}