@isTest
private class AgencyContactHandlerTest {

    public static Account account1 = new Account();
    public static Account account2 = new Account();
    public static Agency_Hospital_Link__c aHosLink = new Agency_Hospital_Link__c();
    public static Agency_Contact__c aContact = new Agency_Contact__c();
    static testMethod void testBeforeInsert() {

        // 取引先
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        insert account1;

        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        insert account2;


        List<Account> accTestList = [SELECT Id, Name FROM Account order by Name];
        System.debug('GYFaccTestList='+accTestList);
        System.assertEquals(10, accTestList.size());
        System.assertEquals('test1医院', accTestList[0].Name);

        //经销商医院データを作る
        aHosLink.Name = 'test1经销商医院';
        aHosLink.Hospital__c = account1.Id;
        aHosLink.Agency__c = account2.Id;
        insert aHosLink;

        //.客户人员データを作る
        List<Agency_Contact__c> aContactList = new List<Agency_Contact__c>();
        Agency_Contact__c aContact1 = new Agency_Contact__c();
        aContact1.Agency_Hospital__c = aHosLink.Id;
        aContact1.Type__c = '医生';
        aContact1.Name = 'testAgencyUser';
        aContactList.add(aContact1);

        Agency_Contact__c aContact2 = new Agency_Contact__c();
        aContact2.Agency_Hospital__c = aHosLink.Id;
        aContact2.Type__c = '护士';
        aContact2.Name = 'testAgencyUser';
        aContactList.add(aContact2);
        Test.startTest();
        insert aContactList;
        ////经销商医院名前を変更する
        //aContact.Name = 'testAgencyUser1'
        //update aContact;

        Agency_Contact__c clonedAC = aContact1.clone(false,true,false,false);
        clonedAC.Name = 'testAgencyUser2';
        clonedAC.Agency_ID__c = '000000000000000';
        clonedAC.Agency_Hospital__c  = null;
        insert clonedAC;
        Test.stopTest();
    }
    static testMethod void testBeforeUpdate() {

        // 取引先
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        insert account1;

        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        insert account2;


        List<Account> accTestList = [SELECT Id, Name FROM Account order by Name];
        System.debug('GYFaccTestList='+accTestList);
        System.assertEquals(10, accTestList.size());
        System.assertEquals('test1医院', accTestList[0].Name);

        //经销商医院データを作る
        aHosLink.Name = 'test1经销商医院';
        aHosLink.Hospital__c = account1.Id;
        aHosLink.Agency__c = account2.Id;
        insert aHosLink;

        //.客户人员データを作る
        aContact.Agency_Hospital__c = aHosLink.Id;
        aContact.Type__c = '医生';
        aContact.Name = 'testAgencyUser';

        Agency_Contact__c aContact2 = new Agency_Contact__c();
        aContact2.Agency_Hospital__c = aHosLink.Id;
        aContact2.Type__c = '医生';
        aContact2.Name = 'testAgencyUser1';
        insert aContact2;

        Test.startTest();
        try{
            insert aContact;
        }catch(Exception e){
            system.debug('Exception from insert contact:'+e.getMessage());
        }
        ////.客户人员名前を変更する
        aContact.Name = 'testAgencyUser1';
        try {
            update aContact;
        } catch (Exception e) {

        }
        
        Test.stopTest();
    }

    @isTest static void test_setAgency_Contact_Share_ToRole() {
        Oly_TriggerHandler.bypass('AgencyHospitalHandler');
        MergeAgencyActivityBatchTest.makeNormalData(true);

        // assert
        List<Agency_Contact__c> tList = [SELECT Name, Agency_Hospital__r.Agency__c
                FROM Agency_Contact__c ORDER BY Id LIMIT 2];
        System.assertEquals(2, tList.size());
        System.assertEquals(MergeAgencyActivityBatchTest.agency1.Id, tList[0].Agency_Hospital__r.Agency__c);
        System.assertEquals(MergeAgencyActivityBatchTest.agency1.Id, tList[1].Agency_Hospital__r.Agency__c);
        List<Agency_Contact__Share> tsList = [SELECT Id
                FROM Agency_Contact__Share WHERE ParentId = :tList AND RowCause = 'Manual'];
        // System.assertEquals(2, tsList.size());

        Test.startTest();
        Delete tList[0];
        try{
            AgencyContactHandler handler = new AgencyContactHandler();
            handler.newMap = new Map<Id, Agency_Contact__c>();
            handler.oldMap = new Map<Id, Agency_Contact__c>();
            handler.newList =new List<Agency_Contact__c>();
            handler.oldList = new List<Agency_Contact__c>();
            handler.afterInsert();
            handler.afterUndelete();           
        }catch(Exception e){
            system.debug('Exception from undelete');
        }
        Test.stopTest();
    }
}