/*
 * Author: Zhang,Heyang
 * Created Date: 2023/08/07
 * Purpose: TestClass
 *
 * */
@isTest
private class LexNewAndEditRentalApplyPIPLTest{
    public static String raRecordTypeId = Schema.SObjectType.Rental_Apply__c.getRecordTypeInfosByDeveloperName().get('StandardRequest').getRecordTypeId();
    public static String rectCoAg = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
    public static String rectAg = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
    @testSetup
    static void setupTestData() {
        // Id rid = UserInfo.getUserId();
        Profile p = [select id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId()];
        // String sql = 'select ';
        // DescribeSObjectResult objectType = rid.getSobjectType().getDescribe();
        // List<String> objectFields = new List<String>(objectType.fields.getMap().keySet());
        // sql += String.join(objectFields, ',') +' from '+'User'+' where id =\''+rid+'\' limit 1';
        // User thisUser = Database.query(sql);
        User u1 = new User();
        System.runAs(thisUser){
            u1 = new User(Test_staff__c = true);
            u1.LastName = '_サンブリッジ';
            u1.FirstName = 'あZZ';
            u1.Alias = 'あ';
            u1.Email = 'olympusTest01@sunbridge.com';
            u1.Username = 'olympusTest01@sunbridge.com';
            u1.CommunityNickname = 'あ';
            u1.IsActive = true;
            u1.EmailEncodingKey = 'ISO-2022-JP';
            u1.TimeZoneSidKey = 'Asia/Tokyo';
            u1.LocaleSidKey = 'ja_JP';
            u1.LanguageLocaleKey = 'ja';
            u1.ProfileId = p.id;
            u1.Job_Category__c = '销售服务';
            u1.Province__c = '東京';
            u1.ManagerId = thisUser.id;
            u1.Dept__c = '医疗西南营业本部';
            insert u1;
        }
        System.runAs(u1){
            Profile prof1 = [select Id from Profile where Name ='2S3_销售市场管理者'];
            List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
            List<RecordType> rectCo1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 普外科'];
            List<RecordType> rectCo2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 普外科'];
            if (rectCo.size() == 0) {
                return;
            }
            Account account0 = new Account(RecordTypeId = rectCo[0].Id, AgentCode_Ext__c = '9999909', Name = 'olympus0',Is_Active__c = '有効',Grade__c = '一级');
            insert account0;
            Account account1 = new Account(RecordTypeId = rectCo1[0].Id, AgentCode_Ext__c = '9999900', Name = 'olympus1',ParentId = account0.Id,Hospital__c = account0.Id,Department_Class_Label__c = '普外科',Grade__c = '一级');
            insert account1;
            Account account2 = new Account(RecordTypeId = rectCo2[0].Id, AgentCode_Ext__c = '66666', Name = 'olympus2',ParentId = account1.Id,Department_Class__c = account1.Id,Hospital__c = account0.Id,Is_Active__c = '有効',Grade__c = '一级');
            insert account2;
            Account testRectHpAccounts = new Account(Name = 'testRectHpAccount', RecordTypeId = rectCoAg, Hospital__c = account0.Id,Grade__c = '一级');
            insert testRectHpAccounts;
            Contact testContacts = new Contact(email='jplumber@salesforce.com', firstname='Joe', lastname='Plumber',RecordTypeId = rectAg,  AWS_Data_Id__c = 'TestContact', accountId = testRectHpAccounts.id);
            insert testContacts;
            Rental_Apply__c ren = new Rental_Apply__c();
            ren.RecordTypeId = raRecordTypeId;
            ren.Name = 'TestRA';
            ren.Person_In_Charge__c = u1.Id;
            ren.applyUser__c = thisUser.Id;
            ren.Demo_purpose1__c = '产品试用';
            ren.demo_purpose2__c  ='试用（无询价）';
            ren.Product_category__c = 'SP';
            ren.Demo_purpose_text__c = 'test';
            ren.Request_shipping_day__c = System.today();
            ren.direct_send__c = '上门自提';
            ren.pickup_time__c = System.now();
            ren.Loaner_received_staff__c = 'Test';
            ren.Loaner_received_staff_phone__c = '18900092367';
            ren.Hospital__c = account0.Id;
            ren.Strategic_dept__c = account1.Id;
            ren.Account__c =  account2.Id;
            ren.Loaner_medical_Staff__c = testContacts.Id;
            ren.Phone_number__c = '18900092367';
            ren.Combine_Pack__c = 'Tesyt';
            ren.DB_loaner_request__c = '345678o';
            ren.status__c = '已批准';
            ren.direct_send__c = '紧急情况同城自提配送至医院';
            insert ren;
            PI_Policy_Configuration__c config1 = new PI_Policy_Configuration__c(Sobject_Type__c  = 'Rental_Apply__c');
            insert config1;
            PI_Policy_Configuration__c config2 = new PI_Policy_Configuration__c(Sobject_Type__c  = 'Rental_Apply__cV2');
            insert config2;
            PI_Policy_Configuration__c config3 = new PI_Policy_Configuration__c(Sobject_Type__c  = 'Contact');
            insert config3;
            PI_Field_Policy_Detail__c pfpd1 = new PI_Field_Policy_Detail__c(AWS_Field_API__c = 'phone1', SF_Field_API_Name__c = 'Phone1',PI_Policy_Configuration__c = config1.Id, Enable_Encrypt__c=true);
            insert pfpd1;
            PI_Field_Policy_Detail__c pfpd2 = new PI_Field_Policy_Detail__c(AWS_Field_API__c = 'phone2', SF_Field_API_Name__c = 'Phone2',PI_Policy_Configuration__c = config2.Id, Enable_Encrypt__c=true);
            insert pfpd2;
            PI_Field_Policy_Detail__c pfpd3 = new PI_Field_Policy_Detail__c(AWS_Field_API__c = 'phone2', SF_Field_API_Name__c = 'Phone2',PI_Policy_Configuration__c = config3.Id, Enable_Encrypt__c=true);
            insert pfpd3;
        }
    }
    @isTest
    static void tesMethod1(){
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId()];
        System.runAs(thisUser){
            Rental_Apply__c ra = [SELECT Id,Name FROM Rental_Apply__c where RecordTypeId =:raRecordTypeId limit 1];
            String body='{"size":1,"totalSize":1,"done":true,"queryLocator":null,"entityTypeName":"ProfileLayout","records":[{"attributes":{"type":"ProfileLayout","url":"/services/data/v41.0/tooling/sobjects/ProfileLayout/01G1000000MyT9pEAF"},"Layout":{"attributes":{"type":"Layout","url":"/services/data/v41.0/tooling/sobjects/Layout/00h10000006nKGwAAM"},"Name":"备品借出申请页面布局（窗口）","TableEnumOrId":"01I10000000tNjNEAU"},"ProfileId":"00e10000000Y3o5AAC","Profile":{"attributes":{"type":"Profile","url":"/services/data/v41.0/tooling/sobjects/Profile/00e10000000Y3o5AAC"},"Name":"系统管理员"},"RecordTypeId":"01210000000RHIiAAO"}]}';
            Test.setMock(HttpCalloutMock.class, new BaseHttpMock(body,'1','200'));
            System.Test.startTest();
            String record_type_id = raRecordTypeId;
            String objectType = 'Rental_Apply__c';
            List<Metadata.LayoutSection> layout = MetaDataUtility.GetRecordTypePageLayout(record_type_id,objectType);
            LexNewAndEditRentalApplyPIPLController.initData(ra.Id, record_type_id,objectType);
            LexNewAndEditRentalApplyPIPLController.getOrignalRentalApply(ra.Name);
            System.Test.stopTest(); 
        }
    }

    public class BaseHttpMock implements HttpCalloutMock {
        String body ='';
        String status = '';
        String statusCode = '';
        public BaseHttpMock(String body,String status,String statusCode){
            this.body = body;
            this.status = status;
            this.statusCode = statusCode;
        }
        public HTTPResponse respond(HTTPRequest req) {
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setBody(body); 
            res.setStatus('OK');
            res.setStatusCode(200);
            return res;
        }
    }
}