/**
 * Created by Tony on 2017/8/4.
 */
let test = require('tape');
let mongoose = require("mongoose");
let dbm = require("../../../config/db/db_manager");
require('../../../modules/main/models/prj_properties');
let ppFacade = require('../../../modules/main/facade/prj_properties_facade');

let rationLibId = 3;
let randomPrjId = Math.round(Math.random() * 100);
//let randomPrjId = 35;
dbm.connect();

/*
test('测试 - 创建dummy项目属性: ', function (t) {
    let items = [];
    items.push({key: "contractNum", dispName: "合同号", value: "天字第一号"});
    items.push({key: "appType", dispName: "工程专业", value: "建筑工程"});
    items.push({key: "constructType", dispName: "工程类别", value: "住宅"});
    items.push({key: "startDate", dispName: "开工日期", value: "2017-07-31"});
    items.push({key: "completeDate", dispName: "竣工日期", value: "2018-07-31"});
    items.push({key: "location", dispName: "工程地点", value: "珠海新香洲人民西路666号"});
    items.push({key: "owner", dispName: "建设单位", value: "珠海纵横创新"});
    items.push({key: "designCompany", dispName: "设计单位", value: "珠海设计局"});
    items.push({key: "buildingCompany", dispName: "施工单位", value: "中建十局"});
    items.push({key: "superviseCompany", dispName: "监理单位", value: "珠海监理"});
    items.push({key: "auditCompany", dispName: "审核单位", value: "创新审核"});
    items.push({key: "author", dispName: "编制人", value: "张三"});
    items.push({key: "auditor", dispName: "审核人", value: "李四"});

    let results = ppFacade.createPrjProperties(randomPrjId, items);
    results.then(function(rst) {
        console.log('rst.projectID: ' + rst.projectID);
        for (let prop of rst.properties) {
            console.log('property ' + prop.dispName + ': ' + prop.value);
        }
        t.pass('just pass!');
        t.end();
    });
});
//*/

/*
test('测试 - 创建dummy标准项目属性: ', function (t) {
    let items = [];
    items.push({key: "contractNum", dispName: "合同号", value: "天字第一号"});
    items.push({key: "appType", dispName: "工程专业", value: "建筑工程"});
    items.push({key: "constructType", dispName: "工程类别", value: "住宅"});
    items.push({key: "startDate", dispName: "开工日期", value: "2017-07-31"});
    items.push({key: "completeDate", dispName: "竣工日期", value: "2018-07-31"});
    items.push({key: "location", dispName: "工程地点", value: "珠海新香洲人民西路666号"});
    items.push({key: "owner", dispName: "建设单位", value: "珠海纵横创新"});
    items.push({key: "designCompany", dispName: "设计单位", value: "珠海设计局"});
    items.push({key: "buildingCompany", dispName: "施工单位", value: "中建十局"});
    items.push({key: "superviseCompany", dispName: "监理单位", value: "珠海监理"});
    items.push({key: "auditCompany", dispName: "审核单位", value: "创新审核"});
    items.push({key: "author", dispName: "编制人", value: "张三"});
    items.push({key: "auditor", dispName: "审核人", value: "李四"});

    let results = ppFacade.createStdPrjProperties(rationLibId, items);
    results.then(function(rst) {
        console.log('rst.projectID: ' + rst.projectID);
        for (let prop of rst.properties) {
            console.log('property ' + prop.dispName + ': ' + prop.value);
        }
        t.pass('just pass!');
        t.end();
    });
});
//*/

/*
test('测试 - 整个更新项目属性: ', function (t) {
    let items = [];
    items.push({key: "contractNum", dispName: "合同号", value: "人字第三号"});
    items.push({key: "appType", dispName: "工程专业", value: "建筑工程"});
    items.push({key: "constructType", dispName: "工程类别", value: "住宅"});
    items.push({key: "startDate", dispName: "建设日期", value: "2017-08-01"});
    items.push({key: "owner", dispName: "建设单位", value: "珠海纵横创新"});
    items.push({key: "designCompany", dispName: "设计单位", value: "珠海设计局"});
    items.push({key: "buildingCompany", dispName: "施工单位", value: "中建十局"});

    let udtObj = {"projectID": 35, "properties": items};

    let rst = ppFacade.updateWhole(udtObj);
    if (rst) {
        rst.then(function(results){
            for (let prop in results) {
                console.log(prop + ': ' + results[prop]);
            }
            t.pass('just pass!');
            t.end();
        });
    } else {
        console.log('update failed!');
        t.pass('just pass!');
        t.end();
    }
});
//*/

/*
test('测试 - 新增项目属性: ', function (t) {
    let newItem = {key: "superviseCompany", dispName: "监理单位", value: "珠海德信"};
    let results = ppFacade.addOrChangeProperty(35, newItem);
    if (results instanceof Promise) {
        results.then(function (rst) {
            for (let prop in rst) {
                console.log("property " + prop + ' : ' + rst[prop]);
            }
        })
    } else {
        for (let prop in results) {
            console.log("property " + prop + ' : ' + results[prop]);
        }
    }

    t.pass('just pass!');
    t.end();

});
//*/

/*
test('测试 - 删除全体项目属性: ', function (t) {
    let rst = ppFacade.removePrjProperties(35);
    if (rst) {
        if (rst instanceof Promise) {
            rst.then(function(results){
                for (let prop in results) {
                    console.log(prop + ': ' + results[prop]);
                }
                t.pass('just pass with remove1!');
                t.end();
            });
        } else {
            for (let prop in results) {
                console.log(prop + ': ' + results[prop]);
            }
            t.pass('just pass with remove2!');
            t.end();
        }
    } else {
        t.pass('just pass but not remove!');
        t.end();
    }
});
//*/

/*
test('测试 - 更新项目属性: ', function (t) {
    let rst = ppFacade.updateOneProperty(35, "buildingCompany", "中建十一局");
    if (rst) {
        rst.then(function(results){
            for (let prop in results) {
                console.log(prop + ': ' + results[prop]);
            }
            t.pass('just pass with update!');
            t.end();
        });
    } else {
        t.pass('just pass but not update!');
        t.end();
    }
});
//*/

/*
test('测试 - 获取项目属性: ', function (t) {
    let rst = ppFacade.getPrjProperty(35);
    if (rst) {
        rst.then(function(results){
            console.log(results.projectID);
            console.log(results.properties);
            t.pass('just pass with update!');
            t.end();
        });
    } else {
        t.pass('just pass but not update!');
        t.end();
    }
});
//*/

/*
test('测试 - 获取标准项目属性: ', function (t) {
    let rst = ppFacade.getStdPrjProperties(rationLibId);
    if (rst) {
        rst.then(function(results){
            // console.log(results.rationLibID);
            // console.log(results.properties);
            let rt = ppFacade.createPrjProperties(randomPrjId, results.properties);
            rt.then(function (rt1) {
                console.log('创建后ID: '+ rt1.projectID);
            })
            t.pass('just pass with update!');
            t.end();
        });
    } else {
        t.pass('just pass but not update!');
        t.end();
    }
});
//*/

/*
test('测试 - 移除一项属性: ', function (t) {
    let rst = ppFacade.removeOneProperty(35, "superviseCompany");
    if (rst) {
        rst.then(function(results){
            for (let prop in results) {
                console.log(prop + ': ' + results[prop]);
            }
            t.pass('just pass with remove!');
            t.end();
        });
    } else {
        t.pass('just pass but not remove!');
        t.end();
    }
});
//*/

test('close the connection', function (t) {
    // setTimeout(function () {
    //     mongoose.disconnect();
    //     t.pass('closing db connection');
    //     t.end();
    // }, 100);
    mongoose.disconnect();
    t.pass('closing db connection');
    t.end();
})