/*
 * Author: Guo, Aline Yaying
 * Created Date: 03/22/2022
 * Purpose: Test Class
 * History:
 *      03/22/2022 - Guo, Aline Yaying - Initial Code.
 *
 * */
@isTest
private class NewAndEditLeadControllerTest {
  @TestSetup
  static void makeData() {
    List<String> strList = new List<String>();
    strList.add('Lead');
    strList.add('Contact');
    strList.add('Inquiry_form__c');
    strList.add('LeadV2');
    TestDataUtility.CreatePIPolicyConfigurations(strList);
  }
  static testMethod void testMethod1() {
    Test.setMock(
      HttpCalloutMock.class,
      new TestDataUtility.CreateMetaDataUtilityHttpMock()
    );
    Test.startTest();
    List<RecordType> rectCo = [
      SELECT Id
      FROM RecordType
      WHERE IsActive = TRUE AND SobjectType = 'Account' AND Name = '病院'
    ];
    if (rectCo.size() == 0) {
      return;
    }
    List<RecordType> rectAcc = [
      SELECT Id
      FROM RecordType
      WHERE
        IsActive = TRUE
        AND SobjectType = 'Account'
        AND Name = '戦略科室分類 呼吸科'
    ];
    if (rectAcc.size() == 0) {
      return;
    }
    Account accountItem1 = new Account(
      Name = 'testaccount001',
      Dealer_discount__c = 10,
      RecordTypeId = rectCo[0].Id
    );
    insert accountItem1;
    Account accountItem3 = new Account(
      Name = 'testaccount001',
      Dealer_discount__c = 10,
      RecordTypeId = rectAcc[0].Id,
      ParentId = accountItem1.Id
    );
    insert accountItem3;
    Account accountItem2 = new Account(
      Name = 'testaccount002',
      Dealer_discount__c = 10,
      RecordTypeId = rectCo[0].Id,
      Hospital__c = accountItem1.Id,
      Department_Class__c = accountItem3.Id
    );
    insert accountItem2;

    Contact contact2 = new Contact();
    contact2.AccountId = accountItem2.Id;
    //contact2.FirstName = '責任者';
    contact2.LastName = 'test1经销商';
    contact2.AWS_Data_Id__c = 'Test123';
    insert contact2;

    Lead leadTest = new Lead();
    leadTest.LastName = 'test';
    leadTest.Company = 'test';
    leadTest.hospital__c = accountItem2.Hospital__c;
    leadTest.Contact_Name__c = contact2.Id;
    insert leadTest;

    Inquiry_form__c inquiryform = new Inquiry_form__c();
    inquiryform.Name = '2023062001';
    inquiryform.Request1__c = '需要报价';
    inquiryform.Product1__c = '超声';
    inquiryform.Hospital__c = accountItem2.Hospital__c;
    inquiryform.Contact_Name__c = contact2.Id;

    inquiryform.Opportunity_Division__c = '询价';
    insert inquiryform;

    String contactDataId = contact2.AWS_Data_Id__c;

    Boolean isClone = true;
    Boolean isNew = true;
    Map<string, string> AWSToSobjectEncryptedMap = new Map<String, String>();
    String AWSToSobjectEncryptedMapJson = JSON.serialize(
      AWSToSobjectEncryptedMap
    );
    PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo(
      'Lead'
    );
    for (PI_Field_Policy_Detail__c PIDetail : piIntegration.PIDetails) {
      AWSToSobjectEncryptedMap.put(
        PIDetail.AWS_Field_API__c,
        PIDetail.SF_Field_API_Name__c
      );
    }
    Map<string, string> mso = ApexPages.currentPage().getParameters();
    mso.put('newclone', 'newclone');
    mso.put('CF00N1000000962np_lkid', inquiryform.Id);
    mso.put('CF00N10000006ps6f_lkid', contact2.Id);
    mso.put('CF00N10000002CvC5_lkid', accountItem2.Hospital__c);
    mso.put('CF00N10000002CvC5', 'Company');
    mso.put('LeadSource', 'LeadSource');

    String InquiryFormId = mso.get('CF00N1000000962np_lkid');
    String contactId = mso.get('CF00N10000006ps6f_lkid');
    Boolean isNewMode = false;
    String recordId = leadTest.Id;
    String rid = '';
    ApexPages.StandardController con = new ApexPages.StandardController(
      leadTest
    );
    NewAndEditLeadController lead = new NewAndEditLeadController(con);
    Inquiry_form__c ifc = lead.ifc;
    String token = lead.awsToken;
    String AWSDataId = lead.AWSDataId;
    String AWSDataIdInquiryForm = lead.AWSDataIdInquiryForm;
    contactId = lead.contactId; //For Lookup field
    String contactsInfo = lead.contactsInfo; //key sfid;value awsid
    Map<String, String> sfIdToAWSIdMap = new Map<String, String>();

    SObject obj = con.getRecord();
    obj.put('Hospital_Name__c', mso.get('CF00N10000002CvC5_lkid'));
    String hospitalId = mso.get('CF00N10000002CvC5_lkid');
    if (obj.Id != null) {
      Lead leadData = [
        SELECT
          Id,
          RecordTypeId,
          AWS_Data_Id__c,
          Contact_Name__r.Id,
          Contact_Name__r.AWS_Data_Id__c
        FROM Lead
        WHERE id = :obj.Id
      ];
      String rtTypeId = leadData.RecordTypeId;
      AWSDataId = leadData.AWS_Data_Id__c;
      if (leadData.Contact_Name__r.Id != null) {
        sfIdToAWSIdMap.put(
          String.valueof(leadData.Contact_Name__r.Id).subString(0, 15),
          leadData.Contact_Name__r.AWS_Data_Id__c
        );
      }
      contactsInfo = JSON.serialize(sfIdToAWSIdMap);
    }

    String leadJson = '{"RecordTypeId":"01210000000QiRaAAK","Lead_No__c":"IN-JS-2022040127183","Status":"未跟进","Hospital_Name__c":"0011000000VAPEt","Department_Class__c":"0011000000V9bh5","Salutation":"","LastName":"***","Contact_Name__c":"0031m00000DHrkV","Cancel_Reason__c":"","Phone":"","Opp_Name_Search__c":"000000000000000","Email":"","Opp_Name__c":"Test","owner_not_automatically_update__c":false,"Purchase_Plan__c":"有(无预算)","Lead_Inquiry_form__c":"000000000000000","Opportunity_Division__c":"询价","LeadSource":"电话","Campaign__c":"000000000000000","urgent__c":false,"SI_PromoteInquiry__c":false,"Request__c":"","Request_Detail__c":"","Opportunity_stage__c":"预算没有批准","Close_Forecasted_Date__c":"2022/04/01","Purchase_Reason__c":"更新","Sales_Method__c":"单一来源采购","Fund_Basis__c":"銀行資金","Purchase_Type__c":"一般引合","Company":"江苏省人民医院 呼吸科 呼吸科","Name_Encrypted__c":"48a2693f6bf73d16e0abd48caf7fb756","Phone_Encrypted__c":"","Email_Encrypted__c":"","AWS_Data_Id__c":"959584917346320385"}';
    NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', false);
    NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', true);
    NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', isNew);

    Test.stopTest();
  }

  static testMethod void testMethod2() {
    Test.setMock(
      HttpCalloutMock.class,
      new TestDataUtility.CreateMetaDataUtilityHttpMock()
    );
    Test.startTest();
    List<RecordType> rectCo = [
      SELECT Id
      FROM RecordType
      WHERE IsActive = TRUE AND SobjectType = 'Account' AND Name = '病院'
    ];
    if (rectCo.size() == 0) {
      return;
    }
    List<RecordType> rectAcc = [
      SELECT Id
      FROM RecordType
      WHERE
        IsActive = TRUE
        AND SobjectType = 'Account'
        AND Name = '戦略科室分類 呼吸科'
    ];
    if (rectAcc.size() == 0) {
      return;
    }
    Account accountItem1 = new Account(
      Name = 'testaccount001',
      Dealer_discount__c = 10,
      RecordTypeId = rectCo[0].Id
    );
    insert accountItem1;
    Account accountItem3 = new Account(
      Name = 'testaccount001',
      Dealer_discount__c = 10,
      RecordTypeId = rectAcc[0].Id,
      ParentId = accountItem1.Id
    );
    insert accountItem3;
    Account accountItem2 = new Account(
      Name = 'testaccount002',
      Dealer_discount__c = 10,
      RecordTypeId = rectCo[0].Id,
      Hospital__c = accountItem1.Id,
      Department_Class__c = accountItem3.Id
    );
    insert accountItem2;

    Contact contact2 = new Contact();
    contact2.AccountId = accountItem2.Id;
    //contact2.FirstName = '責任者';
    contact2.LastName = 'test1经销商';
    contact2.AWS_Data_Id__c = 'Test123';
    insert contact2;

    Lead leadTest = new Lead();
    leadTest.LastName = 'test';
    leadTest.Company = 'test';
    leadTest.hospital__c = accountItem2.Hospital__c;
    leadTest.Contact_Name__c = contact2.Id;
    insert leadTest;

    Inquiry_form__c inquiryform = new Inquiry_form__c();
    inquiryform.Name = '2023062001';
    inquiryform.Request1__c = '需要报价';
    inquiryform.Product1__c = '超声';
    inquiryform.Hospital__c = accountItem2.Hospital__c;
    inquiryform.Contact_Name__c = contact2.Id;

    inquiryform.Opportunity_Division__c = '询价';
    insert inquiryform;

    String contactDataId = contact2.AWS_Data_Id__c;

    Boolean isClone = true;
    Boolean isNew = true;
    Map<string, string> AWSToSobjectEncryptedMap = new Map<String, String>();
    String AWSToSobjectEncryptedMapJson = JSON.serialize(
      AWSToSobjectEncryptedMap
    );
    PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo(
      'Lead'
    );
    for (PI_Field_Policy_Detail__c PIDetail : piIntegration.PIDetails) {
      AWSToSobjectEncryptedMap.put(
        PIDetail.AWS_Field_API__c,
        PIDetail.SF_Field_API_Name__c
      );
    }
    Map<string, string> mso = ApexPages.currentPage().getParameters();
    mso.put('newclone', 'newclone');
    mso.put('CF00N1000000962np_lkid', null);
    mso.put('CF00N10000006ps6f_lkid', contact2.Id);
    mso.put('CF00N10000002CvC5_lkid', accountItem2.Hospital__c);
    mso.put('CF00N10000002CvC5', 'Company');
    mso.put('LeadSource', 'LeadSource');

    String InquiryFormId = mso.get('CF00N1000000962np_lkid');
    String contactId = mso.get('CF00N10000006ps6f_lkid');
    Boolean isNewMode = false;
    String recordId = leadTest.Id;
    String rid = '';
    ApexPages.StandardController con = new ApexPages.StandardController(
      leadTest
    );
    NewAndEditLeadController lead = new NewAndEditLeadController(con);
    Inquiry_form__c ifc = lead.ifc;
    String token = lead.awsToken;
    String AWSDataId = lead.AWSDataId;
    String AWSDataIdInquiryForm = lead.AWSDataIdInquiryForm;
    String contactsInfo = lead.contactsInfo; //key sfid;value awsid
    Map<String, String> sfIdToAWSIdMap = new Map<String, String>();
    SObject obj = con.getRecord();
    obj.put('Hospital_Name__c', mso.get('CF00N10000002CvC5_lkid'));
    String hospitalId = mso.get('CF00N10000002CvC5_lkid');

    String leadJson = '{"RecordTypeId":"01210000000QiRaAAK","Lead_No__c":"IN-JS-2022040127183","Status":"未跟进","Hospital_Name__c":"0011000000VAPEt","Department_Class__c":"0011000000V9bh5","Salutation":"","LastName":"***","Contact_Name__c":"0031m00000DHrkV","Cancel_Reason__c":"","Phone":"","Opp_Name_Search__c":"000000000000000","Email":"","Opp_Name__c":"Test","owner_not_automatically_update__c":false,"Purchase_Plan__c":"有(无预算)","Lead_Inquiry_form__c":"000000000000000","Opportunity_Division__c":"询价","LeadSource":"电话","Campaign__c":"000000000000000","urgent__c":false,"SI_PromoteInquiry__c":false,"Request__c":"","Request_Detail__c":"","Opportunity_stage__c":"预算没有批准","Close_Forecasted_Date__c":"2022/04/01","Purchase_Reason__c":"更新","Sales_Method__c":"单一来源采购","Fund_Basis__c":"銀行資金","Purchase_Type__c":"一般引合","Company":"江苏省人民医院 呼吸科 呼吸科","Name_Encrypted__c":"48a2693f6bf73d16e0abd48caf7fb756","Phone_Encrypted__c":"","Email_Encrypted__c":"","AWS_Data_Id__c":"959584917346320385"}';
    NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', false);
    NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', true);
    NewAndEditLeadController.saveLead(leadJson, 'avgwshDFcxAS', isNew);
 
    Test.stopTest();
  }

  static testMethod void testMethod3() {
    Lead leadTest = new Lead();
    String url = ApexPages.currentPage()
      .getParameters()
      .put('CF00N10000006ps6f_lkid', '000000000000000');
    url = ApexPages.currentPage()
      .getParameters()
      .put('RecordType', '01210000000QiRf');
    Test.startTest();
    try {
      NewAndEditLeadController.ControllerUtil();
      ApexPages.StandardController con = new ApexPages.StandardController(
        leadTest
      );
      NewAndEditLeadController lc = new NewAndEditLeadController(con);
      String jsonString = '["Department_Class__c"]';
      system.debug('jsonString:' + JSON.deserializeUntyped(jsonString));
      String token = lc.awsToken;
      String AWSDataId = lc.AWSDataId;
      String AWSDataIdInquiryForm = lc.AWSDataIdInquiryForm;
      String contactId = lc.contactId; //For Lookup field
      String contactsInfo = lc.contactsInfo; //key sfid;value awsid
      Inquiry_form__c ifc = lc.ifc;
      NewAndEditLeadController.queryAccount(jsonString, '000000000000000');
    } catch (Exception e) {
      system.debug('Exception from query account:' + e.getMessage());
    }
    Test.stopTest();
  }
}