"use strict";
var fs = require( 'fs' );
var path = require( 'path' );
var util = require( './util' ).obj;
var bUtil = require( './util/build.util' ).obj;

var conf = require( "../config/build.config" );

exports.build_cmd = [ {
    "info": "创建项目目录",
    "cmd": "build_project"
}, {
    "info": "复制默认的项目配置",
    "cmd": "copy_project_conf"
}, {
    "info": "创建项目频道",
    "cmd": "build_module"
} ];

exports.init = function() {
    var project_name;
    if ( arguments.length > 1 ) {
        project_name = arguments[ 1 ];
    } else {
        project_name = bUtil.getName( "请输入您要创建的项目名：" );
    }
    if ( !project_name ) {
        return;
    }
    exports.cmd( project_name );
};
exports.cmd = function( project_name ) {
    exports[ bUtil.select_cmd( exports.build_cmd ) ]( project_name );
};
exports.build_project = function( project_name, unNext ) {
    project_name = project_name || bUtil.getName( "请输入您要创建的项目名：" );
    if ( project_name === "" ) {
        return util.log( "创建项目结束！！" );
    }
    if ( fs.existsSync( project_name ) ) {
        return util.log( "项目目录已存在！" );
    }
    util.log( "创建工程目录 " + project_name );

    var dir = conf.default_dir;
    for ( var i = 0; i < dir.length; i++ ) {
        util.mkdir( project_name + dir[ i ] );
    }

    util.log( "创建工程目录 " + project_name + " 完成！！" );

    !unNext && exports.cmd( project_name );
};
exports.copy_project_conf = function( project_name, unNext ) {
    project_name = project_name || bUtil.getName( "请输入您要创建的项目名：" );
    if ( project_name === "" ) {
        return util.log( "创建项目结束！！" );
    }
    if ( fs.existsSync( __dirname + "/" + project_name ) ) {
        if ( readlineSync.question( "项目目录已存在！继续操作会覆盖原有配置。是否继续?（y/n）" ) !== "y" ) {
            return;
        }
    }
    util.copyFile( __dirname + "/file/hoofile.js", project_name + "/hoofile.js", function( data ) {
        return data.replace( /\{\$projectName\}/g, project_name );
    } );
    //copy( path + "/file/package.json", project_name + "/package.json" );

    !unNext && exports.cmd( project_name );
};
exports.build_module = function( project_name, unNext ) {
    var project_dir, prj_hoo_conf, module_name;

    project_name = project_name || bUtil.getName( "请输入您要创建的项目名：" );
    if ( project_name === "" ) {
        return util.log( "创建项目结束！！" );
    }

    project_dir = path.join( __dirname, project_name );
    if ( !fs.existsSync( project_dir ) ) {
        return util.log( "不存在 " + project_name + " 此项目！！" );
    }

    if ( ( prj_hoo_conf = exports.is_project_dir( path.join( project_dir, conf.hoofile ) ) ) === false ) {
        return util.error( "请在项目目录里创建项目的频道！！" );
    }

    if ( ( module_name = bUtil.getName( "请输入您要创建的模块名：" ) ) === "" ) {
        return;
    }

    if ( !module_name ) {
        return util.error( "请输入你要创建的频道名！！" );
    }

    var tplSrc = prj_hoo_conf.set.tplSrc;

    util.mkdir( path.join( project_dir, tplSrc, "home", module_name, "images" ) );
    util.mkdir( path.join( project_dir, tplSrc, "home", module_name, "images_" ) );
    util.mkdir( path.join( project_dir, tplSrc, "modules", module_name ) );

    util.log( "创建频道 " + module_name + " 完成！！" );

    !unNext && exports.cmd( project_name );
};