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

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

  private _listParecer: Event[] = []
  private _listDeferimento: Event[] = []
  // local storage keyName
  private keyNameparecer: string;
  private keyNamedeferiemnto: string; 
  private _countparecer = 0
  private _countDeferiemnto = 0
 
  constructor() {
 
    this.keyNameparecer = (SHA1(this.constructor.name+"parecer")).toString()
    this.keyNamedeferiemnto = (SHA1(this.constructor.name+"deferimneto")).toString()
    
    setTimeout(()=>{ 
      let restoreParecer = localstoreService.get(this.keyNameparecer, {})
      let restoreDeferimento = localstoreService.get(this.keyNamedeferiemnto, {})
      
      this._listParecer = restoreDeferimento.listDeferimento || []
      this._listDeferimento = restoreParecer.listParecer || []
      this._countparecer =  parseInt(restoreParecer.count) || 0
      this._countDeferiemnto = parseInt(restoreDeferimento.count) || 0
      
    }, 10)
 
  }
 
  get listparecer() {
    return this._listParecer
  }
 
  get listdeferimento() {
   return this._listDeferimento
  }

  get countparecer() {
    return this._countparecer || 0
  }
  set countparecer(value: number) {
    this._countparecer = value
    this.saveParecer()
  }

  get countdeferimento() {
    return this._countDeferiemnto || 0
  }
  set countdeferimento(value: number ) {
    this._countDeferiemnto = value
    this.saveDeferimento()
  }
 
  resetparecer(eventsList: any) {
    this._listParecer = eventsList
 
    this.countparecer = this._listParecer.length
    this.saveDeferimento()
  }
 
  resetdeferimento(list: any) {
    this._listDeferimento = list
 
   this.countdeferimento =this._listDeferimento.length
   this.saveParecer()
  }

  private saveParecer() {
    setTimeout(()=>{ 
      localstoreService.set(this.keyNameparecer,{
        listParecer: this._listParecer,
        count: this._countparecer,
      })
    }, 10)
    
  }
 
  private saveDeferimento() {
    setTimeout(()=>{ 
      localstoreService.set(this.keyNamedeferiemnto,{
        listDeferimento: this._listParecer,
        count: this._countDeferiemnto,
      })
    }, 10)
    
  }

}

export const PedidosStore = new PedidosStoreService()
