@isTest
private class SWOControllerTest {
    

    @TestSetup
    static void setup(){
        TestDataUtility.CreatePIPolicyConfigurations(new string[]{'SWO__c','Contact'});
    }
    
    public static User UserD(){
        User user2 = new User();
        user2.LastName = 'test2';
        user2.FirstName = 'test2';
        user2.Alias = 'test2';
        user2.Email = 'test2@test.com';
        user2.Username = 'Xin_Prectech@olympus.com.cn';
        user2.CommunityNickname = 'test2';
        user2.IsActive = true;
        user2.EmailEncodingKey = 'ISO-2022-JP';
        user2.TimeZoneSidKey = 'Asia/Tokyo';
        user2.LocaleSidKey = 'ja_JP';
        user2.LanguageLocaleKey = 'ja';
        user2.ProfileId = '00e28000001sZE6';//System.Label.SystemAdmin;
        insert user2;

        return user2;
    }    

    static testMethod void testMethod1() {

        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        List<RecordType> rectOpp = [select Id from RecordType where IsActive = true and SobjectType = 'Opportunity' and Name = 'SSBD'];
        Id pricebookId = Test.getStandardPricebookId();
        
        Pricebook2 pricebook = new Pricebook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            CurrencyIsoCode = 'CNY'
        );
        insert pricebook;
        
        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.IsActive = true;
        product1.ProductCode = 'product1';
        product1.ProductStatus__c = '1';
        product1.NMPAStatus_one__c = 'Z1';
        product1.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product1.MaterialStatus_one_End__c = Date.today().addDays(22);
        insert product1;

        User_FaultInfo__c uf = new User_FaultInfo__c();
        // uf.Name = 'Test';
        insert uf;

        // User user = new User();
        // user.LastName = 'test3';
        // user.FirstName = 'test3';
        // user.Alias = 'test3';
        // user.Email = 'test3@test.com';
        // user.Username = 'test111@test222.com';
        // user.CommunityNickname = 'test3';
        // user.IsActive = true;
        // user.EmailEncodingKey = 'ISO-2022-JP';
        // user.TimeZoneSidKey = 'Asia/Tokyo';
        // user.LocaleSidKey = 'ja_JP';
        // user.LanguageLocaleKey = 'ja';
        // user.ProfileId = '00e28000001sZE6';//System.Label.SystemAdmin;
        // insert user;

        // User_FaultInfo__c uf = new User_FaultInfo__c();
        // uf.Name = 'Test';
        // insert uf;

        PageReference page = new PageReference('apex/SWOPage?caseId='+uf.Id);
        System.Test.setCurrentPage(page);
        SWOController controller = new SWOController();
        controller.init();
        // controller.swo.REPEAT_REPAIR__c = true;
        controller.swo.QUANTITY__c = 22;
        controller.swo.ESTIMATED_LABOUR_HOURS__c = 22;
        controller.swo.LABOUR_RATE__c = 22;
        controller.swo.OFFICE__c = 'Beijing';
        controller.swo.SWO_PRIORITY__c = 'Medium';
        controller.swo.SWO_STATUS__c = 'Closed';
        controller.swo.TYPE__c = 'Service';
        controller.swo.ASSIGNEDTO__c = UserD().Id;
        controller.swo.DEPARTMENT__c = 'Field Service';
        controller.swo.REASON__c = 'Replacement';
        controller.swo.PRODUCT_TYPE__c = 'NDT';
        controller.swo.ITEM__c = product1.Id;
        // controller.swo.INSTRUMENT_RECEIVE_DATE__c = Datetime.now();
        // controller.swo.ASSIGNED_TO_TECHNICIAN_DATE__c = Datetime.now().addDays(-1);
        // TECHNICIAN_COMPLETION_DATE__c = 
        // Datetime.
        controller.save();



    }

    static testMethod void updateTest() {
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        List<RecordType> rectOpp = [select Id from RecordType where IsActive = true and SobjectType = 'Opportunity' and Name = 'SSBD'];
        Id pricebookId = Test.getStandardPricebookId();
        
        Pricebook2 pricebook = new Pricebook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            CurrencyIsoCode = 'CNY'
        );
        insert pricebook;
        
        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.IsActive = true;
        product1.ProductCode = 'product1';
        product1.ProductStatus__c = '1';
        product1.NMPAStatus_one__c = 'Z1';
        product1.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product1.MaterialStatus_one_End__c = Date.today().addDays(22);
        insert product1;

        User_FaultInfo__c uf = new User_FaultInfo__c();
        // uf.Name = 'Test';
        insert uf;

         List<RecordType> rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        Profile prof = [select Id from Profile where Name ='SSBG DEALER IE'];
        Account dealer = new Account();
        dealer.Name = 'test dealer';
        dealer.RecordTypeId = rectDealer[0].Id;
        dealer.ProductSegment__c = 'BS';
        dealer.PostCode__c='000000';
        insert dealer;
        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=dealer.id);
        insert core;
        User testUser = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = '00e28000001sZE6',TimeZoneSidKey='Asia/Shanghai', UserName='testUser01@prectech.com');

        User user2 = UserD();//new User();
        // user2.LastName = 'test2';
        // user2.FirstName = 'test2';
        // user2.Alias = 'test2';
        // user2.Email = 'test2@test.com';
        // user2.Username = 'Xin_Prectech@olympus.com.cn';
        // user2.CommunityNickname = 'test2';
        // user2.IsActive = true;
        // user2.EmailEncodingKey = 'ISO-2022-JP';
        // user2.TimeZoneSidKey = 'Asia/Tokyo';
        // user2.LocaleSidKey = 'ja_JP';
        // user2.LanguageLocaleKey = 'ja';
        // user2.ProfileId = '00e28000001sZE6';//System.Label.SystemAdmin;
        // insert user2;

        SWO__c swo = new SWO__c();
        swo.Name = 'Test';
        swo.QUANTITY__c = 22;
        swo.ESTIMATED_LABOUR_HOURS__c = 22;
        swo.LABOUR_RATE__c = 22;
        swo.OFFICE__c = 'Beijing';
        swo.SWO_PRIORITY__c = 'Medium';
        swo.SWO_STATUS__c = 'Close Denied';
        swo.TYPE__c = 'Service';
        swo.ASSIGNEDTO__c = user2.Id;
        swo.DEPARTMENT__c = 'Field Service';
        swo.REASON__c = 'Replacement';
        swo.PRODUCT_TYPE__c = 'NDT';
        swo.ITEM__c = product1.Id;
        swo.INSTRUMENT_RECEIVE_DATE__c = Datetime.now();
        swo.INSTRUMENT_SHIP_DATE__c = Datetime.now().addDays(1);
        swo.DIAGNOSIS_START__c = Datetime.now();
        swo.DIAGNOSIS_END__c = Datetime.now().addDays(1);
        swo.PO_RECEIVE_DATE__c = Datetime.now();
        swo.Quotation_send__c = Datetime.now().addDays(1);
        swo.REPAIR_START__c = Datetime.now();
        swo.REPAIR_END__c = Datetime.now().addDays(1);

        swo.CALIBRATION_START__c = Datetime.now();
        swo.CALIBRATION_END__c = Datetime.now().addDays(1);

        swo.FQC_START__c = Datetime.now();
        swo.FQC_end__c = Datetime.now().addDays(1);

        swo.Repair_DateTime__c = Datetime.now();
        swo.DELIVERED_TO_SERVICE__c = Datetime.now().addDays(1);
        swo.CASE_NUMBER__c = uf.Id;

        // swo.REPAIR_START__c = Datetime.now();
        // swo.REPAIR_END__c = Datetime.now().addDays(1);

        insert swo;

        Quotes__c quotes = new Quotes__c();
        quotes.SWO__c = swo.Id;
        quotes.QuotesType__c = '零件报价单';
        quotes.DATE__c = Date.today();
        quotes.LEAD_TIME__c = '1 week';
        quotes.STATUS__c = 'Qualifying';
        quotes.LOCATION__c = 'China - Service';
        quotes.TERMS__c = '1% 10 Net 30';
        quotes.EXPIRES__c = Date.today();
        quotes.EXP_CLOSE__c = Date.today();
        quotes.ORDER_TYPE__c = 'LS Core Clinical';

        insert quotes;

        Diagnosis_Part__c diagnosisPart = new Diagnosis_Part__c();
        diagnosisPart.SWO__c = swo.Id;
        insert diagnosisPart;

        Mail_Merge__c  mailMerge = new Mail_Merge__c(); 
        mailMerge.SWO__c = swo.Id;
        insert mailMerge;
        Attachment att = new Attachment(
            Name = 'ORDER-123456.pdf',
            parentId = mailMerge.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att;

        Test.startTest();
        PageReference page = new PageReference('apex/SWOPage?id='+swo.Id +'&type=Product');
        System.Test.setCurrentPage(page);
        SWOController controller = new SWOController(new ApexPages.StandardController(swo));
        controller.init();
        controller.save();
        controller.sendEmail(swo.ASSIGNEDTO__c);

        controller.init();
        // Integer num = Integer.valueOf('Test--->'+controller.SwoStatus);
        // controller.SwoStatus = '1111';
        controller.swo.SWO_STATUS__c = 'Quotation for approval';
        controller.swo.ASSIGNEDTO__c = user2.Id;
        controller.save();

        controller.init();
        swo.SWO_STATUS__c = 'Quotation approved';
        controller.save();

        controller.init();
        swo.SWO_STATUS__c = 'Ready for Quote';
        controller.save();

        controller.init();
        swo.SWO_STATUS__c = 'Parts Hold';
        controller.save();

        controller.init();
        swo.SWO_STATUS__c = 'Delivered to Technician';
        controller.save();

        controller.init();
        swo.SWO_STATUS__c = 'Ready to Quote';
        controller.save();

        controller.init();
        swo.SWO_STATUS__c = 'Ready for Sales order';
        controller.save();

        controller.init();
        swo.SWO_STATUS__c = 'Repair';
        controller.save();

        controller.init();
        swo.SWO_STATUS__c = 'In Queue';
        controller.save();


        controller.setMail();
        controller.setFault();
        controller.setCase();
        controller.setProduct();
        controller.setQuotes();
        controller.setRepair();
        controller.setTracking();
        controller.setTechnical();
        controller.setEstimation();
        controller.setGeneral();
        controller.setIncoming();
        controller.setOutgoing();
        controller.setDiagnosis();

        controller.uploadFile();
        controller.edit();
        controller.addDiagnosisInfo();
        controller.openPageSave();
        controller.repairReturn();
		Test.stopTest();
    }

    
    static testMethod void testMethod3() {

        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        List<RecordType> rectOpp = [select Id from RecordType where IsActive = true and SobjectType = 'Opportunity' and Name = 'SSBD'];
        Id pricebookId = Test.getStandardPricebookId();
        
        Pricebook2 pricebook = new Pricebook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            CurrencyIsoCode = 'CNY'
        );
        insert pricebook;
        
        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.IsActive = true;
        product1.ProductCode = 'product1';
        product1.ProductStatus__c = '1';
        product1.NMPAStatus_one__c = 'Z1';
        product1.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product1.MaterialStatus_one_End__c = Date.today().addDays(22);
        insert product1;

        User_FaultInfo__c uf = new User_FaultInfo__c();
        // uf.Name = 'Test';
        insert uf;

        // User user = new User();
        // user.LastName = 'test';
        // user.FirstName = 'test';
        // user.Alias = 'test';
        // user.Email = 'test@test.com';
        // user.Username = 'test111@test222.com';
        // user.CommunityNickname = 'test';
        // user.IsActive = true;
        // user.EmailEncodingKey = 'ISO-2022-JP';
        // user.TimeZoneSidKey = 'Asia/Tokyo';
        // user.LocaleSidKey = 'ja_JP';
        // user.LanguageLocaleKey = 'ja';
        // user.ProfileId = '00e28000001sZE6';//System.Label.SystemAdmin;
        // insert user;


        PageReference page = new PageReference('apex/SWOPage?caseId='+uf.Id);
        System.Test.setCurrentPage(page);
        SWOController controller = new SWOController();
        controller.init();

        controller.save();



    }

}