import { BasePage } from '@/manager/shared/base/BasePage';
import { InvoiceBulkMarkAsPaidDialog } from '@/manager/modules/invoice/views/InvoiceBulkMarkAsPaidDialog';
import { InvoiceCreateDialog } from '@/manager/modules/invoice/views/InvoiceCreateDialog';
import { InvoiceDetailsPage } from '@/manager/modules/invoice/views/InvoiceDetailsPage';
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 InvoiceListPage extends BasePage {
  readonly actionButton = this.header.getByTestId('invoice-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'));
  readonly createdAtDatePicker = new RangeDatePicker(getFilterHost(this.body, 'Created at'));
  readonly dataTable = new DataTable(this.body);
  readonly bulkMarkAsPaidActionButton = this.dataTable.rowSelector.getAction('invoice-bulk-mark-as-paid-action');

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

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

  async openInvoiceBulkMarkAsPaidDialog(): Promise<InvoiceBulkMarkAsPaidDialog> {
    await this.bulkMarkAsPaidActionButton.click();
    return new InvoiceBulkMarkAsPaidDialog(this.host);
  }

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

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