import { Router } from 'aurelia-router';
import { I18N } from 'aurelia-i18n';
import { PLATFORM, autoinject } from "aurelia-framework";
import { ServiceBase } from "../../data/service-base";
import { Country, 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 CountryList extends ListViewModelBase<Country> {
  public ressourceName: string = LocalityRessourceName;

  constructor(router: Router, logger: CustomLogger, i18n: I18N) {
    super(router, logger, new ServiceBase<Country>(EntityTypeNames.Country));
  }


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

  public getDetailsUrl(self, entity: Country) {
    if (entity && entity.id) {
      return this.routePrefix + entity.id;
    }
    return null;
  }

  public get routePrefix(): string {
    return this.router.routes.find(x => x.name == 'country-detail').href.replace('' + Various.NewId, '');
  }

  public getDataGridColumns() {
    let isSelectedCol = GridHelper.getSelectedColDef(this);
    let defs: ColDef[] = [
      isSelectedCol,
      {
        headerName: this.i18n.tr("common:country.code"),
        field: "code",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("common:country.region"),
        field: "region",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("common:country.isFavorite"),
        field: "isFavorite",
        type: FieldType.Boolean
      },
      {
        headerName: this.i18n.tr("common:country.isEU"),
        field: "isEU",
        type: FieldType.Boolean
      },
      {
        headerName: this.i18n.tr("common:locality.name") + this.i18n.tr("general.inBracketsFR"),
        field: "name.lang1",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("common:locality.name") + this.i18n.tr("general.inBracketsNL"),
        field: "name.lang2",
        type: FieldType.String
      },
      {
        headerName: this.i18n.tr("common:locality.name") + this.i18n.tr("general.inBracketsEN"),
        field: "name.lang3",
        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 importCountries() {
    this.router.navigateToRoute('import-data', { "context": "country", "errorView": "/node_modules/digiwall-lib/src/vm/countries/country-error.html" });

  }
}

