/**
 * Created by CSL on 2017-10-25.
 */

let mongoose = require('mongoose');
let calcProgramFacade = require('../facade/calc_program_facade');

module.exports = {
    getProjectCalcProgram: getProjectCalcProgram,
    getStdCalcProgram: getStdCalcProgram,
    saveCalcItem: saveCalcItem,
    saveCalcItems: saveCalcItems,
    updateTemplate: updateTemplate,
    updateTemplateFile: updateTemplateFile,
    addTemplate: addTemplate,
    deleteTemplate: deleteTemplate
};

async function getProjectCalcProgram(req, res) {
    let result = {error: 0, message: '', data: null};

    try {
        let projCP = await calcProgramFacade.getProjectCalcProgramFile(req.body.data.calcProgramID);
        result.data= projCP;
    }catch (err){
        result.error = 1;
        result.message = err.message;
    }

    res.json(result);
};

async function getStdCalcProgram(req, res) {
    let result = {error: 0, message: '', data: null};

    let data = JSON.parse(req.body.data);
    try {
        let stdCP = await calcProgramFacade.getStdCalcProgramFile(data.ID);
        result.data= stdCP;
    }catch (err){
        result.error = 1;
        result.message = err.message;
    }

    res.json(result);
};

async function saveCalcItem(req, res) {
    let result = {error: -1, data: -1, message: ''};

    calcProgramFacade.saveCalcItem(req.body.data, function (err, msg) {
        if (err) {
            result.error = 1;
            result.data = 0;
        }
        else {
            result.error = 0;
            result.data = 1;
        }
        result.message = msg;
        res.json(result);
    });

};

async function saveCalcItems(req, res) {
    let result = {};

    calcProgramFacade.saveCalcItems(req.body.data, function (err, msg) {
        if (err) {
            result.error = 1;
            result.data = 0;
        }
        else{
            result.error = 0;
            result.data = 1;
        }
        result.message = msg;
        res.json(result);
    });
};

async function updateTemplate(req, res) {
    let result = {};
    calcProgramFacade.updateTemplate(req.body.data, function (err, msg) {
        if (err) {
            result.error = 1;
            result.data = 0;
        }
        else{
            result.error = 0;
            result.data = 1;
        }
        result.message = msg;
        res.json(result);
    });

};

async function updateTemplateFile(req, res) {
    let result = {};
    await calcProgramFacade.updateTemplateFile(req.body.data, function (err, msg, data) {
        if (err) {
            result.error = 1;
            result.message = msg;
            result.data = null;
        }
        else{
            result.error = 0;
            result.message = msg;
            result.data = data;
        }
        res.json(result);
    });

};

async function addTemplate(req, res) {
    let result = {};
    calcProgramFacade.addTemplate(req.body.data, function (err, msg) {
        if (err) {
            result.error = 1;
            result.data = 0;
        }
        else{
            result.error = 0;
            result.data = 1;
        }
        result.message = msg;
        res.json(result);
    });

};

async function deleteTemplate(req, res) {
    let result = {};
    calcProgramFacade.deleteTemplate(req.body.data, function (err, msg) {
        if (err) {
            result.error = 1;
            result.data = 0;
        }
        else{
            result.error = 0;
            result.data = 1;
        }
        result.message = msg;
        res.json(result);
    });

};