/**
 * Created by Tony on 2017/8/9.
 */

// let consts = require('../../../modules/main/models/project_consts');
// let projectConst = consts.projectConst;
let prjMdl = require('../../../modules/pm/models/project_model');
let projectDataMdl = require('../../../modules/main/models/project');

module.exports = {
    prepareProjectData: prepareProjectData,
    prepareMultiProjectData: prepareMultiProjectData
};

function prepareProjectData(userId, prjId, filter, callback) {
    let rawDataObj = null;
    let basicInformation = prjMdl.project.getBasicInfo(prjId);
    prjMdl.project.getUserProject(userId, prjId, function(err, msg, rst){
        if (!err) {
            rawDataObj = {};
            rawDataObj.prj = rst;
            projectDataMdl.getFilterData(prjId, filter, function (results) {
                if (results) {
                    rawDataObj.prjData = results;
                    basicInformation.then(function(basicRst) {
                        if (basicRst !== null && basicRst !== undefined) {
                            let doc = (rawDataObj.prj._doc)?rawDataObj.prj._doc:rawDataObj.prj;
                            doc.property.basicInformation = basicRst;
                        }
                        callback(false, 'Succeeded!', rawDataObj);
                    });
                    // callback(false, 'Succeeded!', rawDataObj);
                } else {
                    callback(true, 'No data were found!', null);
                }
            });
        } else {
            callback(err, msg, null);
        }
    });
}

async function prepareMultiProjectData(userId, prjIds, filter) {
    function getProjectPageData(prjId) {
        return new Promise(function(resolve, reject) {
            prjMdl.project.getUserProject(userId, prjId, function(err, msg, rst){
                if (!err) {
                    projectDataMdl.getFilterData(prjId, filter, function (results) {
                        if (results) {
                            rawDataObj = {};
                            rawDataObj.prj = rst;
                            rawDataObj.prjData = results;
                            /*
                            prjMdl.project.getBasicInfo(prjId).then(function(basicRst) {
                                if (basicRst !== null && basicRst !== undefined) {
                                    let doc = (rawDataObj.prj._doc)?rawDataObj.prj._doc:rawDataObj.prj;
                                    doc.property.basicInformation = basicRst;
                                }
                                resolve(rawDataObj);
                            });
                            /*/
                            resolve(rawDataObj);
                            //*/
                        } else {
                            return reject('No data were found!');
                        }
                    });
                } else {
                    return reject(err);
                }
            });
            // let prjRst = prjMdl.project.getUserProjectAsync(userId, prjId);
            // rawDataObj = {};
            // rawDataObj.prj = prjRst;
            // projectDataMdl.getFilterData(prjId, filter, function (results) {
            //     if (results) {
            //         rawDataObj.prjData = results;
            //         basicInformation.then(function(basicRst) {
            //             if (basicRst !== null && basicRst !== undefined) {
            //                 let doc = (rawDataObj.prj._doc)?rawDataObj.prj._doc:rawDataObj.prj;
            //                 doc.property.basicInformation = basicRst;
            //             }
            //             resolve(rawDataObj);
            //         });
            //     } else {
            //         return reject(rawDataObj);
            //     }
            // });
        });
    }
    let parallelFunctions = [];
    for (let prjId of prjIds) {
        parallelFunctions.push(getProjectPageData(prjId));
    }
    let rst = await Promise.all(parallelFunctions);
    return rst;
}