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

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

 // main data
 private _listPr: any[] = []
 private _listMd: any[] = []
 // local storage keyName
 private keyNamemd: string;
 private keyNamepr: string; 
 private _count = 0
 private _countPr = 0
 private _countMd = 0

  constructor() {

    this.keyNamemd = (SHA1(this.constructor.name+"md")).toString()
    this.keyNamepr = (SHA1(this.constructor.name+"pr")).toString()
    
    
    setTimeout(()=>{
      let restoreMd = localstoreService.get(this.keyNamemd, {})
      let restorePr = localstoreService.get(this.keyNamepr, {})

      this._listPr = restorePr.listPr || []
      this._listMd = restoreMd.lisMd || []
      this._countMd = parseInt(restoreMd.countListMd) || 0
      this._countPr = parseInt(restorePr.countListPr) || 0
      this._count =  (this._countMd + this._countPr) || 0
    }, 10)

  }

  get listpr() { return this._listPr }
  get listmd() { return this._listMd }
  
  get count() { return this._count  || 0 }
  set count(value: number) {
    this._count = value
  }

  get countPr() { return this._countPr  || 0 }
  set countPr (value: number) {
    this._countPr = value
  }

  get countMd() { return this._countMd  || 0 }
  set countMd (value) {
    this._countMd = value
  }

  resetpr(eventsList: any) {
    this._listPr = eventsList

    this.countPr = this._listPr.length
    this.count = this.countPr + this.countMd

    this.savePr()
  }

  resetmd(eventsList: any) {
    this._listMd = eventsList

    this.countMd = this._listMd.length
    this.count = this.countPr + this.countMd
    this.saveMd()
  }

  private saveMd() {
    setTimeout(()=>{
      localstoreService.set(this.keyNamemd,{
        lisMd: this._listMd,
        countListMd: this._countMd
      })
    }, 10)
    
  }

  private savePr() {
    setTimeout(()=>{ 
      localstoreService.set(this.keyNamepr,{
        listPr: this._listPr,
        countListPr: this._countPr
      })
    }, 10)
    
  }

}

export const EventoAprovacaoStore = new EventoaprovacaoStoreService()
