import { BookingBasketContent } from '@/portal/modules/booking/components/BookingBasketContent';
import { BookingCreateCustomer } from '@/portal/modules/booking/booking-types';
import { BookingPaymentView } from '@/portal/modules/booking/views/BookingPaymentView';
import { Checkbox } from '@/portal/shared/components/Checkbox';
import { PhoneField } from '@/portal/shared/components/PhoneField';
import { SingleAutocomplete } from '@/portal/shared/components/SingleAutocomplete';
import { SingleSelect } from '@/portal/shared/components/SingleSelect';
import { TextField } from '@/portal/shared/components/TextField';
import { BaseView } from '@/shared/base/BaseView';
import { Locator } from '@playwright/test';

export class BookingCustomerView extends BaseView {
  readonly customerTypeSelect = new SingleSelect(this.host.getByTestId('customer-type-select'));
  readonly companyNameInput = new TextField(this.host.getByTestId('company-name-input'));
  readonly vatNumberInput = new TextField(this.host.getByTestId('vat-number-input'));
  readonly firstNameInput = new TextField(this.host.getByTestId('first-name-input'));
  readonly lastNameInput = new TextField(this.host.getByTestId('last-name-input'));
  readonly emailInput = new TextField(this.host.getByTestId('email-input'));
  readonly phoneInput = new PhoneField(this.host.getByTestId('phone-input'));
  readonly streetInput = new TextField(this.host.getByTestId('street-input'));
  readonly zipInput = new TextField(this.host.getByTestId('zip-input'));
  readonly cityInput = new TextField(this.host.getByTestId('city-input'));
  readonly countryAutocomplete = new SingleAutocomplete(this.host.getByTestId('country-combobox'));
  readonly termsCheckboxes = this.host.getByTestId('terms-checkbox');
  readonly acceptChangesButton = this.host.getByTestId('accept-basket-changes-button');
  isMobile = false;

  async fill(customer: BookingCreateCustomer): Promise<void> {
    await this.customerTypeSelect.select(customer.type);

    if (customer.type === 'business') {
      if (this.isMobile) {
        await this.acceptChangesButton.click();
      }

      await this.companyNameInput.fill(customer.companyName ?? '');

      if (customer.vatNumber) {
        await this.vatNumberInput.fill(customer.vatNumber);
      }
    }

    await this.firstNameInput.fill(customer.firstName);
    await this.lastNameInput.fill(customer.lastName);
    await this.emailInput.fill(customer.email);
    await this.phoneInput.fill(customer.phone, customer.country.name);
    await this.streetInput.fill(customer.street);
    await this.zipInput.fill(customer.postalCode);
    await this.cityInput.fill(customer.city);
    await this.countryAutocomplete.fill(customer.country.name);

    for (const locator of await this.termsCheckboxes.all()) {
      const checkbox = new Checkbox(locator);
      await checkbox.check();
    }
  }

  async confirm(): Promise<BookingPaymentView> {
    await Promise.all([this.host.waitForURL('**/checkout/step/payment?lang=en'), this.getSubmitButton().click()]);
    return new BookingPaymentView(this.host);
  }

  async openDrawer(): Promise<void> {
    await this.host.getByTestId('drawer-drag-handle').click();
  }

  getSubmitButton(): Locator {
    return this.isMobile
      ? this.host.getByTestId('drawer-submit-button')
      : this.host.getByTestId('standalone-submit-button');
  }

  getBasketContent(): BookingBasketContent {
    return new BookingBasketContent(
      this.isMobile ? this.host.getByTestId('basket-content-drawer') : this.host.getByTestId('basket-content-card')
    );
  }
}
