 status-message = require './status-message'
 
 class FileHandler
-    settings: {}
-
     constructor: (session) ->
         @session = session
+        @settings = {}
+        @readbytes = 0
+        @closed = false
+
+    set: (index, value) ->
+        @settings[index] = value
+        if index == "display-name"
+            @remote_address = value.split(":")[0]
+            @basename = path.basename(value.split(":")[1])
+        else if index == "data"
+            @datasize = parseInt(value,10)
+
+    get: (index) ->
+        return @settings[index]
 
     create: ->
         @tempfile = path.join(os.tmpdir(), randomstring(10), @basename)

        mkdirp.sync(dirname)
        @fd = fs.openSync(@tempfile, 'w')

    write: (str) ->
        fs.writeSync(@fd, str)

    open: ->
        fs.closeSync @fd
        console.log "[ratom] opening #{@tempfile}"
        # register events
        atom.workspace.open(@tempfile, activatePane:true).then (editor) =>


    save: ->
        if not @session.alive
            console.log "[ratom] Error saving #{path.basename @tempfile} to #{@remote_address}"
            status-message.display "Error saving #{path.basename @tempfile} to #{@remote_address}", 2000
            return
        console.log "[ratom] saving #{path.basename @tempfile} to #{@remote_address}"
        status-message.display "Saving #{path.basename @tempfile} to #{@remote_address}", 2000
        @session.send "save"
        @session.send "token: #{@settings['token']}"
        data = fs.readFileSync(@tempfile)
        @session.send "data: " + Buffer.byteLength(data)
        @session.send data

    close: ->
        console.log "[ratom] closing #{path.basename @tempfile}"
        @subscriptions.dispose()
        @session.try_close()

class Session
    constructor: (socket) ->
        @should_parse_data = false
        @nconn = 0
        @socket = socket
        @send "Atom "+ atom.getVersion()
        @alive = true
