import { CustomLogger } from './../../modules/custom-logger';
import { ServiceBase } from './../../data/service-base';
import { TranslatedProperty, Language } from './../../model/common-model';
import { Router } from 'aurelia-router';
import { autoinject } from 'aurelia-framework';
import * as toastr from 'toastr';
import { DialogController } from 'aurelia-dialog';
import { UtilsMlField } from '../utils-ml-field';
import { ViewModelBase } from '../../vm/view-model-base';
import { EntityTypeNames } from '../../constants/constants';
import { TranslatedPropertyHelper } from '../translated-property-helper';

@autoinject
export class IncompleteMultilingualField extends ViewModelBase {
  public ribbonHeaderText: string = this.translate("incompletemlfield.missingTranslations");
  public ressourceName: string = "";

  private languageService: ServiceBase<Language>;

  private languages: Array<Language> = [];
  private mlProps: Array<IncompleteMlField>;

  private warnMsg: string;

  private dialogTitle: string = this.translate('incompletemlfield.missingTranslations');
  private autoFillBlanksLabel: string = this.translate('incompletemlfield.autoFillBlanks');

  constructor(router: Router, logger: CustomLogger, private dialogController: DialogController) {
    super(router, logger);
    this.languageService = new ServiceBase<Language>(EntityTypeNames.Language, null, EntityTypeNames.ResourceNames.Language);
  }

  private translate(key: string): string {
    return this.i18n.i18next.exists(key) ?
      this.i18n.tr(key) : this.i18n.tr(key, { ns: 'common' })
  }

  public async activate(params) {
    if (params.callback) params.callback(this);
    this.mlProps = params.mlProps;
    this.languages = params.languages ?? await this.languageService.getAllEntities();
    this.warnMsg = "<p>" + this.translate("incompletemlfield.mlInputNotComplete") + "</p>";
    //@ts-ignore
    this.warnMsg = this.warnMsg.replaceAll("{br}", "</p><p>");
  }

  private autoFillBlanks() {
    let noTr = this.mlProps.find(x => this.languages.every(l => !UtilsMlField.isFilled(l, x)));
    if (noTr) {
      return toastr.error(this.translate("incompletemlfield.atLeastOneTrOn") + this.i18n.tr(noTr.label));
    }

    for (let mlProp of this.mlProps) {
      let filledLang = this.languages.find(l => UtilsMlField.isFilled(l, mlProp));
      this.languages
        .filter(l => !UtilsMlField.isFilled(l, mlProp))
        .forEach(l => mlProp.prop[UtilsMlField.getLanguageColumnName(l)] = mlProp.prop[UtilsMlField.getLanguageColumnName(filledLang)]);
    }
  }

  private validate() {
    this.mlProps.forEach(ml => { ml.prop = (TranslatedPropertyHelper.createMissingProperty(ml.prop) as any); })

    let missingTr = this.mlProps.find(x => this.languages.some(l => !UtilsMlField.isFilled(l, x)));
    if (missingTr) {
      return toastr.error(this.translate("incompletemlfield.missingTrOn") + this.i18n.tr(missingTr.label));
    }
    this.dialogController.ok(true);
  }

  private close() {
    this.dialogController.cancel();
  }
}

export interface IncompleteMlField {
  prop: TranslatedProperty;
  label: string;
  isRequired: boolean;
  isTextarea?: boolean;
}
