import { BaseDialog } from '@/manager/shared/base/BaseDialog';
import { ICustomerEditGeneralData } from '@/manager/modules/customer/types/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 { getInputHostByLabel, getSnackbarHost } from '@/manager/shared/utils/host-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 snackbar = getSnackbarHost(this.host, 'Action successfully executed!');

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

  async edit(data: ICustomerEditGeneralData): Promise<CustomerDetailsPage> {
    data.type != null && (await this.customerTypeAutocomplete.fill(data.type));
    data.locations != null && (await this.addAnotherLocationAutocomplete.fill(data.locations));

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

    data.firstName != null && (await this.firstNameTextField.fill(data.firstName));
    data.lastName != null && (await this.lastNameTextField.fill(data.lastName));
    data.email != null && (await this.emailTextField.fill(data.email));
    data.phone != null && data.country != null && (await this.phoneField.fill(data.phone, data.country.name));
    data.language != null && (await this.languageAutocomplete.fill(data.language.name));
    data.street != null && (await this.streetTextField.fill(data.street));
    data.postalCode != null && (await this.postalCodeTextField.fill(data.postalCode));
    data.city != null && (await this.cityTextField.fill(data.city));
    data.country != null && (await this.countryAutocomplete.fill(data.country.name));

    await Promise.all([this.snackbar.waitFor(), this.host.waitForURL('**/customers/*'), this.submitButton.click()]);
    return new CustomerDetailsPage(this.host);
  }
}
