'use strict';

/* Services */

angular.module('glark.services', [])
    .factory('ace', function () {
        return ace.edit('editor');
    })

    .factory('EditSession', function () {
        return ace.require("ace/edit_session").EditSession;
    })

    .factory('editor', function (EditSession, ace) {
        ace.setTheme("ace/theme/twilight");

        return {
            focus: function () {
                setTimeout(function () {
                    ace.focus();
                }, 0);
            },

            setSession: function (session) {
                session.setFoldStyle('markbegin');

                ace.setSession(session);
                ace.setReadOnly(false);
            },

            clearSession: function () {
                ace.setSession(new EditSession(''));
                ace.setReadOnly(true);
            },

            goToLine: function (lineNumber) {
                ace.gotoLine(lineNumber);
            },

            find: function (value) {
                return ace.find(value);
            },

            findNext: function () {
                return ace.findNext();
            },

            findPrevious: function () {
                return ace.findPrevious();
            }
        };
    })

    .factory('File', function (EditSession) {
        var readAndSetFileContent = function (file, fileEntry) {
            var reader = new FileReader();
            reader.onload = function (event) {
                file.session.setValue(event.target.result, -1);
            };
            reader.readAsText(fileEntry);
        };

        return function (fileEntry) {
            this.fileEntry = fileEntry;
            this.name = fileEntry.name;
            this.session = new EditSession('');

            // TODO: Should be changed.
            this.session.setMode("ace/mode/javascript");

            readAndSetFileContent(this, fileEntry);
        };
    })

    .factory('workspace', function ($rootScope) {
        return {
            files: [],
            activeFile: null,
            
            addFile: function (file) {
                this.files.push(file);
            },

            getFileCount: function () {
                return this.files.length;
            },

            setActiveFile: function (file) {
                this.activeFile = file;
            },

            getActiveFile: function () {
                return this.activeFile;
            }
        };
    });

@@ -1246,32 +1246,36 @@
       %7D;%0A    %7D)%0A
+    
 %0A    .factory('F
@@ -1273,20 +1273,26 @@
 actory('
-F
+f
 ile
+system
 ', funct
@@ -1300,43 +1300,56 @@
 on (
-EditSession) %7B%0A        var readAndS
+$q, $rootScope) %7B%0A        return %7B%0A            g
 etFi
@@ -1357,18 +1357,17 @@
 eContent
- =
+:
  functio
@@ -1373,14 +1373,8 @@
 on (
-file, 
 file
@@ -1374,32 +1374,78 @@
 n (fileEntry) %7B%0A
+                var defered = $q.defer();%0A    
             var 
@@ -1467,24 +1467,28 @@
 leReader();%0A
+    
             
@@ -1542,28 +1542,26 @@
     
-file.session.setValu
+    defered.resolv
 e(ev
@@ -1581,15 +1581,57 @@
 sult
-, -1);%0A
+);%0A                    $rootScope.$digest();%0A    
     
@@ -1637,24 +1637,28 @@
          %7D;%0A
+    
             
@@ -1695,18 +1695,138 @@
         
-%7D;
+        return defered.promise;%0A            %7D%0A        %7D;%0A    %7D)%0A%0A    .factory('File', function (filesystem, EditSession) %7B
 %0A%0A      
@@ -1865,28 +1865,70 @@
             
-this
+var file = this;%0A            %0A            file
 .fileEntry =
@@ -1951,20 +1951,20 @@
         
-this
+file
 .name = 
@@ -1983,36 +1983,36 @@
 me;%0A            
-this
+file
 .session = new E
@@ -2080,20 +2080,20 @@
         
-this
+file
 .session
@@ -2121,24 +2121,36 @@
 vascript%22);%0A
+            
 %0A           
@@ -2154,45 +2154,171 @@
     
-readAndSetFileContent(this, fileEntry
+var promise = filesystem.getFileContent(fileEntry);%0A            promise.then(function (content) %7B%0A                file.session.setValue(content, -1);%0A            %7D
 );%0A 

Create filesystem services.