 fs = require 'fs'
-
+electron = require 'electron'
+ewin = electron.remote.getCurrentWindow()
+{log_file} = ewin.settings
 class Log
     constructor: (entries=[])->
         @entries = []

log.get_load_promise = ->
    new Promise (resolve, reject) ->
        if fs.existsSync 'log'
            fs.rename('log', 'log.json')
            console.log 'Old log file found. Renamed to log.json'

        fs.readFile 'log.json', 'utf8', (err, data)->
            old_log = []
            if err
                console.log err

        @save()

    save: ->
        str_entries = JSON.stringify(@entries, null, if ewin.isDebug then 4 else null)
        #saving backup
        localStorage.myoulog_backup = str_entries
        console.log 'Saving log file...'
        fs.writeFile log_file, str_entries, (err)->
            if err
                console.log err
            else
                console.log 'Log file saved here: ', log_file

    get_reward: (ratio=2/8, date_range=[0,Date.now()])->
        reward = 0
        for {active, date}, i in @entries
            if date_range[1] > date > date_range[0]
                if active
                    reward += @get_duration i
                else
                    reward -= @get_duration i
                    reward = Math.max 0, reward

        return reward*ratio

    get_duration: (index)->
        entry = @entries[index]
