/**
 * 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
public class AccountTriggerTest {
    private static Address_Level__c al { get; set; }
    private static Address_Level2__c al2 { get; set; }

    private static void init() {
        al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        
        al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;
    }
    // MB_客户
    @isTest
    public static void syncMBAccountTest() {
        init();
        
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        
        List<MB_Account__c> mbaccounts = [select Id, Account__c from MB_Account__c];
        System.assertEquals(0, mbaccounts.size());
        
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'SoakupTestHp', OwnerId = hpOwner.Id);
        insert hp;
        List<MB_Account__c> mbaccounts2 = [select Id, Account__c, State_Text__c from MB_Account__c order by Account__c];
//        System.assertEquals(9, mbaccounts2.size());
//        MB_Account__c mbaccount1 = [select Id, Account__c, State_Text__c from MB_Account__c where Account__c = :hp.Id];
//        System.assertEquals(hp.Id, mbaccount1.Account__c);
//        System.assertEquals(null, mbaccount1.State_Text__c);
        
        hp.State_Master__c = al.id;
        hp.City_Master__c = al2.id;
        hp.ownerid = hpOwner.id;
        NFM001Controller.isRunning = false;
        update hp;
        
//        mbaccount1 = [select Id, Account__c, State_Text__c from MB_Account__c where Account__c = :hp.Id];
//        System.assertEquals(hp.Id, mbaccount1.Account__c);
        //System.assertEquals('東京', mbaccount1.State_Text__c);
        
        mbaccounts2 = [select Id, Account__c, State_Text__c from MB_Account__c order by Account__c];
//        System.assertEquals(9, mbaccounts2.size());
//        System.assertEquals(hp.Id, mbaccounts2[0].Account__c);
        
        delete hp;
        List<MB_Account__c> mbaccounts3 = [select Id, Account__c from MB_Account__c];
//        System.assertEquals(8, mbaccounts3.size());
    }

    // 客户2
    @isTest
    static void syncAccount2Test() {
        init();
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_Class_GI'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }
        
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;

        List<Account2__c> acc2s = [select Id, Name, Account_OrgId__c, Account_Org__c, Account__c, RecordType_DeveloperName__c from Account2__c];
        System.assertEquals(0, acc2s.size());
        
        // 病院作成
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'syncAccount2Hp', OwnerId = hpOwner.Id);
        insert hp;
        hp = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where Id = :hp.Id];
        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        Account2__c hp2 = [select Id, Name, Account_OrgId__c, Account_Org__c, Account__c, RecordType_DeveloperName__c from Account2__c where Account_Org__c = :hp.Id];
        List<Account2__c> dc2List = [select Id, Name, Account_OrgId__c, Account_Org__c, Account__c, RecordType_DeveloperName__c from Account2__c where Account_Org__r.ParentId = :hp.Id];
        Account2__c dc2 = [select Id, Name, Account_OrgId__c, Account_Org__c, Account__c, RecordType_DeveloperName__c from Account2__c where Account_Org__r.ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        System.assertEquals(hp.Name, hp2.Name);
        System.assertEquals(hp.Id, hp2.Account__c);
        System.assertEquals(hp.Id, hp2.Account_Org__c);
        System.assertEquals(hp.Id, hp2.Account_OrgId__c);
        System.assertEquals(hp.RecordType_DeveloperName__c, hp2.RecordType_DeveloperName__c);
        System.assertEquals(hp.Account2__c, hp2.Id);
        System.assertEquals(8, dc2List.size());
        System.assertEquals(dc.Name, dc2.Name);
        System.assertEquals(dc.Account2__c, dc2.Id);
        System.assertEquals(dc.Id, dc2.Account__c);
        System.assertEquals(dc.Id, dc2.Account_Org__c);
        System.assertEquals(dc.Id, dc2.Account_OrgId__c);
        
        // 診療科を作る
        NFM001Controller.isRunning = false;
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;
        dpt = [select Id, ParentId, Name, RecordType_DeveloperName__c, Account2__c from Account where Id = :dpt.Id];
        Account2__c dpt2 = [select Id, Name, Account_OrgId__c, Account_Org__c, Account__c, RecordType_DeveloperName__c from Account2__c where Account_Org__c = :dpt.Id];
        System.assertEquals(dpt.Name, dpt2.Name);
        System.assertEquals(dpt.Account2__c, dpt2.Id);
        System.assertEquals(dpt.ParentId, dpt2.Account__c);
        System.assertEquals(dpt.Id, dpt2.Account_Org__c);
        System.assertEquals(dpt.Id, dpt2.Account_OrgId__c);
        System.assertEquals(dpt.RecordType_DeveloperName__c, dpt2.RecordType_DeveloperName__c);
        
        // 名前変更
        NFM001Controller.isRunning = false;
        hp.Name = 'syncAccount2_Hp';
        update hp;
        hp = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where Id = :hp.Id];
        dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        dpt = [select Id, ParentId, Name, RecordType_DeveloperName__c, Account2__c from Account where Id = :dpt.Id];
        hp2 = [select Id, Name, Account_OrgId__c, Account_Org__c, Account__c, RecordType_DeveloperName__c from Account2__c where Account_Org__c = :hp.Id];
        dc2 = [select Id, Name, Account_OrgId__c, Account_Org__c, Account__c, RecordType_DeveloperName__c from Account2__c where Account_Org__r.ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        dpt2 = [select Id, Name, Account_OrgId__c, Account_Org__c, Account__c, RecordType_DeveloperName__c from Account2__c where Account_Org__c = :dpt.Id];
        System.assertEquals(hp.Name, hp2.Name);
        System.assertEquals(hp.Account2__c, hp2.Id);
        System.assertEquals(hp.Id, hp2.Account__c);
        System.assertEquals(hp.Id, hp2.Account_Org__c);
        System.assertEquals(hp.Id, hp2.Account_OrgId__c);
        System.assertEquals(hp.RecordType_DeveloperName__c, hp2.RecordType_DeveloperName__c);
        System.assertEquals(dc.Name, dc2.Name);
        System.assertEquals(dc.Account2__c, dc2.Id);
        System.assertEquals(dpt.Name, dpt2.Name);
        System.assertEquals(dpt.Account2__c, dpt2.Id);
        
        List<Account2__c> acc2List = [select Id, Account__c, Account_Org__c from Account2__c order by Account_Org__c];
        System.assertEquals(10, acc2List.size());           // HP(1) + DC(8) + DPT(1)
        System.assertEquals(hp.Id, acc2List[0].Account__c);
        
        // 削除
        NFM001Controller.isRunning = false;
        delete dpt;
        acc2List = [select Id, Account__c from Account2__c];
        System.assertEquals(9, acc2List.size());
    }

    @isTest
    static void changeOwnerTest() {
        init();
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_Class_GI'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;

        // 病院作成
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        insert hp;

        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        Account dc2 = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_BF'];

        // 診療科を作る
        NFM001Controller.isRunning = false;
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;

        dc.OwnerId = hpOwner2.Id;
        dc2.OwnerId = hpOwner2.Id;
        update new Account[] {dc, dc2};
    }

    @isTest
    static void fseChangeToUpdRepairTest() {
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_Class_GI'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];

        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', Work_Location__c = '北京', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;

        // 病院作成
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner2.Id;
        insert hp;

        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        
        // 診療科を作る
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;

        //Product2 productA = new Product2( Name='テスト商品');
        //insert productA;
        
        //// 価格表エントリを作成する     
        //PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=productA.Id);
        //entry.UnitPrice = 0;
        //entry.IsActive = true;
        //entry.UseStandardPrice = false;
        //entry.CurrencyIsoCode = 'CNY';
        //entry.Product2Id = productA.Id;
        //insert entry;
        
        // 納入機器を作成する
        Asset asset = new Asset();
        asset.Name = 'テスト機器';
        asset.AccountId = dpt.Id;
        asset.Department_Class__c = dc.Id;
        asset.Hospital__c = hp.Id;
        asset.SerialNumber = 'testserial';
        insert asset;

        // 修理を作成する01
        Repair__c repair01 = new Repair__c();
        repair01.Account__c = dpt.Id;
        repair01.Department_Class__c = dc.Id;
        repair01.Hospital__c = hp.Id;
        repair01.Delivered_Product__c = asset.Id;
        repair01.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today().addDays(0);  // 维修合同判断日がサービス契約の中間辺りの日付
        repair01.FSE_Work_Location__c = '上海';
        insert repair01;

        hp.FSE_GI_Main_Leader__c = hpOwner2.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner.Id;
        update hp;
    }
}