import { Timeouts } from '@utility-lib/constants';
import { GuestDetails } from '@user-lib/interface/guest-details';
import { UserModal } from '@profile-lib/user-modal';
import { GuestTravelInfo } from '@profile-lib/guest-travel-info';
import { createName } from '@user-lib/factory/guest_user';

const ProfileSelector = {
  profileDelegateInfo: '.ta-profile-delegate-info__personal-delegate',
  clickPickUser: '[class= "ta-user-group-simple__more ng-star-inserted"]',
  pickUserModal: '.ta-modal-header__text-container',
  checkbox: '.ta-checkbox__input',
  personalDelegateInput: '.ta-users-list-simple-modal__search-container',
  personalDelegateCheckbox:
    '[class = "ta-users-list-simple-modal__user ng-star-inserted"] [class = "mat-checkbox ta-checkbox mat-accent ng-untouched ng-pristine ng-valid"]',
  saveDelegate:
    '[class = "ta-button-v2 button button--default button--primary button--regular"]',
  saveChanges: 'ta-sticky-footer .ta-sticky-footer__save-button',
  personalDelegateTag:
    'ta-profile-delegate-info ta-user-group-simple[formcontrolname="myDelegatesUsers"] .ta-user-tag-simple__inner',
  switchTravelerToggle: '.ta-other-travelers__button-toggle',
  createTravelerButton: '.ta-other-travelers__header-add-traveler-icon',
  createProfileButton: '.ta-button__content',
  editButton: '.ta-other-travelers__traveler-row__actions [icon="more-dots"]',
  guestRow: '.ta-other-travelers__traveler-row',
  editTravelerPage: '.ta-profile-sub-header',
  backArrow: '[data-testid= "profileBackArrow"]',
  removeGuestTravelerModal: 'ta-modal[class^="ta-remove-other-traveler-modal"]',
  confirmRemoveGuestButton: '.ta-remove-other-traveler-modal--remove',
  travelerInfoText: '.ta-other-travelers__info-text'
};
export class Profile {
  static profileDelegateInfoText(): void {
    cy.get(ProfileSelector.profileDelegateInfo).should('be.visible');
    cy.get(ProfileSelector.profileDelegateInfo).should(
      'include.text',
      ' These users can see and manage my personal trips '
    );
  }
  static clickPickUser(): void {
    cy.get(ProfileSelector.clickPickUser).click();
    cy.get(ProfileSelector.pickUserModal).should('be.visible');
    cy.get(ProfileSelector.pickUserModal).should(
      'include.text',
      'Who can see my personal trips'
    );
  }

  static setUserPersonalTrip(): void {
    cy.get(ProfileSelector.checkbox).click();
  }

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

  static setPersonalDelegateUser(userFullName: string) {
    cy.allure().logStep('set personal delegate user');
    cy.get(ProfileSelector.personalDelegateCheckbox).click();
    cy.get(ProfileSelector.saveDelegate).click();
    cy.get(ProfileSelector.personalDelegateTag).contains(userFullName).should('exist');
    cy.get(ProfileSelector.saveChanges).contains('Save changes').click();
  }
  static switchGuestTravelerTab(guestType: string): void {
    cy.allure().logStep('Switch to guests tab');
    cy.get(ProfileSelector.switchTravelerToggle).contains(guestType).click();
  }

  static openNewGuestModal(): void {
    cy.allure().logStep('Open new personal guest modal');
    cy.get(ProfileSelector.createTravelerButton).click();
  }

  private static clickOnCreateProfileButton(): void {
    cy.get(ProfileSelector.createProfileButton).contains('Create new profile').click();
  }
  static saveGuestProfile(): void {
    this.clickOnCreateProfileButton();
    cy.wait('@waitForCreateTraveler', Timeouts.MAX_TIMEOUT_120_SEC).then((xhr) => {
      expect(xhr.response?.statusCode, 'Created Traveler').to.equal(200);
    });
  }

  static openEditTravelerModal(givenName: string, familyName: string): void {
    cy.get(ProfileSelector.guestRow).then(($rows) => {
      const userRow = $rows
        .get()
        .filter(($row: any) => $row.innerText.indexOf(`${givenName} ${familyName}`));
      cy.wrap(userRow).within(() => {
        cy.get(ProfileSelector.editButton).click();
      });
      cy.get(ProfileSelector.createProfileButton).contains('Edit profile').click();
    });
    cy.get(ProfileSelector.editTravelerPage).should('be.visible');
  }
  static editTraveler(): void {
    this.clickOnSaveButton();
    cy.wait('@waitForEditTraveler', Timeouts.MAX_TIMEOUT_120_SEC).then((xhr) => {
      expect(xhr.response?.statusCode, 'Created Traveler').to.equal(200);
    });
  }
  private static clickOnSaveButton(): void {
    cy.get(ProfileSelector.createProfileButton).contains(' Save changes ').click();
  }

  static clickBackArrow(): void {
    cy.get(ProfileSelector.backArrow).click();
  }

  static deleteGuestTraveler(givenName: string, familyName: string): void {
    cy.get(ProfileSelector.guestRow).then(($rows) => {
      const userRow = $rows
        .get()
        .filter(($row: any) => $row.innerText.indexOf(`${givenName} ${familyName}`));
      cy.wrap(userRow).within(() => {
        cy.get(ProfileSelector.editButton).click();
      });
      cy.get(ProfileSelector.createProfileButton)
        .contains('Remove from guest list')
        .click();
    });

    cy.get(ProfileSelector.removeGuestTravelerModal).should('be.visible');
    cy.get(ProfileSelector.confirmRemoveGuestButton).click();
  }

  static travelerInfoText(): void {
    cy.get(ProfileSelector.travelerInfoText).should('be.visible');
  }

  static personalGuestTrip(givenName: string, familyName: string): void {
    cy.get(ProfileSelector.guestRow).then(($rows) => {
      const userRow = $rows
        .get()
        .filter(($row: any) => $row.innerText.indexOf(`${givenName} ${familyName}`));
      cy.wrap(userRow).within(() => {
        cy.get(ProfileSelector.editButton).click();
      });
      cy.get(ProfileSelector.createProfileButton).contains('Create new trip').click();
    });
  }
  static createBusinessUser = (guestDetails: GuestDetails) => {
    const {
      givenName = '',
      familyName = '',
      email = '',
      age = 30,
      travelerType = 'Business'
    } = guestDetails;

    UserModal.fillTravelerName(givenName, familyName);
    UserModal.fillBirthday(age);
    UserModal.fillGender('MALE');
    UserModal.fillEmail(email);
    if ('Business' === travelerType) {
      UserModal.checkCostFields();
    }
    UserModal.save();
    GuestTravelInfo.expectGuestToExists({
      email,
      firstName: givenName,
      lastName: familyName
    });
  };

  static createPersonalUser = (guestDetails: GuestDetails) => {
    const { givenName = '', familyName = '', email = '', age = 30 } = guestDetails;

    UserModal.fillTravelerName(givenName, familyName);
    UserModal.fillBirthday(age);
    UserModal.fillEmail(email);
    Profile.saveGuestProfile();
    GuestTravelInfo.expectGuestToExists({
      email,
      firstName: givenName,
      lastName: familyName
    });
  };

  static editPersonalGuest = (guestDetails: GuestDetails) => {
    const { givenName = '', familyName = '', email = '' } = guestDetails;
    Profile.openEditTravelerModal(givenName, familyName);

    const newGivenName = createName(8);
    const newFamilyName = createName(8);

    UserModal.fillTravelerName(newGivenName, newFamilyName);
    UserModal.fillGender('MALE');
    Profile.editTraveler();
    GuestTravelInfo.waitToBeLoaded();
    GuestTravelInfo.waitUpdateList();
    Profile.clickBackArrow();
    GuestTravelInfo.expectGuestToExists({
      email,
      firstName: newGivenName,
      lastName: newFamilyName
    });
  };

  static deletePersonalGuest = (guestDetails: GuestDetails) => {
    const { givenName = '', familyName = '' } = guestDetails;
    Profile.deleteGuestTraveler(givenName, familyName);
    GuestTravelInfo.waitToBeLoaded();
    GuestTravelInfo.waitUpdateList();
    Profile.travelerInfoText();
  };
}
