import { BookingPriceChange } from '@/manager/modules/booking/booking-types';
import { BookingPriceChangeConfirmDialog } from '@/manager/modules/booking/views/BookingPriceChangeConfirmDialog';
import { PriceChangeRadioGroup } from '@/manager/modules/ui/price-change/components/PriceChangeRadioGroup';
import { BaseDialog } from '@/manager/shared/base/BaseDialog';
import { Checkbox } from '@/manager/shared/components/Checkbox';
import { SingleDatePicker } from '@/manager/shared/components/SingleDatePicker';
import { getDialogErrorsHost, getSnackbarHost } from '@/manager/shared/utils/locator-utils';
import { Page } from '@playwright/test';

export class BookingPriceChangeDialog extends BaseDialog {
  readonly priceChangeRadioGroup = new PriceChangeRadioGroup(this.main);
  readonly unitCheckbox = new Checkbox(this.main.getByTestId('price-change-target-unit'));
  readonly recurringProductsCheckbox = new Checkbox(this.main.getByTestId('price-change-target-recurring-products'));
  readonly changeAtDatePicker = new SingleDatePicker(this.main.getByTestId('date-picker'));
  readonly confirmDialog = new BookingPriceChangeConfirmDialog(this.host);
  readonly errors = getDialogErrorsHost(this.main);
  readonly snackbar = getSnackbarHost(this.host, 'Price adjustment saved successfully');
  readonly noBookingsAffectedSnackbar = getSnackbarHost(
    this.host,
    'No price changes have been scheduled, as the selected bookings would not be affected by the price adjustment you have chosen.'
  );

  constructor(host: Page) {
    super(host, 'booking-price-change-dialog');
  }

  async edit(data: BookingPriceChange, currency: string): Promise<void> {
    await this.fill(data, currency);
    await this.submit();
    return this.confirm();
  }

  async fill(data: BookingPriceChange, currency: string): Promise<void> {
    await this.priceChangeRadioGroup.fill(data, currency);
    await this.unitCheckbox.set(data.adjustUnit);
    await this.recurringProductsCheckbox.set(data.adjustRecurringProducts);
    await this.changeAtDatePicker.fill(data.changeAt);
  }

  async submit(): Promise<void> {
    await this.submitButton.click();
  }

  async confirm(): Promise<void> {
    await Promise.all([this.snackbar.waitFor(), this.confirmDialog.submit()]);
  }
}
