import { BookingBasketContent } from '@/apps/portal/modules/booking/components/BookingBasketContent';
import { IBookingCreateCustomer } from '@/apps/portal/modules/booking/types/booking-entity-types';
import { BookingPaymentView } from '@/apps/portal/modules/booking/views/BookingPaymentView';
import { Checkbox } from '@/apps/portal/shared/components/inputs/Checkbox';
import { PhoneField } from '@/apps/portal/shared/components/inputs/PhoneField';
import { SingleAutocomplete } from '@/apps/portal/shared/components/inputs/SingleAutocomplete';
import { SingleSelect } from '@/apps/portal/shared/components/inputs/SingleSelect';
import { TextField } from '@/apps/portal/shared/components/inputs/TextField';
import { BaseView } from '@/shared/modules/base/views/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: IBookingCreateCustomer): Promise<void> {
    await this.customerTypeSelect.select(customer.type);

    if (customer.type === 'business') {
      this.isMobile && (await this.acceptChangesButton.click());
      await this.companyNameInput.fill(customer.companyName ?? '');
      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')
    );
  }
}
