var express = require('express');
var fs = require("fs");
var less = require("less");
var _3builder = require("./3builder");
var Path = require("path");
var http = require('http');
var url = require("url");

module.exports = {
  
  startServer: function(){

    var app = express();
    // all environments
    app.set('port', process.env.PORT || 3000);
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);

    app.get("/rodco/:appName/assets/:asset", function(req, res) {
      res.setHeader("Content-Type", "text-javascript");
      var asset = req.params.asset;
      var appName = req.params.appName;
      fs.readFile( Path.join(  process.cwd() , "apps", appName, "app", "assets", asset ) , 
        function(err, contents){
          if(err){
            res.status(500);
            return res.send(err);
          }
          res.send(contents)
        }
      );
    });

    app.get("/dependencies/:name", function(req, res) {
      res.setHeader("Content-Type", "text-javascript");

      fs.readFile( Path.join( process.cwd(), "apps", "dependencies", req.params.name ) , 
        function(err, file){
          if(err){
            //get App Name From req.Host          
            var urlParts = req.headers.referer.split("/")
            return res.redirect("/rodco/dependency/" + urlParts[ urlParts.length -1 ] +  "/build");
          }
          return res.send(file);    
        }
      );
    });

    app.get('/dependencies/:app/build', 
      function(req, res) {
        res.setHeader("Content-Type", "text-javascript");
        var appName = req.params.name
        _3builder.buildDependency( appName )
        .then( 
          function( contents ){
            return res.send(contents);
          } 
        );
      }
    );

    app.get("/rodco/:appName/:device", function(req, res) {
      res.setHeader("Content-Type", "text-javascript");
      var device = req.params.device;
      var appName = req.params.appName;
      fs.readFile( Path.join(  process.cwd() , "apps", appName, "app", device) , 
        function(err, contents){
          if(err){
            res.status(500);
            return res.send(err);
          }
          res.send(contents)
        }
      );
    });

    app.get('/rodco/:appName', 
      function(req, res) {
        res.setHeader("Content-Type", "text-javascript");
        var baseDir = process.cwd();
        var appName = req.params.appName;
        pck = fs.readFileSync( Path.join( baseDir, "apps", appName, "package.json"  ), "utf-8"  );
        _3builder.buildApp( appName )
        .then( 
          function( html ){
            html = html.replace("3vot.domain = 'demo.3vot.com';","3vot.domain = 'localhost:"+ app.get('port') +"';")
            return res.send( html );
          } 
        ).fail( function(error){  res.send(arguments) } );
      }
    );

    http.createServer(app).listen(app.get('port'), function(){
      console.log('Express server listening on port ' + app.get('port'));
    });
  }
};
@@ -201,16 +201,17 @@
 url%22);%0A%0A
+%0A
 module.e
@@ -273,24 +273,127 @@
  express();%0A
+    %0A    var pck = require( Path.join( process.cwd(), %223vot.json%22 )  );%0A    var profile = pck.profile;%0A
     // all e
@@ -583,24 +583,25 @@
 p.router);%0A%0A
+%0A
     app.get(
@@ -594,37 +594,48 @@
 %0A%0A    app.get(%22/
-rodco
+%22 + profile  + %22
 /:appName/assets
@@ -1085,16 +1085,33 @@
 app.get(
+%22/%22 + profile  + 
 %22/depend
@@ -1499,17 +1499,19 @@
 ependenc
-y
+ies
 /%22 + url
@@ -1641,17 +1641,34 @@
 app.get(
-'
+%22/%22 + profile  + %22
 /depende
@@ -1681,15 +1681,19 @@
 :app
+Name
 /build
-'
+%22
 , %0A 
@@ -1809,17 +1809,20 @@
 .params.
-n
+appN
 ame%0A    
@@ -2003,21 +2003,32 @@
 p.get(%22/
-rodco
+%22 + profile  + %22
 /:appNam
@@ -2476,15 +2476,26 @@
 get(
-'/rodco
+%22/%22 + profile  + %22
 /:ap
@@ -2499,17 +2499,17 @@
 :appName
-'
+%22
 , %0A     

fix bug on dev server with profile name