import { ProductOneTimeCreateDialog } from '@/manager/modules/product/views/ProductOneTimeCreateDialog';
import { ProductOneTimeDetailsPage } from '@/manager/modules/product/views/ProductOneTimeDetailsPage';
import { ProductRecurringCreateDialog } from '@/manager/modules/product/views/ProductRecurringCreateDialog';
import { ProductRecurringDetailsPage } from '@/manager/modules/product/views/ProductRecurringDetailsPage';
import { DataTable } from '@/manager/modules/ui/data-table/components/DataTable';
import { PriceChangeDialog } from '@/manager/modules/ui/price-change/views/PriceChangeDialog';
import { BasePage } from '@/manager/shared/base/BasePage';
import { DebouncedTextField } from '@/manager/shared/components/DebouncedTextField';
import { MultiAutocomplete } from '@/manager/shared/components/MultiAutocomplete';
import { getFilterHost } from '@/manager/shared/utils/locator-utils';

export class ProductListPage extends BasePage {
  readonly actionMenuButton = this.header.getByTestId('product-list-action-button');
  readonly actionMenu = this.host.getByTestId('product-list-action-menu');
  readonly actionMenuCreateOneTimeItem = this.actionMenu.getByTestId('product-list-action-create-one-time');
  readonly actionMenuCreateRecurringItem = this.actionMenu.getByTestId('product-list-action-create-recurring');
  readonly searchTextField = new DebouncedTextField(getFilterHost(this.body, 'Search'));
  readonly locationAutocomplete = new MultiAutocomplete(getFilterHost(this.body, 'Location'));
  readonly frequencyAutocomplete = new MultiAutocomplete(getFilterHost(this.body, 'Frequency'));
  readonly dataTable = new DataTable(this.body);
  readonly priceChangeActionButton = this.dataTable.rowSelector.getAction('action-change-price');

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

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

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

  async openProductOneTimeCreateDialog(): Promise<ProductOneTimeCreateDialog> {
    await this.dataTable.waitForLoading();
    await this.actionMenuButton.click();
    await Promise.all([this.host.waitForURL('/products/one-time/create'), this.actionMenuCreateOneTimeItem.click()]);
    return new ProductOneTimeCreateDialog(this.host);
  }

  async openProductRecurringCreateDialog(): Promise<ProductRecurringCreateDialog> {
    await this.dataTable.waitForLoading();
    await this.actionMenuButton.click();
    await Promise.all([this.host.waitForURL('/products/recurring/create'), this.actionMenuCreateRecurringItem.click()]);
    return new ProductRecurringCreateDialog(this.host);
  }

  async goToProductOneTimeDetailsPage(index: number): Promise<ProductOneTimeDetailsPage> {
    await Promise.all([this.host.waitForURL('**/products/one-time/*'), this.dataTable.clickRow(index)]);
    return new ProductOneTimeDetailsPage(this.host);
  }

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