import { Injectable } from '@angular/core';
import { localstoreService } from './localstore.service'
import { SHA1 }  from  'crypto-js'
import { customTask } from '../models/dailyworktask.model';
import { ObjectQueryService } from '../services/object-query.service';

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

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

  ObjectQueryService  = new  ObjectQueryService()

  constructor() {

    this.keyName = (SHA1(this.constructor.name+ 'home/eventSource')).toString()
    window['ObjectQueryService'] = this.Query()

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


    setTimeout(()=>{

      // this.Query().Update(
      //   (select:customTask): boolean => select.Folio == 'Formação',
      //   (update:customTask) => update.Folio = 'Formação 5'
      // )

        // this.Query().Update(
        //   (select:customTask): boolean => select.Folio == 'Formação',
        //   (update:customTask) => {
        //     update.Folio = 'Formação 7';
        //     update.DocumentURL = 'peter';
        //   }
        // )

      // this.Query().Update(
      //   (select:customTask): boolean => select.Folio == 'Formação',
      //   (update:customTask): customTask => ({
      //     CreateDate: '',
      //     DocId: 0,
      //     DocumentURL: '',
      //     DocumentsQty: '',
      //     FolderID: 0,
      //     Folio:' ',
      //     Remetente: '',
      //     Senders: '',
      //     SerialNumber: '',
      //     Status: '',
      //     WorkflowName: '',
      //     activityInstanceName: ''
      //   })
      // )

      // this.Query().Delete(
      //   (select: customTask): boolean => select.DocId == 3 && select.DocId >= 1
      // )
      
    }, 5000)
    
  }

  Query() {
    return {
      Update: (select:Function, update:Function) => {
        this.ObjectQueryService.Update(select, update, this._list )
        this.save({dynamicCount : true})
      },
      select:  (select) => {
        this.ObjectQueryService.select(select, this._list )
        this.save({dynamicCount : true})
      },
      Delete : (select) => {
        this.ObjectQueryService.Delete(select, this._list )
        this.save({dynamicCount : true})
      },
      Insert: (insert: customTask | customTask[]) =>{
        this.ObjectQueryService.Insert(insert, this._list)
        this.save({dynamicCount : true})
      }
    }
  }

  ObjectQuery() {}

  get list(): customTask[] {
    return this._list || []
  }

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

  reset(eventsList: any) {
    this._list = eventsList
    this.save({dynamicCount:true})
  }

  private save = ({dynamicCount = false})=> {

    if(dynamicCount) {
      this._count = this._list.length
    }

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

}

export const DespachoStore = new DespachoStoreService()
