import {Injectable} from '@angular/core';
import {PunishmentHistory} from '@api';

@Injectable({
  providedIn: 'root'
})
export class HistoryFormatService {

  constructor() {
  }

  public getPunishmentTime(entry: PunishmentHistory) {
    const date = new Date(entry.punishmentTime);
    return date.toLocaleString(navigator.language, {
      year: 'numeric',
      month: 'short',
      day: 'numeric',
      hour: '2-digit',
      minute: '2-digit',
      hour12: false
    });
  }

  public isActive(entry: PunishmentHistory): boolean {
    if (entry.removedBy !== null) {
      return false;
    }
    if (entry.expiryTime <= 0) {
      return true;
    }
    return entry.expiryTime > Date.now();
  }

  public isPermanent(entry: PunishmentHistory): boolean {
    return entry.expiryTime <= 0;
  }

  public getType(entry: PunishmentHistory): string {
    return entry.type.charAt(0).toUpperCase() + entry.type.slice(1);
  }

  public getExpiredTime(entry: PunishmentHistory): string {
    let suffix: string = '';
    if (entry.removedBy !== null && entry.removedBy !== undefined) {
      suffix = '(Unbanned by ' + entry.removedBy + ')';
    }
    if (entry.expiryTime <= 0) {
      return "Permanent " + this.getType(entry) + " " + suffix;
    }
    const date = new Date(entry.expiryTime);
    return date.toLocaleString(navigator.language, {
      year: 'numeric',
      month: 'short',
      day: 'numeric',
      hour: '2-digit',
      minute: '2-digit',
      hour12: false
    }) + " " + suffix;
  }

  public getAvatarUrl(entry: string, size: string = '25'): string {
    let uuid = entry.replace('-', '');
    if (uuid === 'C' || uuid === 'Console' || uuid === '[Console]') {
      uuid = "f78a4d8dd51b4b3998a3230f2de0c670"
    }
    return `https://crafatar.com/avatars/${uuid}?size=${size}&overlay`;
  }
}
