import { Timeouts } from '@utility-lib/index';

const delegateInfoSelector = {
  delegatesCheckbox: 'ta-profile-delegate-info [formcontrolname="myDelegatesCheckbox"]',
  delegatesInput:
    'ta-profile-delegate-info ta-user-group-simple[formcontrolname="myDelegatesUsers"] ta-user-search-simple[qaid="delegateUserGroup"] input',
  delegateInfoSection: 'ta-profile-delegate-info',
  userSearchPopoverList: '.ta-user-search-simple-popover__list',
  delegateTag:
    'ta-profile-delegate-info ta-user-group-simple[formcontrolname="myDelegatesUsers"] .ta-user-tag-simple__inner',
  saveChangesButton: 'ta-sticky-footer .ta-sticky-footer__save-button',
  loader: '[viewBox="0 0 50 50"]'
};

export class DelegateInfo {
  static visitProfileContactInfo() {
    cy.allure().logStep('visit profile page contact tab');
    cy.visit('app/user2/profile?tab=contact');
    cy.wait('@waitForPassengerInfo');
    cy.wait('@waitForDelegatesInfo');
    cy.get(delegateInfoSelector.loader, Timeouts.MEDIUM_TIMEOUT_60_SEC).should(
      'not.exist'
    );
    cy.get(
      delegateInfoSelector.delegateInfoSection,
      Timeouts.MEDIUM_TIMEOUT_30_SEC
    ).should('be.visible');
    cy.url().should('include', '/profile?tab=contact', Timeouts.SHORT_TIMEOUT_3_SEC);
  }

  static getDataFromCachedAdminUserAndSetHimAsDelegatedUser() {
    cy.task('getDataFromCache', 'userResponse').then((response: any) => {
      this.setDelegateUser(response.fullName);
    });
  }

  static setDelegateUser(userFullName: string) {
    cy.allure().logStep('set delegate user');
    cy.get(delegateInfoSelector.delegatesCheckbox).click();
    cy.get(delegateInfoSelector.delegatesInput).type(userFullName);
    cy.get(delegateInfoSelector.delegateInfoSection).scrollIntoView();
    cy.wait(Timeouts.SHORT_TIMEOUT_3_SEC.timeout);
    cy.get(delegateInfoSelector.userSearchPopoverList).contains(userFullName).click();
    cy.get(delegateInfoSelector.delegateTag).contains(userFullName).should('exist');
    cy.get(delegateInfoSelector.saveChangesButton).contains('Save changes').click();
    cy.wait('@waitForDelegatesInfoSave');
  }
}
