import { off } from 'process';
import { Config } from '../../config';
import { Router } from 'aurelia-router';
import { inject } from 'aurelia-framework';
import { EntityDetailViewModelBase } from '../../vm/detail-view-model-base';
import { AgGridView, AgGridViewUser, User, ViewVisibilityEnum } from '../../model/common-model';
import { ServiceBase } from '../../data/service-base';
import { CustomLogger } from '../../modules/custom-logger';
import { Actions, EntityTypeNames } from '../../constants/constants';

@inject(Router, CustomLogger)
export class AgGridViewDetail extends EntityDetailViewModelBase<AgGridView> {
  public ressourceName: string = EntityTypeNames.AgGridView;
  public documentTitle: string = this.i18n.tr("general.addnew", { ns: "common" });

  protected canAddPublicAgGridView: boolean = true;
  protected canAddSharedAgGridView: boolean = false;

  public userService: ServiceBase<User>;

  constructor(router: Router, logger: CustomLogger) {
    super(router, logger);
    super.initialize(new ServiceBase<AgGridView>(EntityTypeNames.AgGridView));
    this.canAddSharedAgGridView = this.config.agGridConfig.allowSharedView;
    this.canAddPublicAgGridView = this.authService.checkAccess(EntityTypeNames.AgGridView + "Public", Actions.Create);
    this.userService = new ServiceBase<User>(EntityTypeNames.User);
  }


  public async activate(params) {
    let result = await this.service.createEntity();
    this.entity = result;
    this.entity.visibility = "Private";
    this.entity.authorId = this.authService.currentUser.id;
    if (params.callback != null) {
      await params.callback(this.entity);
    }
  }

  public async saveCurrentEntity() {
    await super.save(true, false, false);
    return this.entity;
  }

  public getAvatarUri(userId: number) {
    let agGridViewUser = this.entity.agGridViewUsers.find(x => x.userId == userId);
    return agGridViewUser?.user?.avatarUri;
  }
}