/**
 * @file Apeman app configuration file.
 * @namespace apemanfile
 * @param {object} apeman - Apeman module.
 * @returns {object} - An apeman file object.
 * @see {@link http://www.npmjs.org/package/apeman | apeman}
 * @author {{whoami}}
 *
 */
var path = require('path');
module.exports = (function (apeman) {
    var cwd = process.cwd();
    process.chdir(__dirname);

    var Apemanfile = apeman.ApemanfileObject,
        prototypePath = apeman.findupApp('{{prototype}}'),
        prototype = require(prototypePath)(apeman),
        basedir = __dirname;



    // Helper modules
    var prototypeHelper = prototype.helper();
    var h = prototypeHelper.fallbackCopy(prototypeHelper, {
        lookupPaths: [basedir].concat(prototypeHelper.lookupPaths),
    });


    // Add helper functions if _apemanfile_helper.js found.
    h.loadLocalHelperFile();

    var meta = {
        name: '{{appName}}',
        version: h.readLocalVERSION() || '0.0.0',
        description: '',
        repository:'',
        author:{
            name:'{{whoami}}',
            email: ''
        }
    };

    var baseConstants = {

    },
    productionConstants = h.fallbackCopy(baseConstants, {

    }),
    devConstants = h.fallbackCopy(baseConstants, {

    }),
    scenarioTestConstants = h.fallbackCopy(baseConstants, {

    }),
    browserScenarioTestConstants = h.fallbackCopy(baseConstants, {

    }),
    docConstants = h.fallbackCopy(baseConstants, {

    }),
    ciConstants = h.fallbackCopy(baseConstants, {

    }),
    operationConstants = h.fallbackCopy(baseConstants, {

    }),
    operationDevConstants = h.fallbackCopy(operationConstants, {

    });

    /**
     * File system structure data.
     * An empty object represent a directory.
     * @type object
     * @private
     */
    var structureData = {
        '_apemanfile_helper.js': h.newApemanfileHelperJsFile(h.apemanHelperData)
    };

    /**
     * Packages installed by npm.
     * @type object
     * @private
     */
    var nodePackages = {

    };

    /**
     * Packages installed by npm for development user.
     * @type {object}
     * @private
     */
    var nodePackages$dev = {

    };

    var prototypeTasks = prototype.tasks();

    var tasks = {
        structure: {
            config: {
                data: structureData
            }
        },
        build: prototypeTasks.build.concat([

        ]),
        index: prototypeTasks.index.concat([

        ]),
        installNodePackages: {
            config: {
                packages: nodePackages
            }
        },
        installNodeDevPackages: {
            config: {
                packages: nodePackages$dev
            }
        },
        install: prototypeTasks.install.concat([

        ]),
        test: prototypeTasks.test.concat([

        ]),
        generateApiguide: {
            config: {
                src: prototypeTasks.generateApiguide.config.src
                .map(function (filename) {
                    return path.resolve(path.dirname(prototypePath), filename)
                })
                .concat([
                    path.resolve('lib/**/*.js'),
                    path.resolve('task/**/*.js'),
                    path.resolve('README.md'),
                    path.resolve('Apemanfile.js'),
                    path.resolve('_apemanfile_helper.js')
                ]),
                // Available themes:
                // [ "amelia","cerulean","cosmo","cyborg","flatly","journal","readable",
                //   "simplex","slate","spacelab","spruce","superhero","united"]
                theme: 'united'
            }
        },
        generateCIConstantsJson: {
            config: {
                data: ciConstants
            }
        },
        generateDevConstantsJson: {
            config: {
                data: devConstants
            }
        },
        generateDocConstantsJson: {
            config: {
                data: docConstants
            }
        },
        generateScenarioTestConstantsJson: {
            config: {
                data: scenarioTestConstants
            }
        },
        generateBrowserScenarioTestConstantsJson: {
            config: {
                data: browserScenarioTestConstants
            }
        },
        generateProductionConstantsJson: {
            config: {
                data: productionConstants
            }
        },
        generateOperationConstantsJson: {
            config: {
                data: operationConstants
            }
        },
        generateOperationDevConstantsJson: {
            config: {
                data: operationDevConstants
            }
        },
        doc: prototypeTasks.doc.concat([

        ])
    };

    //Create a new apeman file object.
    var apemanfile = new Apemanfile(
        prototype
    ).set({
        basedir: basedir,
        prototypePath: prototypePath,
        meta: meta,
        tasks: tasks,
        helper: h
    });
    process.chdir(cwd);
    return  apemanfile;
});