import { BaseDialog } from '@/manager/shared/base/BaseDialog';
import { CustomerCreateAccountingFieldSet } from '@/manager/modules/customer/components/CustomerCreateAccountingFieldSet';
import { CustomerCreateAddressFieldSet } from '@/manager/modules/customer/components/CustomerCreateAddressFieldSet';
import { CustomerCreateGeneralFieldSet } from '@/manager/modules/customer/components/CustomerCreateGeneralFieldSet';
import { CustomerCreateData } from '@/manager/modules/customer/customer-types';
import { CustomerDetailsPage } from '@/manager/modules/customer/views/CustomerDetailsPage';
import { getSnackbarHost, getDialogErrorsHost } from '@/manager/shared/utils/locator-utils';
import { getDetailsPageRegExpUrl } from '@/manager/shared/utils/url-utils';
import { Page } from '@playwright/test';

export class CustomerCreateDialog extends BaseDialog {
  readonly generalFieldSet = new CustomerCreateGeneralFieldSet(this.main);
  readonly accountingFieldSet = new CustomerCreateAccountingFieldSet(this.main);
  readonly addressFieldSet = new CustomerCreateAddressFieldSet(this.main);
  readonly errors = getDialogErrorsHost(this.main);
  readonly snackbar = getSnackbarHost(this.host, 'Customer created successfully');

  constructor(host: Page) {
    super(host, 'customer-create-dialog');
  }

  async goto(): Promise<void> {
    await this.host.goto('/customers/create');
  }

  async create(data: CustomerCreateData): Promise<CustomerDetailsPage> {
    await this.fill(data);
    await Promise.all([
      this.snackbar.waitFor(),
      this.host.waitForURL(getDetailsPageRegExpUrl('customers')),
      this.submit(),
    ]);
    return new CustomerDetailsPage(this.host);
  }

  async fill(data: CustomerCreateData): Promise<void> {
    await this.generalFieldSet.customerTypeAutocomplete.fill(data.type);
    await this.generalFieldSet.locationsAutocomplete.fill(data.locations.map((l) => l.name));

    if (data.type === 'business') {
      if (data.companyName != null) {
        await this.generalFieldSet.companyNameTextField.fill(data.companyName);
      }

      if (data.vatType != null) {
        await this.generalFieldSet.vatTypeAutocomplete.fill(data.vatType.name);
      }

      if (data.vatNumber != null) {
        await this.generalFieldSet.vatNumberTextField.fill(data.vatNumber);
      }
    }

    if (data.firstName != null) {
      await this.generalFieldSet.firstNameTextField.fill(data.firstName);
    }

    if (data.lastName != null) {
      await this.generalFieldSet.lastNameTextField.fill(data.lastName);
    }

    await this.generalFieldSet.emailTextField.fill(data.email);
    await this.generalFieldSet.phoneField.fill(data.phone, data.country.name);
    await this.generalFieldSet.languageAutocomplete.fill(data.language.name);

    if (data.note != null) {
      await this.generalFieldSet.noteTextAreaField.fill(data.note);
    }

    if (data.accountsReceivableNumber != null) {
      await this.accountingFieldSet.accountsReceivableNumberTextField.fill(data.accountsReceivableNumber);
    }

    await this.addressFieldSet.streetTextField.fill(data.street);
    await this.addressFieldSet.postalCodeTextField.fill(data.postalCode);
    await this.addressFieldSet.cityTextField.fill(data.city);
    await this.addressFieldSet.countryAutocomplete.fill(data.country.name);
  }

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