import { Config } from '../../../index';
import { IFilterParams } from 'ag-grid-community';
import { Container } from 'aurelia-framework';
import BaseFilter from '../base-filter';
import { StringFilterHelper } from './string-filter-helper';

export class StringFilter extends BaseFilter {
  public filterHelper: StringFilterHelper;

  public init(params: IFilterParams & { searchMinLength: number, isMultiple: boolean }): void {
    this.filterHelper = new StringFilterHelper(params.searchMinLength ?? 0, params.isMultiple ?? true, () => this.textDisplayedChanged());

    this.filterChangedCallback = params.filterChangedCallback;
    this.config = Container.instance.get(Config);

    super.init(params);
  }

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

  public setupContentGui(): string {
    return `<div class="floating-filter__body__search">
              <ui-input type="text" value.bind="filterHelper.textDisplayed & debounce:500" t="[placeholder]common:grid.searchShort" autofocus.one-time="true" allow-clear.one-time="true">
                <ui-input-addon icon="digi-search"></ui-input-addon>
              </ui-input>
              <div class="ui-check__wrapper" if.bind="showEmptyFieldsCheckbox">
                <ui-checkbox checked.bind="filterHelper.displayEmptyFields">\${ 'grid.displayEmptyFields' | t }</ui-checkbox>
              </div>
            </div>`;
  }

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

  public removeFilterValue(): void {
    this.resetFilter();
  }

  public textDisplayedChanged() {
    const instance = this.params.api.getFilterInstance((this.params as IFilterParams).colDef.field);
    instance.setModel({ filterType: 'text', type: 'contains', filter: this.filterHelper.text, isMultiple: this.filterHelper.isMultiple, displayEmptyFields: this.filterHelper.displayEmptyFields });
  }

  public getModel(): any {
    return this.isFilterActive() ?
      { filterType: 'text', type: 'contains', filter: this.filterHelper.text, isMultiple: this.filterHelper.isMultiple, displayEmptyFields: this.filterHelper.displayEmptyFields }
      : null;
  }

  public setModel(model: any): void {
    if (model == null) {
      this.filterHelper.text = null;
      this.filterHelper.isMultiple = true;
    }
    else if (model.filterType == 'text') {
      this.filterHelper.text = model.filter;

      this.filterHelper.isMultiple = model.isMultiple;
    }
    this.filterHelper.textDisplayed = this.filterHelper.text;
    this.filterChangedCallback();
  }

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

  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.textDisplayedChanged.bind(this)));
  }
}
