import { AdminInterceptsApis, LoginPage } from '@admin-lib/index';
import { OtherSettings } from '../lib/other-settings';
import { UserModal } from '../lib/user-modal';
import { Provider_Type, User_Type } from '@support-onboard/index';
import { HomePage } from 'cypress/e2e/rebranded_home_page/lib/home-page';
import { Timeouts } from '@utility-lib/index';

const newUserCredentials = Cypress.env('newUser').NEW_USER_PASSWORD;

describe('Trips: Profile: Other Settings', () => {
  let userEmail: string;
  beforeEach(() => {
    cy.session('otherSettings', () => {
      LoginPage.createNewCompanyAndLoginWithUserByType({
        providers: [Provider_Type.SABRE_FLIGHT],
        users: [User_Type.ADMIN]
      });
    });
    cy.task('getDataFromCache', 'userEmail').then((email: any) => {
      userEmail = email;
    });
    LoginPage.clearExistingLoginSession();
    UserModal.interceptApis();
    AdminInterceptsApis.interceptAdminsApis();
  });

  beforeEach(() => {
    LoginPage.visitTALoginPage();
    LoginPage.loginWithCredentials(userEmail, newUserCredentials);
    LoginPage.waitForLoadLoginPage();
  });

  it(
    'C127196: Verify language preference can be updated from settings page',
    { tags: ['@tier2'] },
    () => {
      OtherSettings.visitPage();
      OtherSettings.checkLanguage();
      OtherSettings.changeLanguage();
    }
  );
  it(
    'C127195: Verify password can be changed from settings page',
    { tags: ['@tier2'] },
    () => {
      const newPassword = newUserCredentials.split('').reverse().join('');
      OtherSettings.visitPage();
      OtherSettings.changePassword(userEmail, newUserCredentials, newPassword);
      LoginPage.clearExistingLoginSession();
      LoginPage.visitTALoginPage();
      LoginPage.loginWithCredentials(userEmail, newPassword);
      cy.wait(Timeouts.SHORT_TIMEOUT_5_SEC.timeout);
      HomePage.verifyHomePage();
    }
  );
});
