import { BookingStripePaymentElement } from '@/portal/modules/booking/components/BookingStripePaymentElement';
import { BookingSuccessView } from '@/portal/modules/booking/views/BookingSuccessView';
import { BaseView } from '@/shared/base/BaseView';
import { PaymentMethod } from '@/shared/modules/payment-method/payment-method-types';

export class BookingPaymentView extends BaseView {
  readonly stripeBody = this.host.getByTitle('Secure payment input frame').frameLocator(':scope').locator('body');
  readonly stripePaymentElement = new BookingStripePaymentElement(this.stripeBody);
  readonly stripeFieldError = this.stripeBody.locator('.Error');
  readonly stripeAlertError = this.host.getByTestId('stripe-error');
  readonly payNowButton = this.host.getByTestId('pay-now-button');

  async fill(paymentMethod: PaymentMethod): Promise<void> {
    switch (paymentMethod.kind) {
      case 'card':
        await this.stripePaymentElement.fillCardInfo(paymentMethod);
        break;
      case 'sepa':
        await this.stripePaymentElement.fillSepaInfo(paymentMethod);
        break;
    }
  }

  async confirm(): Promise<BookingSuccessView> {
    await Promise.all([this.host.waitForURL('**/checkout/success?*lang=en'), this.submit()]);
    return new BookingSuccessView(this.host);
  }

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