import { PriceChange, PriceChangeOption } from '@/manager/modules/ui/price-change/price-change-types';
import { NumberField } from '@/manager/shared/components/NumberField';
import { RadioButton } from '@/manager/shared/components/RadioButton';
import { getInputHostByLabel, getRadioGroupHost } from '@/manager/shared/utils/locator-utils';
import { BaseComponent } from '@/shared/base/BaseComponent';

export class PriceChangeRadioGroup extends BaseComponent {
  readonly main = getRadioGroupHost(this.host);
  readonly increasePriceSection = this.main.getByTestId('price-change-radio-group-item-increase');
  readonly increasePriceRadioButton = new RadioButton(this.increasePriceSection);
  readonly increasePriceNumberField = new NumberField(
    getInputHostByLabel(this.increasePriceSection, 'Increase (excl. VAT) by')
  );
  readonly decreasePriceSection = this.main.getByTestId('price-change-radio-group-item-decrease');
  readonly decreasePriceRadioButton = new RadioButton(this.decreasePriceSection);
  readonly decreasePriceNumberField = new NumberField(
    getInputHostByLabel(this.decreasePriceSection, 'Decrease (excl. VAT) by')
  );

  async fill(data: PriceChange): Promise<void> {
    await this.check(data.option);
    data.option === 'increase'
      ? await this.increasePriceNumberField.fill(data.value)
      : await this.decreasePriceNumberField.fill(data.value);
  }

  private check(option: PriceChangeOption): Promise<void> {
    switch (option) {
      case 'increase':
        return this.increasePriceRadioButton.check();
      case 'decrease':
        return this.decreasePriceRadioButton.check();
    }
  }
}
