public class SearchAgencyContactController {    
    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 aId{set;get;}

    public static Boolean checkNullString(String inputString){
        if(String.isEmpty(inputString)||String.isBlank(inputString)){
            return true;
        }
        return false;
    }

    public SearchAgencyContactController() {
        String acId = ApexPages.currentPage().getParameters().get('acId');
        searchKeyWord = ApexPages.currentPage().getParameters().get('searchContactKeyWord');
        aId = acId;
        PIPL_Search_Contact_Label = Label.PIPL_Search_Contact_Label;
        //1. Query Agency_Contact__c by acId
        List<Agency_Contact__c> acList = new List<Agency_Contact__c>();
        system.debug('Account Id from Front-end:'+acId);
        if(checkNullString(acId)&&checkNullString(searchKeyWord)){
            acList = new List<Agency_Contact__c>();
        }else{
            if(checkNullString(acId)){
                acList = new List<Agency_Contact__c>(); 
            }else {
                //2022-5-12 yjk 将科室匹配改为医院匹配查询联系人 statt
                Agency_Hospital_Link__c ahl = [select id from Agency_Hospital_Link__c where id = :acId];
                acList = new List<Agency_Contact__c>([select Id,AWS_Data_Id__c,Agency_Hospital__c, Agency_Hospital__r.Name from Agency_Contact__c  where Agency_Hospital__c =: ahl.Id and AWS_Data_Id__c!='']); 
                //2022-5-12 yjk 将科室匹配改为医院匹配查询联系人 end
            }
        }    
        //2. Prepare the Agency_Contact__c Info
        Map<String,Agency_Contact__c> awsIdToContactMap = new Map<String,Agency_Contact__c>();
        List<String> acAWSIds = new List<String>();
        for(Agency_Contact__c ac:acList){
            acAWSIds.add(ac.AWS_Data_Id__c);
            awsIdToContactMap.put(ac.AWS_Data_Id__c,ac);
        }
        contactsInfo = JSON.serialize(awsIdToContactMap);
        contactAWSIds = JSON.serialize(acAWSIds);
        staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('Agency_Contact__c'));        
    }

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

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