import { Container } from 'aurelia-framework';
import { I18N } from 'aurelia-i18n';
import { IFloatingFilterParams, FilterChangedEvent } from 'ag-grid-community';
import BaseFloatingFilter from '../base-floating-filter';
import { ServiceBase } from "../../../data/service-base";
import { ValueSelected, User, UserPreFilter } from '../../../model/common-model';
import { UserFilterHelper } from './user-filter-helper';

export class UserFloatingFilter<T extends User> extends BaseFloatingFilter {
  public filterHelper: UserFilterHelper;

  public init(params: IFloatingFilterParams & { userServiceName: string, userService: ServiceBase<T> }): void {
    if (params.userService == null && params.userServiceName == null) {
      throw new Error("userService or userServiceName parameter is missing");
    }

    this.filterHelper = new UserFilterHelper(params.userService ?? new ServiceBase<T>(params.userServiceName),
      this.firstTime,
      (model) => this.setInstanceModel(model),
      () => this.setDropdownName()
    );

    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() {
    this.removeDisposables(false);

    let oldUserValues = this.filterHelper.userValues;
    await this.filterHelper.getUsers();
    if (oldUserValues && oldUserValues.length > 0) {
      this.filterHelper.onOpenDropDown(oldUserValues);
    }
    else {
      this.filterHelper.resetUserValues();
    }
    this.firstTime = false;
    this.filterHelper.setFirstTime(this.firstTime);
  }

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

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

  public onParentModelChanged(parentModel: any, filterChangedEvent?: FilterChangedEvent) {
    if (parentModel != null) {
      if (parentModel.filter != null && Array.isArray(parentModel.filter)) {
        this.filterHelper.userValues = (parentModel.filter as Array<ValueSelected>);
      }
    }
    else {
      if (this.filterHelper.userValues != null || this.filterHelper.selectedUserPreFilter != null) {
        this.resetFilter();
      }
    }

    let userPreFilter = ((parentModel && parentModel.userPreFilter) ? (parentModel.userPreFilter as number) : null);
    if (userPreFilter && userPreFilter != this.filterHelper.selectedUserPreFilter) {
      this.filterHelper.selectedUserPreFilter = userPreFilter;
    }
    this.setDropdownName();
  }

  public getModelAsString(): string {
    if (this.filterHelper.selectedUserPreFilter == UserPreFilter.Me) {
      return this.i18n.tr('grid.me', { ns: "common" });
    }
    else if (this.filterHelper.selectedUserPreFilter == UserPreFilter.NotMe) {
      return this.i18n.tr('grid.notMe', { ns: "common" });
    }
    else if (this.filterHelper.userValues != null && Array.isArray(this.filterHelper.userValues) && this.filterHelper.userValues.some(x => x.selected)) {
      return `${this.i18n.tr('grid.filterOoo', { ns: "common" })} <span style='background-color: gray; color: white; padding: 0 5px 0 5px; border-radius: 20px;'>${this.filterHelper.userValues.filter(x => x.selected).length}</span>`;
    }
    else {
      return this.i18n.tr('grid.noFilter', { ns: "common" });
    }
  }

  public destroy() {
    super.destroy();
    this.removeDisposables(true);
  }

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

  private removeDisposables(all: boolean) {
    this.dispose();
    this.filterHelper.removeDisposables(all);
  }

  private setInstanceModel(model: any) {
    (this.params as IFloatingFilterParams).parentFilterInstance((instance) => {
      (instance as any).filterHelper = this.filterHelper;
      instance.setModel(model);
    });
  }

  private setDropdownName() {
    this.DROPDOWN_NAME = this.getModelAsString();
  }
}
