/**
 * 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 SyncProduct2Test {

    static testMethod void myUnitTest() {
        List<Product2> prs = new List<Product2>();
        
        Product2 pr1 = new Product2(
            Name = 'aiueo商品1_name',
            ProductCode = 'OT1',
            Asset_Model_No__c = 'aiueo商品12345678901234567890123456789012345678901234567890123456789012345678901234567890',
            IsActive = true,
            SFDA_Status__c = '有効'
        );
        prs.add(pr1);
        Product2 pr2 = new Product2(
            Name = 'aiueo商品2_name',
            ProductCode = 'OT2',
            Asset_Model_No__c = 'aiueo商品2',
            IsActive = false,              // 同期されない
            SFDA_Status__c = '不要'
        );
        prs.add(pr2);
        Product2 pr3 = new Product2(
            Name = 'aiueo商品3_name',
            ProductCode = 'OT3',
            Asset_Model_No__c = 'aiueo商品3',
            IsActive = true,
            SFDA_Status__c = '準備中'
        );
        prs.add(pr3);
        insert prs;
        
        Product_Set__c ps1 = new Product_Set__c();
        ps1.Name = 'セット品１';
        ps1.Product_Set_CD__c = '111111';
        ps1.Description__c = '説明';
        ps1.Qty__c = 10;
        //ps1.Valid__c = true;
        ps1.Valid_Date__c = date.today().addDays(-5);
        insert ps1;

        Product_Set_Detail__c psd1 = new Product_Set_Detail__c();
        psd1.Product_Set__c = ps1.id;
        psd1.Price__c = 100;
        psd1.Quantity__c = 10;
        psd1.Product__c = pr1.id;
        insert psd1;

        Product_Set_Detail__c psd2 = new Product_Set_Detail__c();
        psd2.Product_Set__c = ps1.id;
        psd2.Price__c = 100;
        psd2.Quantity__c = 10;
        psd2.Product__c = pr3.id;
        insert psd2;

        List<Product2__c> prcs = [select Id from Product2__c where Product2__c in :prs];
        System.assertEquals(2, prcs.size());
        
        pr3 = [select Id from Product2 where Id = :pr3.Id];
        pr3.SFDA_Status__c = '停止';
        update pr3;
        prcs = [select Id from Product2__c where Product2__c in :prs];
        System.assertEquals(2, prcs.size());
        
        pr2 = [select Id from Product2 where Id = :pr2.Id];
        pr2.IsActive = true;
        update pr2;
        prcs = [select Id, Name, Name_Text__c, OT_CODE_Text__c from Product2__c where Product2__c in :prs order by Product2__c.Name];
        System.assertEquals(3, prcs.size());
        System.assertEquals('aiueo商品1234567890123456789012345678901234567890123456789012345678901234567890123', prcs[0].Name);
        System.assertEquals('aiueo商品2', prcs[1].Name);
        System.assertEquals('aiueo商品3', prcs[2].Name);
        System.assertEquals('aiueo商品1_name', prcs[0].Name_Text__c);
        System.assertEquals('aiueo商品2_name', prcs[1].Name_Text__c);
        System.assertEquals('aiueo商品3_name', prcs[2].Name_Text__c);
        System.assertEquals('OT1', prcs[0].OT_CODE_Text__c);
        System.assertEquals('OT2', prcs[1].OT_CODE_Text__c);
        System.assertEquals('OT3', prcs[2].OT_CODE_Text__c);
        
        pr1.SFDA_Status__c = '新製品申請中';
        pr2.SFDA_Status__c = '準備中';
        pr3.SFDA_Status__c = '有効（再申請中）';
        update new Product2[] {pr1, pr2, pr3};
        prcs = [select Id from Product2__c where Product2__c in :prs];
        System.assertEquals(3, prcs.size());
        
        //delete pr3;
        prcs = [select Id from Product2__c where Product2__c in :prs];
        System.assertEquals(3, prcs.size());

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        Account company1 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = '病院テスト1';
        List<Account> hps = new Account[]{company1};
        insert hps;
          
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        
        insert new Account[] {depart1};

         Asset ast1 = new Asset();
         ast1.SerialNumber = 'abcdefg';
         ast1.Name = '测试资产1';
         ast1.AccountId              = depart1.Id;
         ast1.Department_Class__c    = dc1s[0].Id;
         ast1.Hospital__c            = company1.Id;
         ast1.Product2Id             = pr1.Id;
         ast1.Guarantee_period_for_products__c = Date.today();
         ast1.InstallDate                      = Date.today();
         //ast1.Manage_type__c = '个体管理';
         ast1.FirstApproveDate_old__c = Date.today().addDays(-30);
         ast1.Loaner_accsessary__c = true;
         insert ast1;

         Asset ast2 = new Asset();
         ast2.SerialNumber = '12345';
         ast2.Name = '测试资产2';
         ast2.AccountId              = depart1.Id;
         ast2.Department_Class__c    = dc1s[0].Id;
         ast2.Hospital__c            = company1.Id;
         ast2.Product2Id             = pr2.Id;
         ast2.Guarantee_period_for_products__c = Date.today();
         ast2.InstallDate                      = Date.today();
         //ast2.Manage_type__c = '个体管理';
         ast2.IS_Extend_Gurantee_Txt__c = true;
         ast2.Loaner_accsessary__c = true;
         insert ast2;

        pr1.Extend_new_product_gurantee__c = true;
        pr1.Extend_Gurantee_Start__c = Date.today().addDays(-60);
        pr1.Extend_Gurantee_End__c = Date.today().addDays(60);
        update pr1;

        pr2.Extend_new_product_gurantee__c = false;
        update pr2;        
    }

    static testMethod void deleteUnitTest() {
        Product2 pr1 = new Product2(
            Name = 'aiueo商品1_name',
            ProductCode = 'OT1',
            Asset_Model_No__c = 'aiueo商品12345678901234567890123456789012345678901234567890123456789012345678901234567890',
            IsActive = true,
            SFDA_Status__c = '有効'
        );
        insert pr1;

        StaticParameter.EscapeSyncProduct2Trigger = true;
        update pr1;
        StaticParameter.EscapeSyncProduct2Trigger = false;
        Product2 pr2 = new Product2(
            Name = 'aiueo商品2_name',
            ProductCode = 'OT2',
            Asset_Model_No__c = 'aiueo商品2',
            IsActive = true,              // 同期されない
            SFDA_Status__c = '不要'
        );
        insert pr2;

        Oly_TriggerHandler.bypass('PowerBIBaseHandler');

        delete pr2;
    }
}