package org.wso2.carbon.device.mgt.mobile.impl.ios.dao.impl.profile;

import org.apache.commons.lang3.BooleanUtils;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
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 org.wso2.carbon.device.mgt.mobile.impl.ios.dto.DEPDevice;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dto.DEPProfile;

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 OracleProfileDAO extends AbstractProfileDAO {

    @Override
    public List<DEPProfile> getDEPProfiles(PaginationRequest request) throws MobileDeviceManagementDAOException {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<DEPProfile> depProfiles = new ArrayList<>();
        try {
            Connection conn = IOSDAOFactory.getConnection();
            String sql = "SELECT ID, UUID, PROFILE_NAME, PROFILE_CONTENT, TENANT_DOMAIN FROM IOS_DEP_PROFILE WHERE " +
                    "TENANT_DOMAIN = ? ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain());
            stmt.setInt(2, request.getStartIndex());
            stmt.setInt(3, request.getRowCount());
            rs = stmt.executeQuery();
            while (rs.next()) {
                DEPProfile depProfile = new DEPProfile();
                depProfile.setId(rs.getString("ID"));
                depProfile.setUUID(rs.getString("UUID"));
                depProfile.setProfileName(rs.getString("PROFILE_NAME"));
                depProfile.setProfileContent(rs.getString("PROFILE_CONTENT"));
                depProfile.setTenantDomain(rs.getString("TENANT_DOMAIN"));
                depProfiles.add(depProfile);
            }
        } catch (SQLException e) {
            throw new MobileDeviceManagementDAOException("Error occurred while fetching DEP profiles.", e);
        } finally {
            MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
        }
        return depProfiles;
    }

    @Override
    public List<DEPDevice> getDEPDevices(PaginationRequest request) throws MobileDeviceManagementDAOException {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<DEPDevice> depDevices = new ArrayList<>();
        try {
            Connection conn = IOSDAOFactory.getConnection();
            String sql = "SELECT D.ID, D.SERIAL, D.USERNAME, D.TENANT_DOMAIN, D.STATUS, D.DEP_PROFILE_ID, " +
                    "P.PROFILE_NAME, D.PROFILE_ASSIGN_TIME, D.PROFILE_PUSH_TIME, D.DEVICE_ASSIGNED_TIME," +
                    "D.DEVICE_ASSIGNED_BY, D.NEED_BASIC_AUTH, D.OS, D.DEVICE_FAMILY, D.DEVICE_MODEL, " +
                    "D.DEVICE_MODEL, D.DESCRIPTION, D.COLOR, D.DEVICE_IDENTIFIER, D.IS_AGENT_REQUIRED FROM " +
                    "IOS_PRE_ENROLLED_DEVICE D LEFT OUTER JOIN IOS_DEP_PROFILE P ON P.ID = D.DEP_PROFILE_ID " +
                    "WHERE D.TENANT_DOMAIN = ? ORDER BY D.ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain());
            stmt.setInt(2, request.getStartIndex());
            stmt.setInt(3, request.getRowCount());
            rs = stmt.executeQuery();
            while (rs.next()) {
                DEPDevice depDevice = new DEPDevice();
                depDevice.setId(rs.getString("ID"));
                depDevice.setSerial(rs.getString("SERIAL"));
                depDevice.setUsername(rs.getString("USERNAME"));
                depDevice.setTenantDomain(rs.getString("TENANT_DOMAIN"));
                depDevice.setStatus(rs.getString("STATUS"));
                depDevice.setDepProfileId(rs.getInt("DEP_PROFILE_ID"));
                depDevice.setDepProfileName(rs.getString("PROFILE_NAME"));
                depDevice.setProfileAssignTime(rs.getString("PROFILE_ASSIGN_TIME"));
                depDevice.setProfilePushTime(rs.getString("PROFILE_PUSH_TIME"));
                depDevice.setDeviceAssignedTime(rs.getString("DEVICE_ASSIGNED_TIME"));
                depDevice.setDeviceAssignedBy(rs.getString("DEVICE_ASSIGNED_BY"));
                depDevice.setNeedBasicAuth(BooleanUtils.toBoolean(rs.getInt("NEED_BASIC_AUTH")));
                depDevice.setOS(rs.getString("OS"));
                depDevice.setDeviceFamily(rs.getString("DEVICE_FAMILY"));
                depDevice.setModel(rs.getString("DEVICE_MODEL"));
                depDevice.setDescription(rs.getString("DESCRIPTION"));
                depDevice.setColor(rs.getString("COLOR"));
                depDevice.setAgentRequired(BooleanUtils.toBoolean(rs.getInt("IS_AGENT_REQUIRED")));
                depDevice.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFIER"));
                depDevices.add(depDevice);
            }
        } catch (SQLException e) {
            throw new MobileDeviceManagementDAOException("Error occurred while fetching DEP devices.", e);
        } finally {
            MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
        }
        return depDevices;
    }
}
