/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class StartTradingControllerTest {
    
    static final String NONE = StartTradingController.NONE;
    static final String RC_HOSPITAL = '病院';
    static final String RC_OPP = '引合';
    static final String RC_SENRYAKUKASHITSUBUNRUI = '戦略科室分類 消化科';
    static final String RC_BYOUIN = '病院';
    static final String RC_SHINRYOUKA = '診療科 消化科';
    @TestSetup
    static void makeData(){
        TestDataUtility.CreatePIPolicyConfiguration('Contact');
    }
    /** 初期処理 */
    static testMethod void testInit() {
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        StartTradingController st = new StartTradingController(null);
        
        // リードテストデータ
        RecordType rect = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name =: RC_HOSPITAL limit 1];
        Account hospital = new Account(name = '中国病院');
        hospital.RecordTypeId = rect.Id;
        insert hospital;
        // 戦略課室を取得
        List<Account> dcList = [select Id from Account where ParentId = :hospital.Id and RecordType.Name = :RC_SENRYAKUKASHITSUBUNRUI];
        
        RecordType dept_rect = [select id from RecordType where IsActive = true and SobjectType = 'Account' and Name =:RC_SHINRYOUKA];
        // 診療科選択リストテストデータ
        Account[] acts = new Account[]{
            new Account(Name='*', Department_Name__c = '診療科01', Hospital__c = hospital.Id, Department_Class__c = dcList[0].Id, ParentId = dcList[0].Id, RecordTypeId = dept_rect.Id),
            new Account(Name='*', Department_Name__c = '診療科02', Hospital__c = hospital.Id, Department_Class__c = dcList[0].Id, ParentId = dcList[0].Id, RecordTypeId = dept_rect.Id),
            new Account(Name='*', Department_Name__c = '診療科03', Hospital__c = hospital.Id, Department_Class__c = dcList[0].Id, ParentId = dcList[0].Id, RecordTypeId = dept_rect.Id)
        };
        insert acts;
        
        // 期待値：診療科選択リスト
        List<SelectOption> expectDepList = new List<SelectOption>();
        expectDepList.add(new SelectOption(NONE, NONE));
        expectDepList.add(new SelectOption(acts[0].Id, '中国病院 消化科 診療科01'));
        expectDepList.add(new SelectOption(acts[1].Id, '中国病院 消化科 診療科02'));
        expectDepList.add(new SelectOption(acts[2].Id, '中国病院 消化科 診療科03'));
        
        // 担当者選択リストテストデータ
        Contact[] cts = new Contact[]{
            new Contact(LastName = '担当者１',FirstName= 'ZZ1', AccountId = acts[0].Id),
            new Contact(LastName = '担当者１',FirstName= 'ZZ2', AccountId = acts[1].Id),
            new Contact(LastName = '担当者１',FirstName= 'ZZ3',AccountId = acts[2].Id),
            new Contact(LastName = '担当者２',FirstName= 'ZZ4', AccountId = acts[2].Id),
            new Contact(LastName = '担当者３',FirstName= 'ZZ5', AccountId = acts[2].Id)
        };
        Database.SaveResult[] ctsRet = Database.insert(cts, false);
        
        // 期待値：担当者選択リストMap
        Map<String,List<SelectOption>> expectConMap = new Map<String,List<SelectOption>>();
        // なしの担当者選択リスト
        List<SelectOption> expectConList0 = new List<SelectOption>();
        expectConList0.add(new SelectOption(NONE, NONE));
        expectConMap.put(NONE, expectConList0);
        // 診療科01の担当者選択リスト
        List<SelectOption> expectConList1 = new List<SelectOption>();
        expectConList1.add(new SelectOption(NONE, NONE));
        expectConList1.add(new SelectOption(ctsRet[0].getId(), '担当者１ ZZ1'));
        expectConMap.put(acts[0].Id, expectConList1);
        // 診療科02の担当者選択リスト
        List<SelectOption> expectConList2 = new List<SelectOption>();
        expectConList2.add(new SelectOption(NONE, NONE));
        expectConList2.add(new SelectOption(ctsRet[1].getId(), '担当者１ ZZ2'));
        expectConMap.put(acts[1].Id, expectConList2);
        // 診療科03の担当者選択リスト
        List<SelectOption> expectConList3 = new List<SelectOption>();
        expectConList3.add(new SelectOption(NONE, NONE));
        expectConList3.add(new SelectOption(ctsRet[4].getId(), '担当者３ ZZ5'));
        expectConList3.add(new SelectOption(ctsRet[2].getId(), '担当者１ ZZ3'));
        expectConList3.add(new SelectOption(ctsRet[3].getId(), '担当者２ ZZ4'));
        
        expectConMap.put(acts[2].Id, expectConList3);
        
        // 初期処理テスト
        Lead l = new Lead();
        l.Hospital_Name__c = acts[0].Id;
        //l.Contact_Name__c = cts[0].Id;
        l.Department_Class__c = dcList[0].Id;
        l.LastName = '毛';
        l.FirstName = '沢東';
        l.LeadSource = 'その他';
        l.Other_Society__c = 'その他学会テキスト';
        l.Company = '会社名';
        l.Status = '未跟进';
        insert l;      

        // リードID
        st.leadId = l.Id;
        st.init();
        
        // 診療科選択リストチェック 
        // system.assertEquals(expectDepList, st.depList);
        
        // 担当者選択リストMapチェック
        //system.assertEquals(expectConMap, st.conMap);
    }
    
    /** 診療科選択リスト変更イベント */
    static testMethod void testChange() {
        StartTradingController st = new StartTradingController(null);
        
        // Map設定
        st.conMap = new Map<String,List<SelectOption>>();
        
        // 診療科未設定
        List<SelectOption> sltOptNasi = new List<SelectOption>();
        sltOptNasi.add(new SelectOption(NONE, NONE));
        st.conMap.put(NONE, sltOptNasi);
        
        // 診療科TEST1
        List<SelectOption> sltOpt1 = new List<SelectOption>();
        sltOpt1.add(new SelectOption(NONE, NONE));
        sltOpt1.add(new SelectOption('TANTO1', '担当者１'));
        sltOpt1.add(new SelectOption('TANTO2', '担当者２'));
        st.conMap.put('TEST1', sltOpt1);
        
        // 診療科TEST2
        List<SelectOption> sltOpt2 = new List<SelectOption>();
        sltOpt2.add(new SelectOption(NONE, NONE));
        sltOpt2.add(new SelectOption('TANTO2', '担当者２'));
        sltOpt2.add(new SelectOption('TANTO3', '担当者３'));
        sltOpt2.add(new SelectOption('TANTO4', '担当者４'));
        st.conMap.put('TEST2', sltOpt2);
        
        // 診療科未設定テスト
        st.sltDep = NONE;
        st.depChange();
        system.assertEquals(sltOptNasi, st.conList);
        
        // 診療科TEST1選択テスト
        st.sltDep = 'TEST1';
        st.depChange();
        system.assertEquals(sltOpt1, st.conList);
        
        // 診療科TEST2選択テスト
        st.sltDep = 'TEST2';
        st.depChange();
        system.assertEquals(sltOpt2, st.conList);
    }
    
    /** キャンセル */
    static testMethod void testCancel() {
        StartTradingController st = new StartTradingController(null);
        // リードID
        st.leadId = 'test_leadid';
        // 期待値
        String expectUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/test_leadid';
        // キャンセルテスト
        PageReference pr = st.cancel();
        system.assertEquals(expectUrl, pr.getUrl());
        
    }
    
    /** 戦略科室コード取得 */
    static testMethod void testGetDepartment() {
        StartTradingController st = new StartTradingController(null);

        // 戦略科室・診療科選択テストデータ
        RecordType rect = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name =: RC_HOSPITAL limit 1];
        Account hospital = new Account(name = '中国病院');
        hospital.RecordTypeId = rect.Id;
        insert hospital;
        RecordType recSenryakukashitsu = [select id from RecordType where IsActive = true and SobjectType=: 'Account' and Name=:RC_SENRYAKUKASHITSUBUNRUI limit 1];
        Account deptA = new Account(Name = '戦略科室テスト');
        deptA.RecordTypeId = recSenryakukashitsu.id;
        deptA.Department_Class_Label__c = '消化科';
        deptA.parentId = hospital.Id;
        insert deptA;
        Account a = new Account(Name = '診療科01');
        a.ParentId = deptA.Id;
        a.Hospital__c = hospital.Id;
        a.Department_Class__c = deptA.Id;
        a.RecordTypeId = [select id from RecordType where IsActive = true and SobjectType = 'Account' and Name =:RC_SHINRYOUKA].Id;
        insert a;

        // 診療科選択値
        st.sltDep = a.Id;
        
        // 戦略科室コード取得テスト
        //Account dept = st.getDepartment();
        // 戻り値チェック
        //system.assertEquals(deptA.Id, dept.Department_Class__c);
    }
    
    /** 取引の開始(診療科未選択) */
    static testMethod void testStart01() {
        StartTradingController st = new StartTradingController(null);
        // 診療科未選択
        st.sltDep = NONE;
        // 取引の開始
        PageReference pr = st.start();
        // 戻り値NULL
        system.assertEquals(pr, null);
    }
    
    /** 取引の開始(担当者未選択→担当者登録をチェック) */
    static testMethod void testStart02() {
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        StartTradingController st = new StartTradingController(null);

        // リードテストデータ
        RecordType rect = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name =: RC_HOSPITAL limit 1];
        Account hospital = new Account(name = '中国病院', Is_Active__c = '有効');
        hospital.RecordTypeId = rect.Id;
        insert hospital;
        Lead l = new Lead();
        l.Hospital_Name__c = hospital.Id;
        l.LastName = '毛';
        l.FirstName = '沢東';
        l.LeadSource = 'その他';
        l.Other_Society__c = 'その他学会テキスト';
        l.Company = '会社名';
        insert l;

        // トレーニング
        //2021-07-06  mzy  SWAG-C4H99E 询价中的来源更改 start
        RecordType rectCam =
            [select Id from RecordType
             where IsActive = true and SobjectType = 'Campaign'
                              and DeveloperName = 'ServiceEngineerTraining'];
        Campaign c = new Campaign(Name = 'トレーニング１');
        c.Name2__c = '1234';
        c.RecordTypeId = rectCam.Id;
        c.StartDate = Date.today().addDays(-15);
        c.EndDate = Date.today().addDays(18);
        c.Mailflg_after45__c = true;
        c.Mailflg_cancel__c = true;
        c.Mailflg_before15__c = true;
        c.Mailflg_before7__c = true;
        c.Mailflg_after3__c = true;
        c.BusinessPurpose__c = '10000000000<img';
        c.BusinessResult__c = '10000000000<img';
        c.Society_Hold_Place__c = '10000000000<img';
        insert c;
        CampaignMember[] children = new CampaignMember[] {
            new CampaignMember(LeadId = l.Id, CampaignId = c.Id)
        };
        insert children;
        //2021-07-06  mzy  SWAG-C4H99E 询价中的来源更改 end

        // 戦略科室・診療科選択テストデータ
        RecordType recSenryakukashitsu = [select id from RecordType where IsActive = true and SobjectType=: 'Account' and Name=:RC_SENRYAKUKASHITSUBUNRUI limit 1];
        Account deptA = new Account(Name = '戦略科室テスト');
        deptA.RecordTypeId = recSenryakukashitsu.id;
        deptA.Department_Class_Label__c = '消化科';
        deptA.parentId = hospital.Id;
        insert deptA;
        RecordType recSinryouka = [select id from RecordType where IsActive = true and DeveloperName=:'Department_GI' limit 1];
        Account a = new Account();
        a.Name = '診療科01';
        a.Department_Name__c = '診療科01';
        a.RecordTypeId = recSinryouka.id;
        a.Hospital__c = hospital.Id;
        a.Department_Class__c = deptA.Id;
        a.ParentId = deptA.Id;
        insert a;

        // リード
        st.leadId = l.Id;
        st.lead = l;
        // 診療科選択あり
        st.sltDep = a.Id;
        // 担当者未選択
        st.sltCon = NONE;
        
        System.Test.startTest();

        // 取引の開始
        PageReference pr = st.start();
        
        // 担当者が登録されていることをチェック
        List<Contact> contList = [select LastName, FirstName, Strategic_dept_Class__c from Contact where AccountId =: a.Id order by CreatedDate desc];
        //Contact cont = contList[0];
        // system.assertEquals(cont.LastName, l.LastName);
        // system.assertEquals(cont.FirstName, l.FirstName);
        // system.assertEquals(cont.Strategic_dept_Class__c, deptA.Id);

        System.Test.stopTest();
    }
    
    /** 取引の開始(診療科・担当者選択あり→URLをチェック) */
    static testMethod void testStart03() {
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        StartTradingController st = new StartTradingController(null);

        // リードテストデータ
        RecordType rect = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name =: RC_HOSPITAL limit 1];
        Account hospital = new Account(name = '中国病院', Is_Active__c = '有効');
        hospital.RecordTypeId = rect.Id;
        insert hospital;
        Lead l = new Lead();
        l.Hospital_Name__c = hospital.Id;
        l.LastName = '毛';
        l.FirstName = '沢東';
        l.Company = '会社名';
        insert l;

        // 戦略科室・診療科選択テストデータ
        RecordType recSenryakukashitsu = [select id from RecordType where IsActive = true and SobjectType=: 'Account' and Name=:RC_SENRYAKUKASHITSUBUNRUI limit 1];
        Account deptA = new Account(Name = '戦略科室テスト');
        deptA.RecordTypeId = recSenryakukashitsu.id;
        deptA.Department_Class_Label__c = '消化科';
        deptA.parentId = hospital.Id;
        insert deptA;
        RecordType recSinryouka = [select id from RecordType where IsActive = true and DeveloperName=:'Department_GI' limit 1];
        Account a = new Account();
        a.Name = '診療科01';
        a.ParentId = deptA.Id;
        a.Department_Name__c = '診療科01';
        a.RecordTypeId = recSinryouka.id;
        a.Hospital__c = l.Hospital_Name__c;
        a.Department_Class__c = deptA.Id;
        insert a;

        // 担当者選択テストデータ
        Contact c = new Contact(LastName = '担当者１',FirstName='ZZ6', AccountId = a.Id);
        insert c;
            
        // リードID
        st.leadId = l.Id;
        st.lead = l;
        // 診療科選択あり
        st.sltDep = a.Id;
        // 担当者未選択
        st.sltCon = c.Id;
        
        System.Test.startTest();

        // 取引の開始
        //PageReference pr = st.start();
        
        // 期待値
        /*
        String uri = 'retURL=%2F' + l.Id;
        uri += '&ent=Opportunity';
        rect = [select id from RecordType where IsActive = true and SobjectType =: 'Opportunity' and Name =: RC_OPP limit 1];
        uri += '&RecordType=' + rect.Id;
        uri += '&' + system.label.StartTrading_P_Hospital + '_lkid=' + hospital.Id;
        uri += '&' + system.label.StartTrading_P_Hospital + '=' + hospital.name;
        uri += '&' + system.label.StartTrading_P_Dept + '_lkid=' + deptA.Id;
        uri += '&' + system.label.StartTrading_P_Dept + '=' + deptA.name;
        uri += '&' + system.label.StartTrading_P_Name + '=' + a.Id;
        */
        //Opportunity opp = [select Id from Opportunity where AccountId = :a.Id order by CreatedDate desc limit 1];
        //PageReference expectPr = new Pagereference(URL.getSalesforceBaseUrl().toExternalForm() + '/' + opp.Id + '/e?ent=Opportunity&retURL=%2F' + opp.Id);
        // system.assertEquals(expectPr.getUrl(), pr.getUrl());

        System.Test.stopTest();
    }    
}