/**
 * Created by Tony on 2017/8/3.
 */
let mongoose = require("mongoose");
let prj_prop_mdl = mongoose.model("project_property");
let std_prj_prop_mdl = mongoose.model("cfg_prj_property");


module.exports = {
    createPrjProperties: createPrjProperties,
    removePrjProperties : removePrjProperties,
    updateWhole: updateWhole,
    updateOneProperty: updateOneProperty,
    addOrChangeProperty: addOrChangeProperty,
    removeOneProperty: removeOneProperty,
    getPrjProperty: getPrjProperty,

    createStdPrjProperties: createStdPrjProperties,
    getStdPrjProperties: getStdPrjProperties,

    createPrjPropertiesWithCallBack: createPrjPropertiesWithCallBack,
    removePrjPropertiesWithCallBack: removePrjPropertiesWithCallBack,
    updateWholeWithCallBack: updateWholeWithCallBack,
    updateOnePropertyWithCallBack: updateOnePropertyWithCallBack,
    addOrChangePropertyWithCallBack: addOrChangePropertyWithCallBack,
    removeOnePropertyWithCallBack: removeOnePropertyWithCallBack
};

async function createStdPrjProperties(rationLibId, items) {
    let data = {};
    data.rationLibID = rationLibId;
    data.properties = items;
    let rst = await  std_prj_prop_mdl.create(data);
    return rst;
}

async function getStdPrjProperties(rationLibId) {
    let rst = await  std_prj_prop_mdl.findOne({"rationLibID": rationLibId});
    return rst;
}

async function getPrjProperty(prjId) {
    let rst = await  prj_prop_mdl.findOne({"projectID": prjId});
    return rst;
}
async function createPrjProperties(prjId, dftItems) {
    let data = {};
    data.projectID = prjId;
    data.properties = dftItems;
    let rst = await  prj_prop_mdl.create(data);
    return rst;
};

async function removePrjProperties(prjId) {
    let rst = await  prj_prop_mdl.remove({'projectID': prjId});
    return rst;
};

async function updateWhole(prjPropObj) {
    let rst = null;
    if (prjPropObj && prjPropObj.hasOwnProperty('projectID') && prjPropObj.hasOwnProperty('properties') && (prjPropObj.properties instanceof Array)) {
        rst = await  prj_prop_mdl.update({'projectID': prjPropObj.projectID}, prjPropObj);
    }
    return rst;
}

async function updateOneProperty(prjId, key, newValue) {
    let rst = await  prj_prop_mdl.update({"projectID": prjId, "properties.key": key}, {"$set": {"properties.$.value": newValue}});
    return rst;
}

async function addOrChangeProperty(prjId, newItem) {
    let rst = null;
    rst = await  prj_prop_mdl.findOne({"projectID": prjId, "properties.key": newItem.key});
    //console.log('find result: ' + rst);
    if (rst === null || rst.length === 0) {
        rst = await  prj_prop_mdl.update({"projectID": prjId}, {"$push": {"properties": newItem}});
        //console.log('push result: ' + rst);
    } else {
        rst = await  prj_prop_mdl.update({"projectID": prjId, "properties.key": newItem.key}, {"$set": {"properties.$.value": newItem.value}});
        //console.log('update result: ' + rst);
    }
    return rst;
};

async  function removeOneProperty(prjId, key) {
    let rst = prj_prop_mdl.update({"projectID": prjId}, {"$pull": {"properties": {"key": key}}});
    return rst;
}


function createPrjPropertiesWithCallBack(prjId, dftItems, cb) {
    let data = {};
    data.projectID = prjId;
    data.properties = dftItems;
    let saveObj = new prj_prop_mdl(data);
    saveObj.save(cb);
};

function removePrjPropertiesWithCallBack(prjId, cb) {
    prj_prop_mdl.remove({'projectID': prjId}, cb);
}

function updateWholeWithCallBack(prjPropObj, cb) {
    if (prjPropObj && prjPropObj.hasOwnProperty('projectID') && prjPropObj.hasOwnProperty('properties') && (prjPropObj.properties instanceof Array)) {
        prj_prop_mdl.update({'projectID': prjPropObj.projectID}, prjPropObj, cb);
    }
}

function updateOnePropertyWithCallBack(prjId, key, newValue, cb) {
    prj_prop_mdl.update({"projectID": prjId, "properties.key": key}, {"$set": {"properties.$.value": newValue}}, cb);
}

function addOrChangePropertyWithCallBack(prjId, newItem, cb) {
    prj_prop_mdl.findOne({"projectID": prjId, "properties.key": newItem.key}, function(err, result){
        if (err === null) {
            if (result === null) {
                console.log('no result');
                prj_prop_mdl.update({"projectID": prjId}, {"$push": {"properties": newItem}}, cb);
            } else {
                console.log('has result');
                prj_prop_mdl.update({"projectID": prjId, "properties.key": newItem.key}, {"$set": {"properties.$.value": newItem.value}}, cb);
            }
        }
    });
}

function removeOnePropertyWithCallBack(prjId, key, cb) {
    prj_prop_mdl.update({"projectID": prjId}, {"$pull": {"properties": {"key": key}}}, cb);
}
