import { BasePage } from '@/manager/shared/base/BasePage';
import { BookingCreateDialog } from '@/manager/modules/booking/views/BookingCreateDialog';
import { BookingDetailsPage } from '@/manager/modules/booking/views/BookingDetailsPage';
import { BookingPriceChangeDialog } from '@/manager/modules/booking/views/BookingPriceChangeDialog';
import { BookingRemovePriceChangeDialog } from '@/manager/modules/booking/views/BookingRemovePriceChangeDialog';
import { DataTable } from '@/manager/modules/ui/data-table/components/DataTable';
import { DebouncedTextField } from '@/manager/shared/components/DebouncedTextField';
import { MultiAutocomplete } from '@/manager/shared/components/MultiAutocomplete';
import { RangeDatePicker } from '@/manager/shared/components/RangeDatePicker';
import { getFilterHost } from '@/manager/shared/utils/locator-utils';

export class BookingListPage extends BasePage {
  readonly actionButton = this.header.getByTestId('booking-list-action-button');
  readonly searchTextField = new DebouncedTextField(getFilterHost(this.body, 'Search'));
  readonly locationAutocomplete = new MultiAutocomplete(getFilterHost(this.body, 'Location'));
  readonly statusAutocomplete = new MultiAutocomplete(getFilterHost(this.body, 'Status').nth(0));
  readonly unitTypeAutocomplete = new MultiAutocomplete(getFilterHost(this.body, 'Unit type'));
  readonly createdAtDatePicker = new RangeDatePicker(getFilterHost(this.body, 'Created at'));
  readonly dataTable = new DataTable(this.body);
  readonly priceChangeActionButton = this.dataTable.rowSelector.getAction('index-adjustment-action');

  async goto(): Promise<void> {
    await this.host.goto('/bookings');
  }

  selectBookings(indexes: number | number[]): Promise<void> {
    return this.dataTable.rowSelector.select(indexes);
  }

  async openBookingPriceChangeDialog(): Promise<BookingPriceChangeDialog> {
    await this.priceChangeActionButton.click();
    return new BookingPriceChangeDialog(this.host);
  }

  async openBookingRemovePriceChangeDialog(): Promise<BookingRemovePriceChangeDialog> {
    await this.dataTable.rowSelector.clickAction('remove-index-adjustment-action');
    return new BookingRemovePriceChangeDialog(this.host);
  }

  async openBookingCreateDialog(): Promise<BookingCreateDialog> {
    await this.dataTable.waitForLoading();
    await Promise.all([this.host.waitForURL('/bookings/create'), this.actionButton.click()]);
    return new BookingCreateDialog(this.host);
  }

  async goToBookingDetailsPage(index: number): Promise<BookingDetailsPage> {
    await Promise.all([this.host.waitForURL('**/bookings/*'), this.dataTable.clickRow(index)]);
    return new BookingDetailsPage(this.host);
  }
}
