public with sharing class MCAEHandler extends Oly_TriggerHandler{

    private Map<Id, Maintenance_Contract_Asset_Estimate__c> newMap;
    private Map<Id, Maintenance_Contract_Asset_Estimate__c> oldMap;
    private List<Maintenance_Contract_Asset_Estimate__c> newList;
    private List<Maintenance_Contract_Asset_Estimate__c> oldList;


    public MCAEHandler () {
        this.newMap = (Map<Id, Maintenance_Contract_Asset_Estimate__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Maintenance_Contract_Asset_Estimate__c>) Trigger.oldMap;
        this.newList = (List<Maintenance_Contract_Asset_Estimate__c>) Trigger.new;
        this.oldList = (List<Maintenance_Contract_Asset_Estimate__c>) Trigger.old;
    }

    protected override void afterUpdate() {
        updateMainContract();
    }

    public void updateMainContract(){
        System.debug(LoggingLevel.INFO, '*** in update: ');
        Map<String,List<Maintenance_Contract_Asset_Estimate__c>> mcaMap = new Map<String,List<Maintenance_Contract_Asset_Estimate__c>>();
        for (Maintenance_Contract_Asset_Estimate__c  mca: newList) {
            if(mca.Repair_Price__c!=0){
                List<Maintenance_Contract_Asset_Estimate__c> mcaList = new List<Maintenance_Contract_Asset_Estimate__c>();
                mcaList.add(mca);
                if(mcaMap.containsKey(mca.Maintenance_Contract_Estimate__c)){
                    List <Maintenance_Contract_Asset_Estimate__c> mlist = mcaMap.get(mca.Maintenance_Contract_Estimate__c);
                    mlist.add(mca);
                    mcaMap.put(mca.Maintenance_Contract_Estimate__c,mlist);
                }else{
                    mcaMap.put(mca.Maintenance_Contract_Estimate__c,mcaList);
                }
            }
            
        }

        List<Maintenance_Contract__c> upMContractList = new List<Maintenance_Contract__c>();
        for (Maintenance_Contract_Estimate__c mc : [SELECT Id, Maintenance_Contract__c FROM Maintenance_Contract_Estimate__c WHERE Id IN:mcaMap.keySet()]) {
            Boolean hasThirdPartyReturnc = false;
            for (Maintenance_Contract_Asset_Estimate__c mca: mcaMap.get(mc.Id)) {
                if(mca.Third_Party_Return__c == true){
                    hasThirdPartyReturnc = true;
                }
            System.debug(LoggingLevel.INFO, '*** boolean: ' + hasThirdPartyReturnc);
            }
 
            if(mc.Maintenance_Contract__c != null){
                Maintenance_Contract__c mContract = new Maintenance_Contract__c();
                mContract.Id = mc.Maintenance_Contract__c;
                if(hasThirdPartyReturnc == true){
                    mContract.Third_Party_Return_Contract__c = true;
                }

                System.debug(LoggingLevel.INFO, '*** upMContractList: ' + upMContractList);
                upMContractList.add(mContract);
            }

        }
        System.debug(LoggingLevel.INFO, '*** upMContractList: ' + upMContractList);

        update upMContractList;


    }

    protected override void beforeUpdate() {
        
    }
}