import { Container } from 'aurelia-framework';
import { I18N } from 'aurelia-i18n';
import { IFloatingFilterParams } from 'ag-grid-community';
import BaseFloatingFilter from '../base-floating-filter';
import { NumberFilterHelper } from './number-filter-helper';

export class NumberFloatingFilter extends BaseFloatingFilter {

  public filterHelper: NumberFilterHelper;

  public init(params: IFloatingFilterParams): void {
    this.filterHelper = new NumberFilterHelper((model) => {
      (this.params as IFloatingFilterParams).parentFilterInstance((instance) => {
        instance.setModel(model);
      });
    });
    super.init(params);
    this.i18n = Container.instance.get(I18N);
    if (this.i18n != null) {
      this.DROPDOWN_NAME = this.i18n.tr('grid.noFilter', { ns: "common" });
    }
    this.setupGui();
  }

  public onParentModelChanged(parentModel: any): void {
    this.filterHelper.type = (parentModel?.type ? parentModel.type : 'equals');
    this.filterHelper.fromNumber = (parentModel?.filter ? parentModel.filter : null);
    this.filterHelper.toNumber = (parentModel?.filterTo ? parentModel.filterTo : null);
    this.DROPDOWN_NAME = this.getModelAsString();
  }

  public setupContentGui(): string {
    return this.filterHelper.setupContentGui();
  }

  public resetFilter() {
    this.filterHelper.resetFilter();
  }

  public getModelAsString(): string {
    return this.filterHelper.getModelAsString();
  }

  public setupContentGuiSideFilter(): string {
    return this.filterHelper.setupContentGuiSideFilter();
  }

  protected applyObservers() {
    this.dispose(); // Making sure that everything is disposed before re-adding observers
    this.disposables.push(this.bindingEngine.propertyObserver(this.filterHelper, 'displayEmptyFields').subscribe(() => this.filterHelper.displayEmptyFieldsChanged()));
  }
}
