import { IFilterParams } from 'ag-grid-community';
import { Container } from 'aurelia-framework';
import { EnumerationType } from "../../model/common-model";
import { FilterQueryOp, Predicate } from 'breeze-client';
import { Config } from '../../config';
import { EnumerationTypeService } from '../../services/enumeration-type-service';
import { BaseRelationFilter } from './base-relation-filter';

export class EnumerationFilter extends BaseRelationFilter<EnumerationType> {

  protected filterType: string = 'enumeration';


  private category: number;


  public init(params: IFilterParams & { category: number, service: EnumerationTypeService, relationIdFieldName: string }): void {
    this.config = Container.instance.get(Config);
    this.buildEnumerationTypeService(params);
    this.detectAndSetRelationIdFieldName(params);

    super.init(params);

    if (this.category != null) {
      this.service.gridDataSource.predicates = new Predicate('category', FilterQueryOp.Equals, this.category);
    }

    if (this.service != null) {
      this.service.gridDataSource.queryParameters = Object.assign(
        {}, this.service.gridDataSource.queryParameters || {}, { onlyActifs: true }
      );
    }
  }
  protected buildEnumerationTypeService(params: IFilterParams<any> & { category: number; service: EnumerationTypeService; relationIdFieldName: string; }) {
    if (params.service != null) {
      this.service = params.service;
    }
    else if (params.category != null) {
      this.category = params.category;
      this.service = new EnumerationTypeService(params.category);
    }
    else if (params.colDef.floatingFilterComponentParams.service != null) {
      this.service = params.colDef.floatingFilterComponentParams.service;
    }
    else if (params.colDef.floatingFilterComponentParams.category != null) {
      this.category = params.colDef.floatingFilterComponentParams.category;
      this.service = new EnumerationTypeService(params.colDef.floatingFilterComponentParams.category);
    }

    if (this.service == null && !this.config.agGridConfig.suppressMenu) {
      throw new Error("service or category parameter is missing");
    }
  }

  protected extendFilterValuesChangesModel() {
    return { enumerationIdFieldName: this.relationIdFieldName, displayEmptyFields: this.displayEmptyFields };
  }

  public getModel() {
    let model = super.getModel();
    if (model != null) {
      Object.assign(model, { enumerationIdFieldName: this.relationIdFieldName, displayEmptyFields: this.displayEmptyFields });
    }
    return model;
  }

  public async setupContentGui(): Promise<string> {
    await super.setupContentGui();
    return `     
      <div class="floating-filter__body__enum">
        <div class="floating-filter__body__search">
          <ui-field t="[label]common:grid.searchShort">
            <ui-input type="text" value.bind="valueSearch & 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>
          </ui-field>
          <div class="ui-check__wrapper" if.bind="showEmptyFieldsCheckbox">
            <ui-checkbox checked.bind="displayEmptyFields">\${ 'grid.displayEmptyFields' | t }</ui-checkbox>
          </div>
        </div>

        <div class="floating-filter__body__enum__list">
          <div repeat.for="filterValue of filterValues" if.bind="!filterValue.hide" class="floating-filter__body__enum__list__elem">
            <ui-checkbox checked.bind="filterValue.selected" title.bind="filterValue.name">
              \${filterValue.name}
            </ui-checkbox>
          </div>
        </div>
      </div>`;
  }

  public setupContentGuiSideFilter(): string {
    return `
    <div class="col s12 m12 l12">
      <ui-field label.bind="params.colDef.headerName">
        <ui-select multiple t="[placeholder]general.chooseAnOption" value.bind="selectedIds"
          data-source.bind="service.gridDataSource" options-text="denomination._translation"
          filter-properties.bind="['denomination._translation']" options-value="id">
          <option repeat.for="value of filterValuesSelected" value.bind="value.id" selected.bind="true">
            \${value.name}</option>
          <ui-select-header-template>
            <div class="ui-check__wrapper" if.bind="showEmptyFieldsCheckbox">
                <ui-checkbox checked.bind="displayEmptyFields">\${ 'grid.displayEmptyFields' | t }</ui-checkbox>
            </div>
          </ui-select-header-template>
        </ui-select>
      </ui-field>
    </div>
    `;
  }
}
