/*
 * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
 *
 * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.device.mgt.core.privacy.dao.impl;

import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory;
import io.entgra.device.mgt.core.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import io.entgra.device.mgt.core.device.mgt.core.privacy.dao.PrivacyComplianceDAO;
import io.entgra.device.mgt.core.device.mgt.core.privacy.dao.PrivacyComplianceDAOException;
import io.entgra.device.mgt.core.device.mgt.core.privacy.impl.DeviceEnrollmentMapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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 PrivacyComplianceDAOImpl implements PrivacyComplianceDAO {

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

    @Override
    public List<DeviceEnrollmentMapping> getDevicesOfUser(String username, int tenantId) throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<DeviceEnrollmentMapping> deviceIds = new ArrayList<>();
        try {
            conn = this.getConnection();
            String sql = "SELECT * FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, username);
            stmt.setInt(2, tenantId);
            rs = stmt.executeQuery();

            while (rs.next()) {
                DeviceEnrollmentMapping mapping = new DeviceEnrollmentMapping();
                mapping.setDeviceId(rs.getInt("DEVICE_ID"));
                mapping.setEnrolmentId(rs.getInt("ID"));
                deviceIds.add(mapping);
            }
            if (deviceIds.isEmpty()) {
                return null;
            }
            return deviceIds;

        } catch (SQLException e) {
            throw new PrivacyComplianceDAOException("Error occurred while retrieving device ids " +
                    "related to the given user.", e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, rs);
        }
    }

    @Override
    public void deleteDevice(int deviceId, int tenantId) throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        try {
            conn = this.getConnection();
            String sql = "DELETE FROM DM_DEVICE WHERE ID = ? AND TENANT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, deviceId);
            stmt.setInt(2, tenantId);
            stmt.executeUpdate();

        } catch (SQLException e) {
            throw new PrivacyComplianceDAOException("Error occurred while deleting the devices", e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
    }

    @Override
    public void deleteDeviceEnrollments(int deviceId, int tenantId) throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        try {
            conn = this.getConnection();
            String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, deviceId);
            stmt.setInt(2, tenantId);
            stmt.executeUpdate();

        } catch (SQLException e) {
            String msg = "Error occurred while deleting the device enrolments";
            log.error(msg, e);
            throw new PrivacyComplianceDAOException(msg, e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
    }

    @Override
    public void deleteDeviceEnrollments(int deviceId, int enrolmentId, int tenantId)
            throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        try {
            conn = this.getConnection();
            String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, deviceId);
            stmt.setInt(2, tenantId);
            stmt.setInt(3, enrolmentId);
            stmt.executeUpdate();

        } catch (SQLException e) {
            throw new PrivacyComplianceDAOException("Error occurred while deleting the device enrolments", e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
    }

    @Override
    public void deleteDeviceDetails(int deviceId, int enrolmentId) throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        try {
            conn = this.getConnection();
            String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, deviceId);
            stmt.setInt(2, enrolmentId);
            stmt.executeUpdate();

        } catch (SQLException e) {
            throw new PrivacyComplianceDAOException("Error occurred while deleting the device details.", e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
    }

    @Override
    public void deleteDeviceApplications(int deviceId, int enrolmentId, int tenantId)
            throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        try {
            conn = this.getConnection();
            String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? " +
                    "AND ENROLMENT_ID = ? AND TENANT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, deviceId);
            stmt.setInt(2, enrolmentId);
            stmt.setInt(3, tenantId);
            stmt.executeUpdate();

        } catch (SQLException e) {
            throw new PrivacyComplianceDAOException("Error occurred while deleting the device applications.", e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
    }

    @Override
    public void deleteDeviceProperties(int deviceId, int enrolmentId, int tenantId)
            throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        try {
            conn = this.getConnection();
            String sql = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, deviceId);
            stmt.setInt(2, enrolmentId);
            stmt.executeUpdate();

        } catch (SQLException e) {
            throw new PrivacyComplianceDAOException("Error occurred while deleting the device information.", e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
    }

    @Override
    public void deleteDeviceLocation(int deviceId, int enrolmentId) throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        try {
            conn = this.getConnection();
            String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, deviceId);
            stmt.setInt(2, enrolmentId);
            stmt.executeUpdate();

        } catch (SQLException e) {
            throw new PrivacyComplianceDAOException("Error occurred while deleting the device location.", e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
    }

    @Override
    public void updateDeviceOperationResponses(int enrolmentId) throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        try {
            conn = this.getConnection();
            String sql = "UPDATE DM_DEVICE_OPERATION_RESPONSE SET OPERATION_RESPONSE = ? WHERE ENROLMENT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setNull(1, java.sql.Types.BLOB);
            stmt.setInt(2, enrolmentId);
            stmt.executeUpdate();

        } catch (SQLException e) {
            throw new PrivacyComplianceDAOException("Error occurred while deleting the device information.", e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
    }


    @Override
    public void deleteDeviceOperationDetails(int enrolmentId) throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        try {
            conn = this.getConnection();
            String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, enrolmentId);
            stmt.executeUpdate();

        } catch (SQLException e) {
            throw new PrivacyComplianceDAOException("Error occurred while deleting the device information.", e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
    }

    @Override
    public void deleteOperationEnrolmentMappings(int enrolmentId) throws PrivacyComplianceDAOException {

        Connection conn;
        PreparedStatement stmt = null;
        try {
            conn = this.getConnection();
            String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, enrolmentId);
            stmt.executeUpdate();

        } catch (SQLException e) {
            throw new PrivacyComplianceDAOException("Error occurred while deleting the device information.", e);
        } finally {
            DeviceManagementDAOUtil.cleanupResources(stmt, null);
        }
    }

    private Connection getConnection() throws SQLException {
        return DeviceManagementDAOFactory.getConnection();
    }
}

