import { Container } from 'aurelia-framework';
import { I18N } from 'aurelia-i18n';
import { IFloatingFilterParams, FilterChangedEvent } from 'ag-grid-community';
import BaseFloatingFilter from '../base-floating-filter';
import { Datacontext } from '../../../data/data-context';
import { DateFilterHelper } from './date-filter-helper';

export class DateFloatingFilter extends BaseFloatingFilter {

  public filterHelper: DateFilterHelper;
  private datacontext: Datacontext;

  public init(params: IFloatingFilterParams): void {
    this.filterHelper = new DateFilterHelper((newVal: Date | boolean | number, oldVal: Date | boolean | number) => this._propertyChangedHelper(newVal, oldVal));
    super.init(params);

    this.i18n = Container.instance.get(I18N);

    this.datacontext = Container.instance.get(Datacontext);
    this.DROPDOWN_NAME = this.getModelAsString();
    this.DROPDOWN_ICON = 'digi-calendar';
    super.setupGui();
  }

  private _propertyChangedHelper(newVal: Date | boolean | number, oldVal: Date | boolean | number) {
    if (!this.firstTime) {
      this.DROPDOWN_NAME = this.getModelAsString();
      this.setModel();
    }
  }

  public setModel() {
    (this.params as IFloatingFilterParams).parentFilterInstance((instance) => {
      instance.setModel(this.getModel());
    });
  }

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

  public getModel(): any {
    return this.filterHelper.getModel('greaterThan');
  }

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

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

  private getFilter(): string {
    return this.filterHelper.getFilter();
  }

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

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

  public onParentModelChanged(parentModel: any, filterChangedEvent?: FilterChangedEvent): void {
    if (parentModel != null) {
      let datePreFilter = (parentModel.datePreFilter as number);
      if (datePreFilter != null && datePreFilter != this.filterHelper.selectedDatePreFilter) {
        this.filterHelper.selectedDatePreFilter = datePreFilter;
      }
      else {
        this.filterHelper.fromDate = (parentModel.dateFrom as Date);
        this.filterHelper.toDate = (parentModel.dateTo as Date);
      }
      this.filterHelper.isDateNullable = (parentModel.isDateNullable as boolean);
      this.filterHelper.isLastMonth = (parentModel.isLastMonth as boolean);
      this.filterHelper.isLastWeek = (parentModel.isLastWeek as boolean);
      this.filterHelper.isThisWeek = (parentModel.isThisWeek as boolean);
      this.filterHelper.isThisMonth = (parentModel.isThisMonth as boolean);
      this.filterHelper.isNextWeek = (parentModel.isNextWeek as boolean);
      this.filterHelper.isNextMonth = (parentModel.isNextMonth as boolean);
      this.DROPDOWN_NAME = this.getModelAsString();
    } else {
      this.resetFilter();
    }
  }
}