/*
 * Author: Bubba Li
 * Created Date: 02/08/2022
 * Purpose: SearchContactController
 * Test Class: SearchContactController
 * History: 
 *      02/08/2022 - Bubba Li - Initial Code.
 * 
 * */
public with sharing class SearchContactForReportController {
    public String searchKeyWord{set;get;}
    public String staticResource {get; set;}
    public String contactAWSIds {set;get;}
    public String contactsInfo {set;get;}
    public String PIPL_Search_Contact_Label{set;get;}
    public String ObjectTypeLabel {set;get;}
    public String aId{set;get;}
    public static Boolean checkNullString(String inputString){
        if(String.isEmpty(inputString)||String.isBlank(inputString)){
            return true;
        }
        return false;
    }
    //2023 08 30 张赫阳 PIPL页面改造 start
    public String OFSType{set;get;}
    //2023 08 30 张赫阳 PIPL页面改造 end
    public SearchContactForReportController() {
        String accountId = ApexPages.currentPage().getParameters().get('accountId');
        searchKeyWord = ApexPages.currentPage().getParameters().get('searchContactKeyWord');
        //2023 08 30 张赫阳 PIPL页面改造 start
        OFSType = ApexPages.currentPage().getParameters().get('Type');
        //2023 08 30 张赫阳 PIPL页面改造 end
        ObjectTypeLabel = Schema.getGlobalDescribe().get('Contact').getDescribe().getLabel();
        aId = accountId;
        PIPL_Search_Contact_Label = Label.PIPL_Search_Contact_Label;
        //1. Query Contact by accountId
        List<Contact> conList = new List<Contact>();
        system.debug('Account Id from Front-end:'+accountId);
        if(checkNullString(accountId)&&checkNullString(searchKeyWord)){
            conList = new List<Contact>();
        }else{
            if(checkNullString(accountId)){
                conList = new List<Contact>(); 
            }else {
                //2022-5-12 yjk 将科室匹配改为医院匹配查询联系人 statt
                System.debug('accountId = ' + accountId);
                //deloitte-zhj 20231107 start
                // List<Account> act = [select id,Hospital__c from Account where id = :accountId]; //deloitte-zhj 20231106
                // if(act.size() > 0){
                //     conList = new List<Contact>([select Id,AWS_Data_Id__c,Account.Name from Contact  where 	Account.Hospital__c=:act[0].Hospital__c and AWS_Data_Id__c!='' limit 2000]); 
                // }
                conList = new List<Contact>([select Id,Account.Name from Contact where Account.Hospital__c=:accountId and RecordType.DeveloperName = 'Doctor' limit 2000]); 
                //deloitte-zhj 20231107 end
                //2022-5-12 yjk 将科室匹配改为医院匹配查询联系人 end
            }
            
        }    
        contactsInfo = JSON.serialize(conList);
        //2. Prepare the Contact Info
        // Map<String,Contact> awsIdToContactMap = new Map<String,Contact>();
        // List<String> conAWSIds = new List<String>();
        // for(Contact con:conList){
        //     conAWSIds.add(con.AWS_Data_Id__c);
        //     awsIdToContactMap.put(con.AWS_Data_Id__c,con);
        // }
        // contactsInfo = JSON.serialize(awsIdToContactMap);
        // contactAWSIds = JSON.serialize(conAWSIds);
        // staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('Contact'));        
    }

    @RemoteAction
    public static Response searchContacts(String searchContactName, String accountId) {
        Response res = new Response();
        try {
            String sql = 'SELECT Id, Account.Name, Name, Phone, Email, MobilePhone FROM Contact Where RecordType.DeveloperName = \'Doctor\' ';
            if (!checkNullString(accountId)) {
                // Account act = [SELECT id, Hospital__c FROM Account WHERE id = :accountId];
                // if (!checkNullString(act.Hospital__c)) {
                    sql += 'AND Account.Hospital__c = \'' + accountId + '\' ';
                // } else {
                //     sql += 'AND Account.Hospital__c = \'\' ';
                // }
            }
            if (!checkNullString(searchContactName)) {
                sql += 'AND Name like \'%' + searchContactName + '%\' ';
            }
            sql += 'ORDER BY createddate DESC Limit 1000';
            System.debug('sql =======' + sql);
            List<Contact> conList = Database.query(sql);
            // data.put('conList', conList);
            res.status = 'Success';
            res.message = JSON.serialize(conList);
        } catch (Exception e) {
            res.status = 'fail';
            res.message = e.getMessage() + ' ' + e.getLineNumber();
        }
        return res;
    }

    // @RemoteAction
    // public static Response searchContacts(String awsContactIds,String searchContactName,String accountId) {
    //     system.debug('awsContactIds = ' + awsContactIds);
    //     Response resp = new Response();
    //     resp.status = 'fail';
    //     Map<String,Contact> awsIdToContactMapTemp = new Map<String,Contact>();
    //     if(!checkNullString(awsContactIds)){
    //         List<String> awsDataIds = (List<String>) JSON.deserialize(awsContactIds, List<String>.class);
    //         List<Contact> conListTemp = new List<Contact>();
    //         if(!checkNullString(accountId)){
    //             //2022-5-12 yjk 将科室匹配改为医院匹配查询联系人 statt
    //             //deloitte-zhj 20231107 start
    //             //Account act = [select id,Hospital__c from Account where id = :accountId];
    //             //conListTemp = new List<Contact>([select Id,AWS_Data_Id__c,Account.Name from Contact where Account.Hospital__c=:act.Hospital__c and AWS_Data_Id__c in:awsDataIds]);
    //             conListTemp = new List<Contact>([select Id,AWS_Data_Id__c,Account.Name from Contact where Account.Hospital__c=:accountId and AWS_Data_Id__c in:awsDataIds]);
    //             //deloitte-zhj 20231107 end
    //             //2022-5-12 yjk 将科室匹配改为医院匹配查询联系人 end
    //         }else {
    //             conListTemp = new List<Contact>([select Id,AWS_Data_Id__c,Account.Name from Contact where AWS_Data_Id__c in:awsDataIds]);
    //         }
    //         for(Contact con:conListTemp){
    //             awsIdToContactMapTemp.put(con.AWS_Data_Id__c,con);
    //         }
    //     }
    //     System.debug('awsIdToContactMapTemp = ' + awsIdToContactMapTemp);
    //     Map<String,Contact> noPIContactMapTemp = new Map<String,Contact>();
    //     List<Contact> partnerContactList = AWSServiceTool.getNoPIContact(searchContactName,accountId);
    //     System.debug('partnerContactList = ' + partnerContactList);            
    //     if(partnerContactList.size()>0){
    //         for(Contact con:partnerContactList){
    //             noPIContactMapTemp.put(con.Id,con);
    //         }               
    //     }
    //     if(awsIdToContactMapTemp.keySet().size()>0 ||noPIContactMapTemp.keySet().size()>0){
    //         resp.status = 'success';
    //         resp.message = JSON.serialize(awsIdToContactMapTemp);// PI contact info
    //         resp.noPIContactList = JSON.serialize(noPIContactMapTemp);//NoPI contact info
    //     }
    //     return resp;
    // }

    @RemoteAction
    public static Response searchContactsNoPI(String contactName) {
        Response resp = new Response();
        resp.status = 'fail';
        List<Contact> conListTemp = new List<Contact>([select Id,Name,Account.Name,Phone,Email,MobilePhone from Contact where Name like :contactName]);
        if(conListTemp.size() > 0){
            resp.status = 'success';
            resp.message = JSON.serialize(conListTemp);
        }
        return resp;
    }

    public class Response{
        public String message{set;get;}
        public String status{set;get;}
        public String noPIContactList{set;get;}
    }
}