import { BaseDialog } from '@/manager/shared/base/BaseDialog';
import { CustomerEditGeneralData } from '@/manager/modules/customer/customer-types';
import { CustomerDetailsPage } from '@/manager/modules/customer/views/CustomerDetailsPage';
import { MultiAutocomplete } from '@/manager/shared/components/MultiAutocomplete';
import { PhoneField } from '@/manager/shared/components/PhoneField';
import { SingleAutocomplete } from '@/manager/shared/components/SingleAutocomplete';
import { TextField } from '@/manager/shared/components/TextField';
import { getDialogErrorsHost, getInputHostByLabel, getSnackbarHost } from '@/manager/shared/utils/locator-utils';
import { Page } from '@playwright/test';
import { TextAreaField } from '@/manager/shared/components/TextAreaField';

export class CustomerEditGeneralDialog extends BaseDialog {
  readonly customerTypeAutocomplete = new SingleAutocomplete(getInputHostByLabel(this.main, 'Customer type'));
  readonly addAnotherLocationAutocomplete = new MultiAutocomplete(
    getInputHostByLabel(this.main, 'Add another location')
  );
  readonly companyNameTextField = new TextField(getInputHostByLabel(this.main, 'Company name'));
  readonly vatTypeAutocomplete = new SingleAutocomplete(getInputHostByLabel(this.main, 'VAT type'));
  readonly vatNumberTextField = new TextField(getInputHostByLabel(this.main, 'VAT number'));
  readonly firstNameTextField = new TextField(getInputHostByLabel(this.main, 'First name'));
  readonly lastNameTextField = new TextField(getInputHostByLabel(this.main, 'Last name'));
  readonly emailTextField = new TextField(getInputHostByLabel(this.main, 'Email'));
  readonly phoneField = new PhoneField(getInputHostByLabel(this.main, 'Phone number'));
  readonly languageAutocomplete = new SingleAutocomplete(getInputHostByLabel(this.main, 'Language'));
  readonly streetTextField = new TextField(getInputHostByLabel(this.main, 'Street'));
  readonly postalCodeTextField = new TextField(getInputHostByLabel(this.main, 'Postal code'));
  readonly cityTextField = new TextField(getInputHostByLabel(this.main, 'City'));
  readonly countryAutocomplete = new SingleAutocomplete(getInputHostByLabel(this.main, 'Country'));
  readonly noteTextAreaField = new TextAreaField(getInputHostByLabel(this.main, 'Note'));
  readonly errors = getDialogErrorsHost(this.main);
  readonly snackbar = getSnackbarHost(this.host, 'Action successfully executed!');

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

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

  async fill(data: CustomerEditGeneralData): Promise<void> {
    if (data.type != null) {
      await this.customerTypeAutocomplete.fill(data.type);
    }

    if (data.locations != null) {
      await this.addAnotherLocationAutocomplete.fill(data.locations.map((l) => l.name));
    }

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

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

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

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

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

    if (data.email != null) {
      await this.emailTextField.fill(data.email);
    }

    if (data.phone != null && data.country != null) {
      await this.phoneField.fill(data.phone, data.country.name);
    }

    if (data.language != null) {
      await this.languageAutocomplete.fill(data.language.name);
    }

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

    if (data.street != null) {
      await this.streetTextField.fill(data.street);
    }

    if (data.postalCode != null) {
      await this.postalCodeTextField.fill(data.postalCode);
    }

    if (data.city != null) {
      await this.cityTextField.fill(data.city);
    }

    if (data.country != null) {
      await this.countryAutocomplete.fill(data.country.name);
    }
  }

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