
import {
    Router
} from "express";

import {
    Template as TemplateTable,
    Element as ElementTable,
    Variable as VariableTable
} from "../sequelize/template.model.mjs";


export var Routes = Router();


Routes.get('/templates', async function (Request, Response)
{
    let TemplateList = await TemplateTable.findAll({
        include: [
            ElementTable,
            VariableTable
        ]
    });

    // convert to raw object
    TemplateList = JSON.parse(JSON.stringify(TemplateList));
    
    // sort templates by name
    TemplateList.sort(function (a, b) {
        if (a.name < b.name) return -1;
        if (a.name > b.name) return 1;
        return 0;
    });

    Response.send(TemplateList);
})


Routes.get('/template/:templateID', async function (Request, Response)
{
    let template = await TemplateTable.findByPk(Request.params.templateID, {
        include: [
            ElementTable,
            VariableTable
        ]
    });

    // parse JOSN configs from elements
    template.elements = template.elements.map((element) => {
        element.config = JSON.parse(element.config);
        return element;
    });

    Response.send(template);
})


Routes.post('/template', async function (Request, Response)
{
    let Template = await TemplateTable.create({
        name: Request.body.name,
        width: Request.body.width,
        height: Request.body.height
    });
    
    Response.status(200);
    Response.send(Template);
})

Routes.put('/template', async function (Request, Response)
{
    let Template = await TemplateTable.findByPk(Request.body.templateId);

    await Template.update({
        name: Request.body.name,
        width: Request.body.width,
        height: Request.body.height
    });

    Response.status(200);
    Response.send(Template);
})


Routes.delete('/template', async function (Request, Response)
{
    let Template = await TemplateTable.findByPk(Request.body.templateId);
    await Template.destroy();

    Response.status(200);
    Response.end();
})


Routes.post('/variable', async function (Request, Response)
{
    let Template = await TemplateTable.findByPk(Request.body.templateId)

    let Variable = await Template.createVariable({
        name: Request.body.name,
        label: Request.body.label,
        regex: Request.body.regex,
        example: Request.body.example,
        default: Request.body.default
    });
    
    Response.status(200);
    Response.send(Variable);
})


Routes.put('/variable', async function (Request, Response)
{
    let Variable = await VariableTable.findByPk(Request.body.variableId)

    Variable.update({
        name: Request.body.name,
        label: Request.body.label,
        regex: Request.body.regex,
        example: Request.body.example,
        default: Request.body.default
    });
    
    Response.status(200);
    Response.send(Variable);
})


Routes.delete('/variable', async function (Request, Response)
{
    let Variable = await VariableTable.findByPk(Request.body.variableId)
    await Variable.destroy();

    Response.status(200);
    Response.send({});
})


Routes.post('/element', async function (Request, Response)
{
    let template = await TemplateTable.findByPk(Request.body.templateId)

    let element = await template.createElement({
        name: Request.body.name,
        type: Request.body.type,
        config: Request.body.config,
        comment: ""
    });
    
    Response.status(200);
    Response.send(element);
})


Routes.put('/element', async function (Request, Response) {
    let element = await ElementTable.findByPk(Request.body.elementId)

    element.update({
        name: Request.body.name,
        type: Request.body.type,
        config: Request.body.config,
        comment: Request.body.comment
    });

    Response.status(200);
    Response.send(element);
})


Routes.delete('/element', async function (Request, Response)
{
    let element = await ElementTable.findByPk(Request.body.elementId)
    await element.destroy();

    Response.status(200);
    Response.send({});
})
