/*global describe it before after  =*/

"use client";

require(["lib/architect/architect", "lib/chai/chai", "/vfs-root"], 
  function (architect, chai, baseProc) {
    var expect = chai.expect;
    
    document.body.appendChild(document.createElement("div"))
        .setAttribute("id", "saveStatus");
    
    architect.resolveConfig([
        {
            packagePath : "plugins/c9.core/c9",
            startdate   : new Date(),
            debug       : true,
            hosted      : true,
            local       : false,
            davPrefix   : "/"
        },
        
        "plugins/c9.core/ext",
        "plugins/c9.core/http",
        "plugins/c9.core/util",
        "plugins/c9.ide.ui/lib_apf",
        "plugins/c9.ide.ui/tooltip",
        "plugins/c9.ide.ui/menus",
        {
            packagePath: "plugins/c9.core/settings",
            testing: true
        },
        {
            packagePath  : "plugins/c9.ide.ui/ui",
            staticPrefix : "plugins/c9.ide.ui"
        },
        "plugins/c9.ide.editors/document",
        "plugins/c9.ide.editors/undomanager",
        {
            packagePath: "plugins/c9.ide.editors/editors",
            defaultEditor: "texteditor"
        },
        "plugins/c9.ide.editors/editor",
        "plugins/c9.ide.editors/tabmanager",
        "plugins/c9.ide.editors/pane",
        "plugins/c9.ide.editors/tab",
        "plugins/c9.ide.ace/ace",
        "plugins/c9.ide.save/save",
        "plugins/c9.ide.save/autosave",
        {
            packagePath : "plugins/c9.vfs.client/vfs_client"
        },
        "plugins/c9.vfs.client/endpoint",
        "plugins/c9.ide.auth/auth",
        {
            packagePath: "plugins/c9.fs/fs",
            baseProc: baseProc
        },
        "plugins/c9.fs/fs.cache.xml",
        
        // Mock plugins
        {
            consumes : ["apf", "ui", "Plugin"],
            provides : [
                "commands", "menus", "commands", "layout", "watcher", "save", 
                "anims", "tree", "preferences", "clipboard", "dialog.alert",
                "dialog.question", "dialog.filesave", "dialog.fileoverwrite",
                "auth.bootstrap", "ace.stripws"
            ],
            setup    : expect.html.mocked
        },
        {
            consumes : ["tabManager", "save", "fs", "autosave", "settings"],
            provides : [],
            setup    : main
        }
    ], function (err, config) {
        if (err) throw err;
        var app = architect.createApp(config);
        app.on("service", function(name, plugin){ plugin.name = name; });
    });
    
    function main(options, imports, register) {
        var tabs     = imports.tabManager;
        var fs       = imports.fs;
        var save     = imports.save;
        var autosave = imports.autosave;
        var settings = imports.settings;
        
        function countEvents(count, expected, done){
            if (count == expected) 
                done();
            else
                throw new Error("Wrong Event Count: "
                    + count + " of " + expected);
        }
        
        expect.html.setConstructor(function(tab){
            if (typeof tab == "object")
                return tab.pane.aml.getPage("editor::" + tab.editorType).$ext;
        });
        
        function changeTab(path, done){
            var tab = tabs.findTab(path);
            tabs.focusTab(tab);
            tab.document.undoManager.once("change", done);
            tab.document.editor.ace.insert("test");
            return tab;
        }
        
        describe('autosave', function() {
            this.timeout(2000)
            
            before(function(done){
                apf.config.setProperty("allow-select", false);
                apf.config.setProperty("allow-blur", false);
                
                tabs.getPanes()[0].focus();
                
                var path = "/autosave1.txt";
                fs.writeFile(path, path, function(){
                    tabs.openFile(path, function(){
                        done();
                    });
                });
                
                settings.set("user/general/@autosave", "true");
                
                bar.$ext.style.background = "rgba(220, 220, 220, 0.93)";
                bar.$ext.style.position = "fixed";
                bar.$ext.style.left = "20px";
                bar.$ext.style.right = "20px";
                bar.$ext.style.bottom = "20px";
                bar.$ext.style.height = "150px";
      
                document.body.style.marginBottom = "180px";
            });
            
            it('should automatically save a tab that is changed', function(done) {
                var path  = "/autosave1.txt";
                var tab = changeTab(path, function(){
                    expect(tab.document.changed).to.ok
                    
                    save.once("afterSave", function(){
                        fs.readFile(path, function(err, data){
                            if (err) throw err;
                            expect(data).to.equal("test" + path);
                            
                            fs.unlink(path, function(){
                                done();
                            })
                        });
                    })
                });
            });
            
            if (!onload.remain){
                describe("unload()", function(){
                    it('should destroy all ui elements when it is unloaded', function(done) {
                        autosave.unload();
                        done();
                    });
                });
                
                //@todo Idea: show in the tabs whether the editor is running atm
                // @todo test fs integration
                
                after(function(done){
                    document.body.style.marginBottom = "";
                    tabs.unload();
                    done();
                });
            }
        });
        
        onload && onload();
    }
});
@@ -309,31 +309,33 @@
     
-architect.resolveConfig
+expect.setupArchitectTest
 (%5B%0A 
@@ -2173,16 +2173,24 @@
 stripws%22
+, %22proc%22
 %0A       
@@ -2413,187 +2413,17 @@
  %5D, 
-function (err, config) %7B%0A        if (err) throw err;%0A        var app = architect.createApp(config);%0A        app.on(%22service%22, function(name, plugin)%7B plugin.name = name; %7D);%0A    %7D
+architect
 );%0A 

fix remaining tests