import { View } from 'aurelia-framework';
import { ViewSlot } from 'aurelia-framework';
import { Container } from 'aurelia-framework';
import { I18N } from 'aurelia-i18n';
import { IFilterParams } from 'ag-grid-community';
import BaseFilter from '../base-filter';
import { NumberFilterHelper } from './number-filter-helper';

export class NumberFilter extends BaseFilter {
  protected DROPDOWN_NAME: string = null;

  protected view: View;
  protected viewSlot: ViewSlot;

  public filterHelper: NumberFilterHelper;

  public init(params: IFilterParams): void {
    this.filterChangedCallback = params.filterChangedCallback;
    this.filterHelper = new NumberFilterHelper((model) => {
      if (this.isDestroyed()) {
        return;
      }
      const instance = this.params.api.getFilterInstance((this.params as IFilterParams).colDef.field);
      instance.setModel(model);
    });
    super.init(params);
    this.i18n = Container.instance.get(I18N);
    this.params = params;
  }

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

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

  public setModel(model: any): void {
    if (model == null) {
      this.filterHelper.type ??= 'equals';
      this.filterHelper.fromNumber = null;
      this.filterHelper.toNumber = null;
    }
    else if (model?.filterType == 'number') {
      this.filterHelper.type = model.type;
      this.filterHelper.fromNumber = model.filter;
      this.filterHelper.toNumber = model.filterTo;
    }
    this.filterChangedCallback();
  }

  public getGui(): HTMLElement {
    return super.getGui();
  }

  public getModel(): any {
    let model = { filterType: 'number', displayEmptyFields: this.filterHelper.displayEmptyFields };
    if (this.isFilterActive()) {
      if (this.filterHelper.type == 'equals') {
        if (!this.filterHelper.fromNumberIsEmpty()) {
          return Object.assign(model, { type: 'equals', filter: this.filterHelper.fromNumber, filterTo: null });
        }
      }
      else {
        if (!this.filterHelper.fromNumberIsEmpty() && !this.filterHelper.toNumberIsEmpty()) {
          return Object.assign(model, { type: 'inRange', filter: this.filterHelper.fromNumber, filterTo: this.filterHelper.toNumber });
        }
        else if (!this.filterHelper.toNumberIsEmpty()) {
          return Object.assign(model, { type: 'lessThan', filter: null, filterTo: this.filterHelper.toNumber });
        }
        else if (!this.filterHelper.fromNumberIsEmpty()) {
          return Object.assign(model, { type: 'greaterThan', filter: this.filterHelper.fromNumber, filterTo: null });
        }
      }
      return model;
    }
    return Object.assign(model, { type: this.filterHelper.type });
  }

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

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

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

  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.filterHelper.displayEmptyFieldsChanged()));
  }
}
