import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { UsersAccess } from '../gdel.component';

@Component({
  selector: 'app-access-dialog',
  templateUrl: './access-dialog.component.html',
  styleUrls: ['./access-dialog.component.css']
})
export class AccessDialogComponent implements OnInit {
  idFile: string;
  usersAccess: UsersAccess[];
  isDeleted: boolean;

  avatarColors = {
    A: 244, B: 67, C:  54, D: 233, E: 30, F:  99, G: 156, H:  39, I: 176, 
    J: 103, K: 58, L:  58, M: 183, N: 63, Ñ:  81, O: 181, P:  33, Q: 150, 
    R: 243, S:  3, T: 169, U: 244, V: 76, W: 175, X:  80, Y: 255, Z: 193
  };

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
  ) {
    this.idFile = '';
    this.usersAccess = [];
    this.isDeleted = false;
  }

  ngOnInit(): void {
    this.idFile = this._data.idFile;
    this.usersAccess = this.getUsersAccess(this._data.accessStr);
    this.isDeleted = this._data.isDeleted;
  }

  getUsersAccess(accessStr: string): UsersAccess[]{
    let accessArr = JSON.parse(accessStr);
    let arrFn: UsersAccess[] = [];

    for(const user of accessArr){
      let userNameArr = user.split(' ');
      let userLabel = '';

      for(let i = 0; i < 2; i++){
        userLabel += userNameArr[i][0];
      }

      let userColor = "rgb(";
      for(let i = 0; i < 3; i++){
        let char: never = user.charAt(i) as never;
        let code = this.avatarColors[char];

        userColor += `${code}, `;
      }

      userColor = userColor.substring(0, userColor.length - 2);
      userColor += ")";

      let userObj: UsersAccess = {
        userLabel: userLabel,
        userName: user,
        userColor: userColor,
        isTrigger: false,
      }

      arrFn.push(userObj);
    }

    return arrFn;
  }
}
