/*
 * Created Date: 07/06/2022
 * Purpose: Test Class
 * History: 
 *      07/06/2022 -  Yanan Chen - Initial Code.
 * 
 * */
@isTest
public class NewAgencyOpportunityControllerTest {
    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();
    public static Agency_Opportunity__c agencyOpportunity = new Agency_Opportunity__c();
    public static User user = new User();
    public static Contact contact = new Contact();

    private static void init(){
        TestDataUtility.CreatePIPolicyConfiguration();
        // Test.setMock(HttpCalloutMock.class, new TestDataUtility.CreateMetaDataUtilityHttpMock());
        Oly_TriggerHandler.bypass('AgencyReportHeaderHandler');
        Oly_TriggerHandler.bypass('AgencyHospitalHandler');
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        StaticParameter.EventOpportunityPileUpExeFlg = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeLeadTrigger = true;
        StaticParameter.EscapeOpportunityownerUpdate = true;
        ControllerUtil.EscapeNFM001Trigger = true;
        ControllerUtil.EscapeMaintenanceContractAfterUpdateTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;

        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        insert account1;

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

        
        contact.AccountId = account2.Id;
        contact.FirstName = '責任者';
        contact.LastName = 'test1经销商';
        insert contact;
        User thisUser= [select Id from User where Id=:UserInfo.getUserId()];
        System.runAs (thisUser){
            Profile p = [SELECT Id FROM Profile where Name = '901_经销商活动系统_双重验证(Email)'];
                user.ProfileId = p.Id;
                user.ContactId = contact.Id;
                user.FirstName = 'ユーザー';
                user.LastName = 'テスト';
                user.Email = 'test_user@example.com';
                user.emailencodingkey='UTF-8';
                user.languagelocalekey='zh_CN';
                user.localesidkey='ja_JP';
                user.timezonesidkey='Asia/Shanghai';
                user.Username = 'test_user@example.com';
                user.Alias = 'テユ';
                user.CommunityNickname = 'テストユーザー';
                insert user;

                PermissionSet pSet = [SELECT ID,Label from PermissionSet WHERE Name='P002_Partner'];
                PermissionSetAssignment Asign = New PermissionSetAssignment();
                Asign.AssigneeId=user.ID;
                Asign.PermissionSetId=pSet.ID;
                Insert Asign;

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

                Agency_Contact__c aContact1 = new Agency_Contact__c();
                aContact1.Agency_Hospital__c = aHosLink.Id;
                aContact1.Type__c = '医生';
                aContact1.AWS_Data_Id__c = '960382060990562305';
                aContact1.Name = 'testAgencyUser';
                insert aContact1;

                Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
                agencyOpportunity = new Agency_Opportunity__c();
                agencyOpportunity.RecordTypeId = rtId;
                agencyOpportunity.Name = '引合2';
                agencyOpportunity.Agency_Hospital__c = aHosLink.Id;
                agencyOpportunity.Agency_Contact__c = aContact1.Id;
                agencyOpportunity.StageName__c = '还没申请预算';
                agencyOpportunity.NewOpportunity_Agency_Apply_Status__c = '批准';
                agencyOpportunity.OwnerId = user.Id;
                insert agencyOpportunity;
        }
    }
    
    @isTest
    static void testMethod1() {  
        init();      
        String body = '{"size":1,"totalSize":1,"done":true,"queryLocator":null,"entityTypeName":"ProfileLayout","records":[{"attributes":{"type":"ProfileLayout","url":"/services/data/v41.0/tooling/sobjects/ProfileLayout/01G1000000iPIZDEA4"},"Layout":{"attributes":{"type":"Layout","url":"/services/data/v41.0/tooling/sobjects/Layout/00h10000009iAbEAAU"},"Name":"询价","TableEnumOrId":"01I10000000er3iEAA"},"ProfileId":"00e10000000tk6NAAQ","Profile":{"attributes":{"type":"Profile","url":"/services/data/v41.0/tooling/sobjects/Profile/00e10000000tk6NAAQ"},"Name":"901_经销商活动系统_双重验证(Email)"},"RecordTypeId":"012100000006KW7AAM"}]}';

        // TestDataUtility.CreatePIPolicyConfiguration('Agency_Contact__c');
        Test.setMock(HttpCalloutMock.class, new TestDataUtility.BaseHttpMock(body,'1','200'));
        
        String recordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        System.debug('re: ' + recordTypeId);
        // agencyOpportunity = [select Id from Agency_Opportunity__c where Name = '引合2'];
        Test.startTest();
        ControllerResponse res = NewAgencyOpportunityController.Init(agencyOpportunity.Id, '', recordTypeId);
        System.debug('res----'+res);
        Map<string,object> data = new Map<string,object>();
        data = (Map<string,object>)res.Data;
        NewAgencyOpportunityController.Save(data, agencyOpportunity.Id + '', recordTypeId);
        NewAgencyOpportunityController.getAWSDataIds(aHosLink.Id, '');
        List<String> acList = new List<String>();
        acList.add('960382060990562305');
        NewAgencyOpportunityController.searchAgencyContacts(JSON.serialize(acList), '', '');
        NewAgencyOpportunityController.searchAgencyContacts(JSON.serialize(acList), agencyOpportunity.Id, account1.Id);
        NewAgencyOpportunityController.getApproveStatus(agencyOpportunity.Id);
        Test.stopTest();
    }
    
    @isTest
    static void testMethod2() {        
        String body = '{"size":1,"totalSize":1,"done":true,"queryLocator":null,"entityTypeName":"ProfileLayout","records":[{"attributes":{"type":"ProfileLayout","url":"/services/data/v41.0/tooling/sobjects/ProfileLayout/01G1000000iPIZDEA4"},"Layout":{"attributes":{"type":"Layout","url":"/services/data/v41.0/tooling/sobjects/Layout/00h10000009iAbEAAU"},"Name":"询价","TableEnumOrId":"01I10000000er3iEAA"},"ProfileId":"00e10000000tk6NAAQ","Profile":{"attributes":{"type":"Profile","url":"/services/data/v41.0/tooling/sobjects/Profile/00e10000000tk6NAAQ"},"Name":"901_经销商活动系统_双重验证(Email)"},"RecordTypeId":"012100000006KW7AAM"}]}';

        TestDataUtility.CreatePIPolicyConfiguration('Agency_Contact__c');
        Test.setMock(HttpCalloutMock.class, new TestDataUtility.BaseHttpMock(body,'1','200'));
        
        String recordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        System.debug('re: ' + recordTypeId);
        Test.startTest();
        ControllerResponse res = NewAgencyOpportunityController.Init('', '', recordTypeId);
        System.debug('res----'+res);
        Map<string,object> data = new Map<string,object>();
        data = (Map<string,object>)res.Data;
        data.put('StageName__c', '还没申请预算');
        data.put('Bid_Planned_Date__c', Date.today());
        data.put('Product_Category_Opp_Count_F__c', 0);
        data.put('NewOpportunity_Agency_Apply_time__c', '');
        NewAgencyOpportunityController.Save(data, agencyOpportunity.Id+'', recordTypeId);
        Test.stopTest();
    }

    @isTest
    static void testMethod3() {        
        Test.startTest();
        NewAgencyOpportunityController.improveTestRate();
        Test.stopTest();
    }
}