import { ProductRecurringDetailsBookingPortalCard } from '@/manager/modules/product/components/ProductRecurringDetailsBookingPortalCard';
import { ProductRecurringDetailsGeneralCard } from '@/manager/modules/product/components/ProductRecurringDetailsGeneralCard';
import { ProductUnitTypeCard } from '@/manager/modules/product/components/ProductUnitTypeCard';
import { ProductListPage } from '@/manager/modules/product/views/ProductListPage';
import { ProductEditUnitTypesDialog } from '@/manager/modules/product/views/ProductEditUnitTypesDialog';
import { ProductRecurringEditBookingPortalDialog } from '@/manager/modules/product/views/ProductRecurringEditBookingPortalDialog';
import { ProductRecurringEditGeneralDialog } from '@/manager/modules/product/views/ProductRecurringEditGeneralDialog';
import { BookingPlanCard } from '@/manager/modules/ui/booking-plan/components/BookingPlanCard';
import { TaxCard } from '@/manager/modules/ui/tax/components/TaxCard';
import { TaxEditDialog } from '@/manager/modules/ui/tax/views/TaxEditDialog';
import { BasePage } from '@/manager/shared/base/BasePage';
import { getPageHeaderBackButtonHost, getPageHeaderStatusHost } from '@/manager/shared/utils/locator-utils';
import { getDetailsPageIdFromUrl } from '@/manager/shared/utils/url-utils';
import { ProductDeleteDialog } from '@/manager/modules/product/views/ProductDeleteDialog';

export class ProductRecurringDetailsPage extends BasePage {
  readonly backButton = getPageHeaderBackButtonHost(this.header);
  readonly publishStatusChip = getPageHeaderStatusHost(this.header);
  readonly actionMenu = this.host.getByTestId('product-action-list');
  readonly actionMenuButton = this.header.getByTestId('product-action-button');
  readonly actionMenuDeleteItem = this.actionMenu.getByTestId('product-action-delete');
  readonly generalCard = new ProductRecurringDetailsGeneralCard(this.body);
  readonly bookingPortalCard = new ProductRecurringDetailsBookingPortalCard(this.body);
  readonly taxCard = new TaxCard(this.body);
  readonly bookingPlanCard = new BookingPlanCard(this.body);
  readonly unitTypeCard = new ProductUnitTypeCard(this.body);

  async goto(id: number): Promise<void> {
    await this.host.goto(`/products/recurring/${id}`);
  }

  getProductId(): number {
    return getDetailsPageIdFromUrl(this.host.url(), 'products/recurring');
  }

  async openProductRecurringEditGeneralDialog(): Promise<ProductRecurringEditGeneralDialog> {
    await Promise.all([
      this.host.waitForURL('**/products/recurring/*/edit/general'),
      this.generalCard.editButton.click(),
    ]);
    return new ProductRecurringEditGeneralDialog(this.host);
  }

  async openProductRecurringEditBookingPortalDialog(): Promise<ProductRecurringEditBookingPortalDialog> {
    await Promise.all([
      this.host.waitForURL('**/products/recurring/*/edit/booking-portal'),
      this.bookingPortalCard.editButton.click(),
    ]);
    return new ProductRecurringEditBookingPortalDialog(this.host);
  }

  async openTaxEditDialog(): Promise<TaxEditDialog> {
    return this.taxCard.openTaxEditDialog();
  }

  async openProductEditUnitTypesDialog(): Promise<ProductEditUnitTypesDialog> {
    return this.unitTypeCard.openProductEditUnitTypesDialog(true);
  }

  async openProductDeleteDialog(): Promise<ProductDeleteDialog> {
    await this.actionMenuButton.click();
    await this.actionMenuDeleteItem.click();
    return new ProductDeleteDialog(this.host);
  }

  async returnToProductListPage(): Promise<ProductListPage> {
    await Promise.all([this.host.waitForURL('/products'), this.backButton.click()]);
    return new ProductListPage(this.host);
  }
}
