import { BaseComponent } from '@/shared/base/BaseComponent';
import { BacsPayment } from '@/shared/modules/payment-method/payment-method-types';

export class BacsPaymentMethodAddForm extends BaseComponent {
  readonly body = this.host.frameLocator('iframe').locator('body');
  readonly sortCodeField = this.body.locator('#sortCode');
  readonly accountNumberField = this.body.locator('#accountNumber');
  readonly fullNameField = this.body.locator('#billingName');
  readonly countrySelect = this.body.locator('#billingCountry');
  readonly addressLine1Field = this.body.locator('#billingAddressLine1');
  readonly cityField = this.body.locator('#billingLocality');
  readonly postalCodeField = this.body.locator('#billingPostalCode');
  readonly administrativeAreaField = this.body.locator('#billingAdministrativeArea');
  readonly confirmCheckbox = this.body.locator('#confirm');
  readonly termsCheckbox = this.body.locator('#termsOfServiceConsentCheckbox');
  readonly submitButton = this.body.locator('[data-testid=hosted-payment-submit-button]');

  async fill(data: BacsPayment): Promise<void> {
    await this.sortCodeField.fill(data.code);
    await this.accountNumberField.fill(data.number);
    await this.fullNameField.fill(data.fullName);
    await this.countrySelect.selectOption(data.country.id);
    await this.addressLine1Field.fill(data.street);

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

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

    if (data.administrativeArea != null) {
      await this.administrativeAreaField.selectOption(data.administrativeArea);
    }

    await this.confirmCheckbox.check();
    await this.termsCheckbox.check();
  }
}
