import { Injectable } from '@angular/core';
import * as _sodium from 'libsodium-wrappers';
import { public_key, secret_key } from 'src/environments/environment';

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

  constructor() {}

  private ord(str: string){return str.charCodeAt(0);}

  encriptar(valor: string) {
    var nonce:Uint8Array = _sodium.randombytes_buf(_sodium.crypto_box_NONCEBYTES);
    console.log(nonce);
    
    
  }
 /*  
 encriptar(valor: string) {
    var nonce = _sodium.randombytes_buf(_sodium.crypto_box_NONCEBYTES);
    
    var encoded = _sodium.crypto_box_easy(
      valor,
      nonce,
      this.stringToUT8(public_key),
      this.stringToUT8(secret_key)
    );

    var data_json = JSON.stringify(encoded);
    var nonce_json = JSON.stringify(nonce);

    let data_base64 = window.btoa(data_json);
    let nonce_base64 = window.btoa(nonce_json);

    let enc = window.btoa(data_base64 + '|' + nonce_base64);

    return enc;
    
  } */

   desencriptar(valor: string) { 
    let valor_base64 = window.atob(valor);
    let arrEnc = valor_base64.split('|');

    let nonce = window.atob(arrEnc[1]);
    let nonce_arr = JSON.parse(nonce);
    let nonce_final = new Uint8Array(nonce_arr);

    let enc = window.atob(arrEnc[0]);
    let enc_arr = JSON.parse(enc);    
    let enc_final = new Uint8Array(enc_arr);   
    
    
    var decoded = _sodium.crypto_box_open_easy(
      enc_final,
      nonce_final,
      this.stringToUT8(public_key),
      this.stringToUT8(secret_key)
    );
    
    let dec = this.UT8ToString(decoded);

    return dec;
    
  }

  private stringToUT8(key: string): Uint8Array {
    let pk_dec = window.atob(key);
    let arr_enc = [];

    for (let index = 0; index < pk_dec.length; index++) {
      arr_enc.push(pk_dec.charCodeAt(index));
    }

    var arr = new Uint8Array(arr_enc);
    return arr;
  }

  private UT8ToString(arr: Uint8Array): string {
    let dec: string = '';
    arr.forEach((element) => {
      dec += String.fromCharCode(element);
    });
    return dec;
  }

  /* private stringToUT8_API(key: string): Uint8Array {
    let arr_enc = [];

    for (let index = 0; index < key.length; index++) {
      arr_enc.push(key.charCodeAt(index));
    }

    var arr = new Uint8Array(arr_enc);
    return arr;
  }

  private utf8_to_b64(str: string) {
    return window.btoa(unescape(encodeURIComponent(str)));
  }

  private b64_to_utf8(str: string): string {
    return window.atob(str);
  }

  private encondeToBase64(data: string): string {
    return unescape(encodeURIComponent(data));
  }

  private Utf8ArrayToStr(array: any) {
    var out, i, len, c;
    var char2, char3;

    out = '';
    len = array.length;
    i = 0;
    while (i < len) {
      c = array[i++];
      switch (c >> 4) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
          // 0xxxxxxx
          out += String.fromCharCode(c);
          break;
        case 12:
        case 13:
          // 110x xxxx   10xx xxxx
          char2 = array[i++];
          out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
          break;
        case 14:
          // 1110 xxxx  10xx xxxx  10xx xxxx
          char2 = array[i++];
          char3 = array[i++];
          out += String.fromCharCode(
            ((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
          );
          break;
      }
    }

    return out;
  } */
}
