import { BookingBillingPeriodSection } from '@/portal/modules/booking/components/BookingBillingPeriodSection';
import { BookingMoveInDateSection } from '@/portal/modules/booking/components/BookingMoveInDateSection';
import { BookingInsuranceSection } from '@/portal/modules/booking/components/BookingInsuranceSection';
import { BookingCreateUnitTypeBilling } from '@/portal/modules/booking/booking-types';
import { BookingBasketView } from '@/portal/modules/booking/views/BookingBasketView';
import { BookingSizeNotAvailableView } from '@/portal/modules/booking/views/BookingSizeNotAvailableView';
import { BaseView } from '@/shared/base/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: BookingCreateUnitTypeBilling, insurance?: string): Promise<void> {
    await this.moveInDateSection.select(moveInDate);
    await this.billingPeriodSection.select(billingPeriod);

    if (insurance) {
      await this.insuranceSection.select(insurance);
    }
  }

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

    if (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);
  }
}
