import { Utility } from '@utility-lib/index';
import { GroupTravelSettings } from '@admin-lib/settings/overview/group-travel_settings';
import {
  TeamEventsInterceptApis,
  CreateEvent,
  EventDetails,
  EventsPage,
  TeamEventParams,
  EventWithTravelWindowsParams
} from '@group-events-lib/index';
import { stagingUsers } from '@fixtures/staging-users';
import { LoginPage } from '@admin-lib/index';
import { SplitUtility } from '@split-lib/index';
import { User_Type } from '@support-onboard/index';
import { eventData } from '@fixtures/event/event_data';

const eventLocation = 'Zurich';
const eventName = 'Zurich event';
const { adminEmail } = stagingUsers.admin_events_page;
const adminCredentials = Cypress.env('eventPage').EVENT_USER_PASSWORD;

export class EventsWorkflow {
  static eventPreparation(toggle: any, userEmail: string, creds: string) {
    cy.loginWithAuthToken({ email: adminEmail, password: adminCredentials, isSA: true });
    TeamEventsInterceptApis.teamEventIntercept();
    GroupTravelSettings.visitOverviewPage();
    GroupTravelSettings.selectEventApprovalFlow(toggle);
    cy.loginWithAuthToken({ email: userEmail, password: creds, isSA: true });
    EventsWorkflow.createDraftEvent(eventLocation, eventName);
  }

  static createDraftEvent(location: string, eventName: string) {
    CreateEvent.openSearchPage();
    Utility.selectVertical('hotel');
    Utility.openEventsPage();
    EventsPage.clickCreateEvent();
    CreateEvent.selectEventLocation(`${location}`);
    CreateEvent.setEventTitle(`${eventName}`);
    CreateEvent.setEventDates('16', '19');
    CreateEvent.createDraftEvent();
  }

  static createEventWithTravelWindows(params: EventWithTravelWindowsParams): void {
    CreateEvent.openSearchPage();
    Utility.openEventsPage();
    EventsPage.clickCreateEvent();
    CreateEvent.selectEventLocation(params.location);
    CreateEvent.setEventTitle(params.eventName);
    CreateEvent.setEventDates(params.eventStartDate, params.eventEndDate);
    CreateEvent.createDraftEvent();
    EventDetails.verifyTeamEventSetupPageOpened(params.eventName);
    EventDetails.setEventParticipant();
    EventDetails.setTravelWindowDates(params.departureDate, params.returnDate);
    EventDetails.setLatestArrivalTime(params.latestArrivalTime);
    EventDetails.setEarliestReturningTime(params.earliestReturningTime);
    EventDetails.saveChanges();
    EventDetails.sendInvite();
    EventDetails.expectTeamEventCreated();
  }

  static createTeamEvent(params: TeamEventParams) {
    cy.allure().logStep('create new event via api');
    cy.updateSplits();
    SplitUtility.isSplitOn('GROUP_TRAVEL_REFACTOR').then((refactored) => {
      if (refactored) {
        return cy.createGroupEventViaApi({
          eventName: params.eventName,
          locationType: params.locationType,
          inviteeIdsOrEmails: (params.invitees || []).map((invitee) => invitee.email),
          startDate: params.startDate,
          endDate: params.endDate,
          placeId: params.placeId
        });
      } else {
        return cy.createTeamEventViaApi({
          eventName: params.eventName,
          locationType: params.locationType,
          inviteeIdsOrEmails: (params.invitees || []).map((invitee) => invitee.id),
          startDate: params.startDate,
          endDate: params.endDate,
          placeId: params.placeId
        });
      }
    });
  }

  static createCompanyAndTeamEvents() {
    LoginPage.createNewCompanyAndLoginWithUserByType({ users: [User_Type.ADMIN] });
    cy.task('getDataFromCache', 'userResponse').then((response: any) => {
      const teamEventParams: TeamEventParams = {
        eventName: eventData.eventName,
        locationType: eventData.locationType,
        invitees: [{ id: response.uuid, email: response.email }],
        startDate: eventData.startDateTime,
        endDate: eventData.endDateTime,
        placeId: eventData.placeId
      };
      EventsWorkflow.createTeamEvent(teamEventParams);
    });
  }
}
