import { EnumFilterHelper } from './enum-filter-helper';
import { Container } from 'aurelia-framework';
import { IFloatingFilterParams, FilterChangedEvent } from "ag-grid-community";
import { I18N } from "aurelia-i18n";
import BaseFloatingFilter from "../base-floating-filter";
import { EnumSelected } from "../../../model/common-model";

export class EnumFloatingFilter extends BaseFloatingFilter {
  public filterHelper: EnumFilterHelper;

  public init(params: IFloatingFilterParams & { category: string, enum: any }) {
    this.filterHelper = new EnumFilterHelper((newVal, oldVal) => this.filterValuesChanged(newVal, oldVal), params.enum, params.category);
    super.init(params);

    this.i18n = Container.instance.get(I18N);
    if (this.i18n != null) {
      this.DROPDOWN_NAME = this.i18n.tr('grid.noFilter', { ns: "common" });
    }
    super.setupGui();
  }

  public async onOpenDropDown() {
    await this.filterHelper.onOpenDropDown();
  }

  public isFilterActive(): boolean {
    return this.filterHelper.isFilterActive();
  }

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

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

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

  public filterValuesChanged(newVal: boolean, oldVal: boolean) {
    if (newVal != oldVal && !this.firstTime) {
      this.DROPDOWN_NAME = this.getModelAsString();
      (this.params as IFloatingFilterParams).parentFilterInstance((instance) => {
        instance.setModel({ filterType: 'enum', filter: this.filterHelper.filterValues });
      });
    }
  }

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

  public onParentModelChanged(parentModel: any, filterChangedEvent?: FilterChangedEvent) {
    if (parentModel) {
      if (parentModel.filter)
        this.filterHelper.getEnumerated(parentModel.filter)
    }
    else {
      if (this.filterHelper.filterValues)
        this.resetFilter();
    }
    this.DROPDOWN_NAME = this.getModelAsString();
  }
}
