public with sharing class LicenseInfoHandler extends Oly_TriggerHandler
{
    private Map<Id, License_Information__c> newMap;
    private Map<Id, License_Information__c> oldMap;
    private List<License_Information__c> newList;
    private List<License_Information__c> oldList;
    public LicenseInfoHandler(){
        this.newMap = (Map<Id, License_Information__c>) Trigger.newMap;
        this.oldMap = (Map<Id, License_Information__c>) Trigger.oldMap;
        this.newList = (List<License_Information__c>) Trigger.new;
        this.oldList = (List<License_Information__c>) Trigger.old;
    }

    protected override void beforeUpdate() {
        updateScope();
    }

    protected override void beforeInsert() {
        updateScope();
    }

    protected override void afterInsert() {
        updateAccountScope();
    }

    protected override void afterUpdate() {
        updateAccountScope();
    }
    protected override void afterDelete() {
        updateAccountScope();
    }

    private void updateScope() {

        for(License_Information__c li: newList){
            if (li.LicenseType__c == '医疗器械经营许可证' || li.LicenseType__c == '第二类医疗器械经营备案凭证' ) {
                
                String str = '';
                if (li.Scope__c != null && li.Scope__c != '') {                     
                    for(String scope : li.Scope__c.split(';')) {
                        str += ';' + '2' + scope;  
                    }
                } 

                if (li.Scope3__c != null && li.Scope3__c != '') {             
                    for(String scope : li.Scope3__c.split(';')) {
                        str += ';' + '3' + scope;  
                    }
                }

                if (str.length() > 0) {
                    li.ScopeKey__c = str.substring(1, str.length());
                } else{
                    li.ScopeKey__c = '';
                }
            }
        }    
    }

    private void updateAccountScope(){

        List<License_Information__c> repList;
        if (trigger.isDelete) {
            repList = oldList;
        } else {
            repList = newList;
        }

        Map<String,Account> accountIdMap = new Map<String,Account>();
        List<License_Information__c> liList = new List<License_Information__c>();
        Account acc = new Account();
        acc.Business_Scope__c = '';
        for (License_Information__c li: repList) {
            
            if ( li.LicenseType__c == '医疗器械经营许可证' || li.LicenseType__c == '第二类医疗器械经营备案凭证' ) {
                Account acc1 = acc.clone();
                acc1.Id = li.LicenseAndAccount__c;
                accountIdMap.put(li.LicenseAndAccount__c, acc1);
            }
        }  
        
        if(accountIdMap.size() > 0){

            liList = [select Id,Name,ScopeKey__c,LicenseType__c,LicenseAndAccount__c,Is_Active_Formula__c
                    from License_Information__c 
                    where LicenseAndAccount__c In :accountIdMap.keySet()
                    AND (LicenseType__c = '医疗器械经营许可证' OR LicenseType__c = '第二类医疗器械经营备案凭证' ) 
                    AND  Is_Active_Formula__c = true];
            
            for (License_Information__c li :liList){
                
                if(String.isNotBlank(li.ScopeKey__c)){
                    String strScope = '';
                    for(Integer i = 0; i < li.ScopeKey__c.length(); i+=120) {
                        if(li.ScopeKey__c.length() > (i + 120)) {
                            strScope += li.ScopeKey__c.substring(i, i + 120) + '\n';
                        } else {
                            strScope += li.ScopeKey__c.substring(i);
                        }
                    }

                    if(accountIdMap.containsKey(li.LicenseAndAccount__c) ){
                        if (String.isNotBlank(accountIdMap.get(li.LicenseAndAccount__c).Business_Scope__c)) {
                            accountIdMap.get(li.LicenseAndAccount__c).Business_Scope__c += ';\n' +strScope ;
                        } else {
                            accountIdMap.get(li.LicenseAndAccount__c).Business_Scope__c = strScope;
                        }
                    } 
                
                } 
            }           
        } 
        if (accountIdMap != null && accountIdMap.size() >0) {
            update accountIdMap.values();
        }
    }

}