/*
 * Author: Zhang,Huajian
 * Created Date: 05/29/2023
 * Purpose: Utility class for describe layouts
 * Test Class: LexNewAndEditContactPIPLControllerTest
 * History: 
 *      05/29/2023 - Zhang,Huajian - Initial Code.
 * 
 * */
public with sharing class LexNewAndEditContactPIPLController {
    public static Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    // 字段信息
    public static Map<string, SObjectField> fieldMap = new Map<string, SObjectField>();

    @AuraEnabled
    public static ResponseBodyLWC initData(Id rid, String recordTypeId, String sobjectType, String accid) {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;

        Boolean isDoctor = true;
        string s = null;
        
        //如果有记录类型，判断是不是医院类型
        if (String.isNotBlank(recordTypeId)) {
            System.debug('recordTypeId = ' + recordTypeId);
            s = Schema.SObjectType.Contact.getRecordTypeInfosById().get(recordTypeId).getDeveloperName();
            //说明无需加密
            if (s == 'Agency' || s == 'Internal_staff') {
                ResponseBodyLWC rbl = LexNewAndEditBasePIPLController.initData(rid, recordTypeId, sobjectType);
                if(!Test.isRunningTest()){
                    data = (Map<String,Object>)rbl.entity;
                }
                isDoctor = false;
                data.put('isDoctor', isDoctor);
                rbl.entity = data;
                return rbl;
            }
        }
        //说明无需加密
        if (String.isNotBlank(accid)) {
            List<Account> accs = [SELECT RecordType.DeveloperName FROM account WHERE id = :accid];
            if (accs.size() > 0) {
                s = accs[0].RecordType.DeveloperName;
                if (s == 'Office' || s == 'AgencyContact' || s == 'Agency') {
                    ResponseBodyLWC rbl = LexNewAndEditBasePIPLController.initData(rid, recordTypeId, sobjectType);
                    if(!Test.isRunningTest()){
                    data = (Map<String,Object>)rbl.entity;
                    }
                    isDoctor = false;
                    data.put('isDoctor', isDoctor);
                    rbl.entity = data;
                    return rbl;
                }
            }
        }

        //说明无需加密
        if (String.isNotBlank(rid)) {
            List<Contact> contact = [select Id,RecordTypeId from Contact where Id=:rid];
            if(String.isNotBlank(contact[0].RecordTypeId)){
                s = Schema.SObjectType.Contact.getRecordTypeInfosById().get(contact[0].RecordTypeId).getDeveloperName();
                if (s == 'Agency' || s == 'Internal_staff') {
                    ResponseBodyLWC rbl = LexNewAndEditBasePIPLController.initData(rid, recordTypeId, sobjectType);
                    if(!Test.isRunningTest()){
                    data = (Map<String,Object>)rbl.entity;
                    }
                    isDoctor = false;
                    data.put('isDoctor', isDoctor);
                    rbl.entity = data;
                    return rbl;
                }
            }
        }
        ResponseBodyLWC rbl = LexNewAndEditBasePIPLController.initData(rid, recordTypeId, sobjectType);
        if(rbl.status == 'Success'){
            if(!Test.isRunningTest()){
            data = (Map<String,Object>)rbl.entity;
            }
            data.put('isDoctor', isDoctor);
            rbl.entity = data;
        }
        return rbl;
    }

    @AuraEnabled
    public static ResponseV2 searchContactByAccountId(String accountId,String RecordTypeId,String Isactive) {
        accountId = accountId.substring(0,15);
        System.debug('accountId = ' + accountId);
        ResponseV2 res = new ResponseV2();
        try{
            List<Account> account = [select Id,Name from Account where Id = :accountId];
            if(account.size() <= 0){
                res.message = '没有找到对应客户';
                res.status = 'fail';
                return res;
            }
            List<Contact> contactList = [select id,AWS_Data_Id__c, CManageCode__c,Name from Contact where AccountId = :account[0].Id];
            Map<String,String> dataIdManageCodeMap = new Map<String,String>();
            for(Contact contact : contactList){
                dataIdManageCodeMap.put(contact.AWS_Data_Id__c,contact.CManageCode__c);
            }
            res.dataIdManageCodeMap = dataIdManageCodeMap;
            res.accountName = account[0].Name;
            res.contactList = contactList;

            ID DoctorRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Doctor').getRecordTypeId();
            Map<String,String> accMap= new Map<String,String>();
            List<Account> accList=[select ID,Is_Active__c,Is_Active_Formula__c from Account where ID =:accountId];
            if(accList.size()>0){
                for(Account acc:accList){
                    if (String.isNotBlank(acc.Is_Active__c)){
                        String subId = acc.Id;
                        accMap.put(subId.substring(0,15), acc.Is_Active__c);
                        continue;
                    }
                    if (String.isNotBlank(acc.Is_Active_Formula__c)){
                        String subId = acc.Id;
                        accMap.put(subId.substring(0,15), acc.Is_Active_Formula__c);
                    }
                }
            }
            System.debug('accMap= ' + accMap);
            //如果是Doctor记录类型的需要进行姓名+手机号判断
            if(DoctorRecordTypeId.equals(RecordTypeId)){
                res.isNameMobileVerif = true;
            }
            System.debug('Isactive= ' + Isactive);
            System.debug('DoctorRecordTypeId.equals(RecordTypeId) = ' + DoctorRecordTypeId.equals(RecordTypeId));
            System.debug('accMap.get(accountId) = ' + accMap.get(accountId));
            if (('有效'.equals(Isactive) || '有効'.equals(Isactive))
                && ('有効'.equals(accMap.get(accountId)) || '有效'.equals(accMap.get(accountId))) && (DoctorRecordTypeId.equals(RecordTypeId))) {
                res.isMobileVerif = true;
            }else {
                res.isMobileVerif = false;
            }
            res.status = 'success';
            return res;
        }catch(Exception e){
            res.message = e.getMessage() + ' ' + e.getLineNumber();
            res.status = 'fail';
            return res;
        }
    }

    public class ResponseV2{
        @AuraEnabled
        public List<Contact> contactList{set;get;}
        @AuraEnabled
        public String accountName{set;get;}
        @AuraEnabled
        public Map<String,String> dataIdManageCodeMap{set;get;}
        @AuraEnabled
        public Boolean isMobileVerif;
        @AuraEnabled
        public Boolean isNameMobileVerif;
        @AuraEnabled
        public String message{set;get;}
        @AuraEnabled
        public String status{set;get;}
    }
}