 fs = require 'fs-extra'
 glob = require 'glob'
 path = require 'path-extra'
+zlib = require 'zlib'
 
 # constant
 window.APPDATA = path.join(window.APPDATA_PATH, 'battle-detail')
 REFRESH_INTERVAL = 60000
-FS_RW_OPTIONS =
-  encoding: 'UTF-8'
 
 # init
 fs.ensureDir APPDATA, (error) ->

  _refreshPacketSync: ->
    return unless (Date.now() - @packetListLastRefresh) > REFRESH_INTERVAL
    # list = fs.readdirSync APPDATA
    list = glob.sync path.join(APPDATA, "*.json")
    list = list.map (p) -> parseInt path.parse(p).name
    list.sort()
    @packetListLastRefresh = Date.now()
    @packetList = list


  savePacketSync: (id, packet) ->
    return unless id? and packet?
    try
      name = "#{id}.json.gz"
      fpath = path.join(APPDATA, name)
      data = zlib.gzipSync(JSON.stringify(packet))
      fs.writeFileSync(fpath, data)

      @packetList.push(id)
      @packetFile[id] =
        name: name
        packet: packet
    catch error
      console.error error

  loadPacket: (id, callback) ->
    return unless id? and callback?
