import { BookingPlan } from '@/shared/types/booking-plan-types';
import { BookingPlanAddDialog } from '@/manager/modules/ui/booking-plan/views/BookingPlanAddDialog';
import { BookingPlanEditDialog } from '@/manager/modules/ui/booking-plan/views/BookingPlanEditDialog';
import { BookingPlansDeleteDialog } from '@/manager/modules/ui/booking-plan/views/BookingPlansDeleteDialog';
import { DataTable } from '@/manager/modules/ui/data-table/components/DataTable';
import { PriceChangeDialog } from '@/manager/modules/ui/price-change/views/PriceChangeDialog';
import { getCardHost, getCardEditButtonHost, getSnackbarHost } from '@/manager/shared/utils/locator-utils';
import { BaseComponent } from '@/shared/base/BaseComponent';

export class BookingPlanCard extends BaseComponent {
  readonly main = getCardHost(this.host, 'Booking plan');
  readonly addButton = getCardEditButtonHost(this.main);
  readonly dataTable = new DataTable(this.main);
  readonly snackbar = getSnackbarHost(this.host.page(), 'Booking plan(s) successfully updated!');

  async fill(plans: BookingPlan[]): Promise<void> {
    for (let index = 0; index < plans.length; index++) {
      const plan = plans[index];
      const bookingPlanAddDialog = await this.openBookingPlanAddDialog();
      await bookingPlanAddDialog.add(plan);
      await bookingPlanAddDialog.main.waitFor({ state: 'hidden' });
    }
  }

  async openBookingPlanEditDialog(index: number): Promise<BookingPlanEditDialog> {
    await Promise.all([
      this.host.page().waitForURL('**/booking-plan/*'),
      this.dataTable.actionHandler.clickAction(index, 'booking-plan-action-menu-edit'),
    ]);
    return new BookingPlanEditDialog(this.host.page(), true);
  }

  async openPriceChangeDialog(): Promise<PriceChangeDialog> {
    await this.dataTable.rowSelector.clickAction('action-change-price');
    return new PriceChangeDialog(this.host.page());
  }

  async openBookingPlansDeleteDialog(): Promise<BookingPlansDeleteDialog> {
    await this.dataTable.rowSelector.clickAction('booking-plan-row-action-delete');
    return new BookingPlansDeleteDialog(this.host.page());
  }

  async openBookingPlanDeleteDialog(index: number): Promise<BookingPlansDeleteDialog> {
    await this.dataTable.actionHandler.clickAction(index, 'booking-plan-action-menu-remove');
    return new BookingPlansDeleteDialog(this.host.page());
  }

  async publishBookingPlans(publish: boolean): Promise<void> {
    await Promise.all([
      this.snackbar.waitFor(),
      this.dataTable.rowSelector.clickAction(
        publish ? 'booking-plan-row-action-publish' : 'booking-plan-row-action-unpublish'
      ),
    ]);
  }

  async openBookingPlanAddDialog(): Promise<BookingPlanAddDialog> {
    await Promise.all([this.host.page().waitForURL('**/booking-plan'), this.addButton.click()]);
    return new BookingPlanAddDialog(this.host.page(), true);
  }
}
