import {ChartLabelFormatter} from "./ChartLabelFormatter";
import {
  ChartLabelModel
} from "../../../../../generated/ervu_dashboard/model/chart/round/label/ChartLabelModel";
import {NotNull, Visible} from "@webbpm/base-package";
import {FormatterUtils} from "../../../../formatter/FormatterUtils";

export class UnitsLabelReplaceValueChartLabelFormatter implements ChartLabelFormatter {
  public valuesToReplace: string[];
  public replaceValue: string = '';
  public noDataValue: string = '';

  @NotNull()
  public labelValueIndex: number;

  public replaceDataLabel: string = '';

  public unitsLabel: string = '';
  public thousandsLabel: string = '';
  public millionsLabel: string = '';
  public billionsLabel: string = '';

  public decimalDelimiter: string;

  @NotNull('decimalDelimiter != null')
  @Visible('decimalDelimiter != null')
  public decimalLength: number = 0;

  format(numberValueModel: ChartLabelModel, labels?: ChartLabelModel[], index?: number): void {
    let labelValueModel = labels[this.labelValueIndex];

    if (!numberValueModel.text) {
      numberValueModel.text = this.noDataValue;
      labelValueModel.text = this.replaceDataLabel;
    }

    if (this.valuesToReplace && this.valuesToReplace.includes(numberValueModel.text)) {
      numberValueModel.text = this.replaceValue;
      labelValueModel.text = this.replaceDataLabel;
    }

    let result = FormatterUtils.formatNumber({
                                               value: numberValueModel.text,

                                               unitsLabel: this.unitsLabel,
                                               thousandsLabel: this.thousandsLabel,
                                               millionsLabel: this.millionsLabel,
                                               billionsLabel: this.billionsLabel,

                                               decimalDelimiter: this.decimalDelimiter,
                                               decimalLength: this.decimalLength
                                             });

    numberValueModel.text = result.formattedNumber;
    labelValueModel.text = result.label;
  }

}