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

        @save()

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

    add_multiple_entries: (entries=[], save=true)->
        for e in entries
            @new_entry e, false
        if save then @save()
        return

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

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

        if log_file != 'log.json' and not fs.existsSync(log_file) and fs.existsSync 'log.json'
            fs.rename 'log.json', log_file
            console.log 'Moving log.json to ' + log_file

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