import { Router } from 'aurelia-router';
import { I18N } from 'aurelia-i18n';
import { PLATFORM, autoinject } from "aurelia-framework";
import { ServiceBase } from "../../data/service-base";
import { FieldType, Locality, LocalityRessourceName, Various } from "../../model/common-model";
import { ListViewModelBase } from "../list-view-model-base";
import { ColDef } from 'ag-grid-community';
import { GridHelper } from '../../resources/elements/ag-grid/grid-helper';
import { CustomLogger } from '../../modules/custom-logger';
import { EntityTypeNames } from '../../constants/constants';

@autoinject
export class LocalityList extends ListViewModelBase<Locality> {
  public ressourceName: string = LocalityRessourceName;

  constructor(router: Router, logger: CustomLogger, i18n: I18N) {
    super(router, logger, new ServiceBase<Locality>(EntityTypeNames.Locality));
    this.service.gridDataSource.expands = ['country', 'commune'];
  }


  public addLocality() {
    this.navigateTo(this.routePrefix + Various.NewId);
  }

  public getDetailsUrl(self, entity: Locality) {
    if (entity && entity.id) {
      return this.routePrefix + entity.id;
    }
    return null;
  }
  public get routePrefix(): string {
    return this.router.routes.find(x => x.name == 'locality-detail').href.replace('' + Various.NewId, '');
  }
  public getDataGridColumns() {
    let isSelectedCol = GridHelper.getSelectedColDef(this);
    let defs: ColDef[] = [
      isSelectedCol,
      {
        headerName: this.i18n.tr("locality.zipCode"),
        field: "zipCode",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("locality.provincieName"),
        field: "provincieName",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("locality.isSubCommune"),
        field: "isSubCommune",
        type: FieldType.Boolean
      },
      {
        headerName: this.i18n.tr("locality.name") + this.i18n.tr("general.inBracketsFR"),
        field: "name.lang1",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("locality.name") + this.i18n.tr("general.inBracketsNL"),
        field: "name.lang2",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("locality.name") + this.i18n.tr("general.inBracketsEN"),
        field: "name.lang3",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("country.country"),
        field: "country.name._translation",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("locality.communeId"),
        field: "commune.name._translation",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("general.createdTime"),
        field: "createdTime",
        hide: true,
        type: FieldType.Date
      },
      {
        headerName: this.i18n.tr("general.updatedTime"),
        field: "updatedTime",
        hide: true,
        type: FieldType.Date
      },
      {
        headerName: this.i18n.tr("general.createdByFullName"),
        field: "createdByFullName",
        hide: true,
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("general.updatedByFullName"),
        field: "updatedByFullName",
        hide: true,
        type: FieldType.String
      }
    ]
    return defs;
  }

  public importLocalities() {
    this.router.navigateToRoute('import-data', { "context": "locality", "errorView": "/node_modules/digiwall-lib/src/vm/localities/locality-error.html" });

  }
}

