/**
 * Created by jimiz on 2017/4/16.
 */
var billsData = require('./bills');
var rationData = require('./ration');
var GLJData = require('./glj');
var ration_glj_data = require('../../ration_glj/facade/ration_glj_facade');
var ration_coe_data = require('../../ration_glj/facade/ration_coe_facade');
var ration_ass_data = require('../../ration_glj/facade/ration_ass_facade');
var quantity_detail_data = require('../../ration_glj/facade/quantity_detail_facade');
var fee_rate_data = require('../../fee_rates/facade/fee_rates_facade');
let projCounter = require('./proj_counter_model');
let projSetting = require('./proj_setting_model');
let volumePriceData = require('../../volume_price/models/volume_price_model');
var consts = require('./project_consts');
var projectConsts = consts.projectConst;
var async = require("async");

var moduleMap = {};

moduleMap[projectConsts.BILLS] = billsData;
moduleMap[projectConsts.RATION] = rationData;
//moduleMap[projectConsts.GLJ] = GLJData;
moduleMap[projectConsts.RATION_GLJ] = ration_glj_data;
moduleMap[projectConsts.RATION_COE] = ration_coe_data;
moduleMap[projectConsts.RATION_ASS] = ration_ass_data;
moduleMap[projectConsts.QUANTITY_DETAIL] = quantity_detail_data;
moduleMap[projCounter.collectionName] = projCounter;
moduleMap[projSetting.collectionName] = projSetting;
moduleMap[volumePriceData.collectionName] = volumePriceData;
moduleMap[projectConsts.FEERATE] = fee_rate_data;

var Project = function (){};

Project.prototype.datas = [];

Project.prototype.prepare = function(data, callback){
    var me = this;
    data.update_data.forEach(function(item){
        item.user_id = data.user_id;
        me.datas.push(item);
        /*
        to do
        moduleMap[item.moduleName].prepare(item.data, jobCallback);
        */
    });
};

Project.prototype.save = function(datas, callback){
    var job, savePoint;
    var functions = [];
    var item;
    var me = this;

    me.prepare(datas, function(job, savePoint){});

    function saveModule(data) {
        return function (cb) {
            if(data.moduleName!=undefined){
                moduleMap[data.moduleName].save(data.user_id, data.data, cb);
            }
           // console.log(moduleMap[data.moduleName])

        }
    }

    for (var i = 0; i < me.datas.length; i++){
        item = me.datas[i];
        functions.push(saveModule(item));
    }

    async.parallel(functions, function(err, results) {
        if (!err){
            callback(null, '', results)
        }
        else{
            callback(1, 'save project failed', null)
        }
    });
    me.datas = [];
};

Project.prototype.getData = function(projectID, callback){
    var functions = [];
    var itemName;

    for (itemName in moduleMap){
        functions.push((function(itemName){
            return function (cb) {
                moduleMap[itemName].getData(projectID, function(err, moduleName, data){
                    cb(err, {moduleName: moduleName, data: data})
                })
            }
        })(itemName))
    }

    async.parallel(functions, function(err, results) {
        if (!err){
            callback(null, '', results)
        }
        else{
            callback(1, 'load project failed', null)
        }
    });

};

module.exports = new Project();
