import { Injectable } from '@angular/core';
import { localstoreService } from './localstore.service'
import { AES, enc, SHA1 }  from  'crypto-js'

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

  private _diplomasAssinadoList  = []
  private _diplomasList  = []
  private _diplomasReviewList = []

  private keyNameDiplomasAssinado: string;
  private keyNameDiplomasList: string;

  private _diplomasAssinadoListCount  = 0
  private _diplomasListCount  = 0
  private _diplomasReviewCount = 0

  constructor() { 
    this.keyNameDiplomasAssinado = (SHA1(this.constructor.name+"diplomasAssinado")).toString()
    this.keyNameDiplomasList = (SHA1(this.constructor.name+"diplomasList")).toString()


    setTimeout(()=>{
      let restoreDiplomasAssinado = localstoreService.get(this.keyNameDiplomasAssinado, {})
      let restoreDiplomasList = localstoreService.get(this.keyNameDiplomasList, {})
      let restoreDiplomasReviewList = localstoreService.get(this.keyNameDiplomasList, {})

      this._diplomasAssinadoList = restoreDiplomasAssinado.list || []
      this._diplomasAssinadoListCount = parseInt(restoreDiplomasAssinado.count)  || 0

      this._diplomasList = restoreDiplomasList.list || []
      this._diplomasListCount = parseInt(restoreDiplomasList.count) || 0


      this._diplomasReviewList = restoreDiplomasReviewList.list  || []
      this._diplomasReviewCount = parseInt(restoreDiplomasReviewList.count)  || 0

    }, 10)
    
  }


  get  deplomasReviewCount() {
    return this._diplomasReviewCount
  }
  set deplomasReviewCount(arg: number) {
    this._diplomasReviewCount = arg
    this.saveDiplomasReviewList()
  }

  get countDiplomasAssinadoListCount() {
    return this._diplomasAssinadoListCount || 0
  }

  set countDiplomasListCount(value) {
    this._diplomasAssinadoListCount = value
    this.saveDiplomasAssinadoList()
  }

  get diplomasListCount() {
    return this._diplomasListCount || 0
  }

  set diplomasListCount(value) {
    this._diplomasListCount = value
    this.saveDiplomasList()
  }

  get diplomasList() {
    return this._diplomasList
  }

  get diplomasAssinadoList() {
    return this._diplomasAssinadoList
  }

  resetDiplomasAssinadoList(value: any[]) {
    
    this._diplomasAssinadoListCount = value.length
    this._diplomasAssinadoList = value
    this.saveDiplomasAssinadoList()
  }

  resetDiplomasList(value: any[]) {
    
    this._diplomasListCount = value.length
    this._diplomasList = value
    this.saveDiplomasList()
  }

  resetDiplomasReview(value: any[]) {
    this._diplomasReviewList =value
    this._diplomasReviewCount = value.length
    this.saveDiplomasReviewList()
  }


  private saveDiplomasReviewList () {
    setTimeout(()=>{ 
      localstoreService.set(this.keyNameDiplomasList,{
        list: this._diplomasReviewList,
        count: this._diplomasReviewCount,
      })
    }, 10)
  }
  
  private saveDiplomasAssinadoList() {
    
    setTimeout(()=>{ 
      localstoreService.set(this.keyNameDiplomasAssinado,{
        list: this._diplomasAssinadoList,
        count: this._diplomasAssinadoListCount,
      })
    }, 10)    
  }

  private saveDiplomasList () {
    setTimeout(()=>{ 
      localstoreService.set(this.keyNameDiplomasList,{
        list: this._diplomasList,
        count: this._diplomasListCount,
      })
    }, 10)
  }

}

export let DeplomasStore = new DeplomasService() 
