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

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

  // main data
  private _list = []
  // local storage keyName
  private keyName: string; 
  private _count = 0

  constructor() {

    this.keyName = (SHA1(this.constructor.name)).toString()
    
    
    setTimeout(()=>{ 
      let restore = localstoreService.get(this.keyName, {})
      this._list = restore.list || []
      this._count = parseInt(restore.count) || 0
    }, 10)

  }
  
  get list() {
    return this._list
  }

  get count() {
    return this._count  || 0
  }
  set count(value) {
    this._count = value
  }

  reset(eventsList: any) {
    this._list = eventsList

    this.count = this._list.length
    this.save()
  }

  private save() {
    setTimeout(()=>{ 
      localstoreService.set(this.keyName,{
        list: this._list,
        count: this._list.length
      })
    }, 10)
    
  }

}

export const PendentesStore = new PendestesStoreService()
