import { BookingBillingPeriodSection } from '@/apps/portal/modules/booking/components/BookingBillingPeriodSection';
import { BookingMoveInDateSection } from '@/apps/portal/modules/booking/components/BookingMoveInDateSection';
import { BookingInsuranceSection } from '@/apps/portal/modules/booking/components/BookingInsuranceSection';
import { IBookingCreateUnitTypeBilling } from '@/apps/portal/modules/booking/types/booking-entity-types';
import { BookingBasketView } from '@/apps/portal/modules/booking/views/BookingBasketView';
import { BookingSizeNotAvailableView } from '@/apps/portal/modules/booking/views/BookingSizeNotAvailableView';
import { BaseView } from '@/shared/modules/base/views/BaseView';

export class BookingOptionsView extends BaseView {
  readonly moveInDateSection = new BookingMoveInDateSection(this.host.getByTestId('move-in-date-section'));
  readonly billingPeriodSection = new BookingBillingPeriodSection(this.host.getByTestId('billing-period-section'));
  readonly insuranceSection = new BookingInsuranceSection(this.host.getByTestId('insurance-section'));
  readonly toBasketButton = this.host.locator('[data-test-id="to-basket-button"]:not(.cursor-wait)');

  async select(moveInDate: Date, billingPeriod: IBookingCreateUnitTypeBilling, insurance?: string): Promise<void> {
    await this.moveInDateSection.select(moveInDate);
    await this.billingPeriodSection.select(billingPeriod);
    insurance && (await this.insuranceSection.select(insurance));
  }

  async selectWithoutMoveInDate(billingPeriod: IBookingCreateUnitTypeBilling, insurance?: string): Promise<void> {
    await this.billingPeriodSection.select(billingPeriod);
    insurance && (await this.insuranceSection.select(insurance));
  }

  async failSelect(moveInDate: Date): Promise<BookingSizeNotAvailableView> {
    await Promise.all([
      this.host.waitForURL('**/unit-type/*/options/not-available/*'),
      this.moveInDateSection.select(moveInDate),
    ]);
    return new BookingSizeNotAvailableView(this.host);
  }

  async confirm(): Promise<BookingBasketView> {
    await Promise.all([this.host.waitForURL('**/unit-type/*/options/basket?lang=en'), this.toBasketButton.click()]);
    return new BookingBasketView(this.host);
  }
}
