import { InvoiceCreatePayment, InvoiceCreatePaymentOption } from '@/manager/modules/invoice/invoice-types';
import { ManualPaymentMethodAddForm } from '@/manager/modules/ui/payment-method/components/ManualPaymentMethodAddForm';
import { PaymentMethodAddForm } from '@/manager/modules/ui/payment-method/components/PaymentMethodAddForm';
import { RadioButton } from '@/manager/shared/components/RadioButton';
import { getRadioGroupHost } from '@/manager/shared/utils/locator-utils';
import { BaseComponent } from '@/shared/base/BaseComponent';

export class InvoiceCreatePaymentRadioGroup extends BaseComponent {
  readonly main = getRadioGroupHost(this.host);
  readonly automaticPaymentSection = this.main.getByTestId('automatic-payment-option');
  readonly automaticPaymentRadioButton = new RadioButton(this.automaticPaymentSection);
  readonly paymentMethodAddForm = new PaymentMethodAddForm(this.automaticPaymentSection);
  readonly manualPaymentSection = this.main.getByTestId('manual-payment-option');
  readonly manualPaymentRadioButton = new RadioButton(this.manualPaymentSection);
  readonly manualPaymentMethodAddForm = new ManualPaymentMethodAddForm(this.manualPaymentSection);

  async fill(data: InvoiceCreatePayment): Promise<void> {
    await this.check(data.option);

    if (data.option === 'charge_automatically' && data.method) {
      await this.paymentMethodAddForm.select(data.method);
    } else if (data.option === 'send_invoice' && data.manual) {
      await this.manualPaymentMethodAddForm.fill(data.manual);
    }
  }

  private check(option: InvoiceCreatePaymentOption): Promise<void> {
    switch (option) {
      case 'charge_automatically':
        return this.automaticPaymentRadioButton.check();
      case 'send_invoice':
        return this.manualPaymentRadioButton.check();
    }
  }
}
