'use strict';

// @module profile factory

var multi_profile   = require('multi-profile');
var node_path       = require('path');
var node_url        = require('url');
var fs              = require('fs-sync');
var lang            = require('./util/lang');
var code            = require('code-this');

// 1. Make sure all values saved by cortex profile is exactly correct
// 2. Make sure all values read from the config file will be examined (user may change that).

var TYPES = {
    path: {
        getter: function (v) {
            // will resolve '~' in the path
            v = multi_profile.resolveHomePath(v);

            if(!fs.isPathAbsolute(v)){

                // convert to absolute path relative to the root directory of the current profile
                v = node_path.join(this.profile.currentDir(), v);
            }

            if(!fs.isDir(v)){
                fs.mkdir(v);
            }

            return v;
        }
    },

    url: {
        setter: function (v, key, attr) {
            v = node_url.parse(String(v));

            // must be an invalid url
            if (!v.host){
                attr.error('invalid url');
            }
            
            return v.href;
        }
    }
};


var DEFAULT_OPTIONS = {
    path: '~/.cortex',

    // configuration schema and default values
    schema: {
        // cortex_root     : {
        //     value       : cortex_root,
        //     type        : TYPES.path
        // },

        module_root     : {
            value       : 'modules',
            type        : TYPES.path
        },

        built_root      : {
            value       : 'built_modules',
            type        : TYPES.path
        },

        built_temp      : {
            value       : 'built',
            type        : {
                getter  : function (v) {
                    v = node_path.join('.cortex', v);

                    if(!fs.isDir(v)){
                        fs.mkdir(v);
                    }

                    return v;
                }
            }
        },

        temp_root       : {
            value       : 'tmp',

            // temp_root will not be saved into profile file
            readOnly    : true,
            type        : {
                getter  : function (v) {
                    // will resolve '~' in the path
                    v = multi_profile.resolveHomePath(v);

                    if(!fs.isPathAbsolute(v)){

                        // convert to absolute path relative to the root directory of the current profile
                        v = node_path.join(this.profile.currentDir(), v);
                    }

                    // Everytime it returns a different directory
                    v = node_path.join(
                        v, 
                        // must be string
                        String(Date.now())
                    );

                    if(!fs.isDir(v)){
                        fs.mkdir(v);
                    }

                    return v;
                }
            }
        },

        watched         : {
            enumerable  : false,
            type        : {
                getter  : function (v) {
                    var dir = this.profile.currentDir();
                    var file = node_path.join(dir, 'watch_list');
                    return fs.exists(file) ?
                        fs.read(file).split(/\r?\n/) :
                        [];
                },

                setter  : function (v) {
                    
                }
            }
        },
        
        registry        : {
            value       : 'http://registry.npmjs.org',
            type        : TYPES.url
        },

        registry_port   : {
            value       : 80,
            type        : {
                validator : function (v) {
                    return typeof v === 'string'
                }
            }
        },

        server_path     : {
            value       : '/mod',
            type        : {

                // 'mod' -> '/mod'
                getter  : function (v) {
                    return '/' + v.replace(/^\/+|\/+$/, '');
                }
            }
        },

        language        : {
            value       : 'en',

            // Uncompleted feature, set to `readOnly` by now
            readOnly    : true
        },

        colors          : {
            value       : true,
            type        : 'boolean'
        },

        service_port    : {

            // CTX -> 074
            value       : 9074,
            type        : 'number'
        },

        workspace       : {
            value       : ''
        },

        enable_snapshot : {
            value       : true,
            type        : 'boolean'
        },

        profile_root    : {
            readOnly    : true,
            type        : {
                getter: function () {
                    return this.profile.currentDir();
                }
            }
        },

        username        : {
            enumerable  : false
        },

        password        : {
            enumerable  : false
        }
    }
};


module.exports = function (options) {
    options = options || {};

    options.context = {};

    var profile = multi_profile(lang.mix(options, DEFAULT_OPTIONS, false));

    options.context.profile = profile;

    return profile;
};


@@ -2188,33 +2188,34 @@
         
-readOnly
+writable
     : 
-tru
+fals
 e,%0A     
@@ -3164,383 +3164,32 @@
     
-getter  : function (v) %7B%0A                    var dir = this.profile.currentDir();%0A                    var file = node_path.join(dir, 'watch_list');%0A                    return fs.exists(file) ?%0A                        fs.read(file).split(/%5Cr?%5Cn/) :%0A                        %5B%5D;%0A                %7D,%0A%0A                setter  : function (v) %7B%0A                    %0A                %7D
+validator: Array.isArray
 %0A   
@@ -3949,24 +3949,24 @@
 set to %60
-readOnly
+writable
 %60 by now
@@ -3978,33 +3978,34 @@
         
-readOnly
+writable
     : 
-tru
+fals
 e%0A      
@@ -4473,25 +4473,26 @@
     
-readOnly
+writable
     : 
-tru
+fals
 e,%0A 
@@ -4694,36 +4694,147 @@
         
-enumerable  : false%0A
+type        : %7B%0A                validator : function (v) %7B%0A                    if(!v)%7B%0A                        return;%0A            
         
@@ -4826,33 +4826,32 @@
                %7D
-,
 %0A%0A        passwo
@@ -4848,24 +4848,73 @@
     
-password
+            if( ~ encodeURIComponent(v).indexOf('%25') )%7B%0A 
         
 : %7B%0A
@@ -4901,36 +4901,35 @@
 %25') )%7B%0A         
-: %7B%0A
+   
             enum
@@ -4924,35 +4924,151 @@
         
-enumerable  : false
+return;%0A                    %7D%0A%0A                    return true;%0A                %7D%0A            %7D%0A        %7D,%0A%0A        password        : %7B
 %0A       

update to trait@0.1.2