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 { SingleSelect } from '@/manager/shared/components/SingleSelect';
import { getInputHostByLabel, getRadioGroupHost } from '@/manager/shared/utils/locator-utils';
import { BaseComponent } from '@/shared/base/BaseComponent';
import { getCurrencySymbol } from '@/shared/utils/formatters';

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')
  );
  readonly valueTypeSelect = new SingleSelect(this.main.getByTestId('price-change-value-type-select'));

  async fill(data: PriceChange, currency: string): Promise<void> {
    await this.check(data.option);
    await this.valueTypeSelect.select(data.valueType === 'percentage' ? '%' : getCurrencySymbol(currency));
    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();
    }
  }
}
