const express = require('express');
const router = express.Router();
const alphaProcesser = require('../AlphaTab/ProcessAlphaTab.js');
const joiner = require("../Parser/joiner.js");
const ConvertToTex = require("../AlphaTab/ConvertToTex.js");
const fs = require('fs');
/**
* Route for getting the parsed object of given AlphaTex.
* WARNING: This is meant only for development purposes. It is not be enabled during production.
* @name visualize/get
* @function
* @inner
* @param {*} req
* @param {string} req.query.tex The AlphaTex to be processed
* @param {object} req.query.lyrics Relates Staff indexes to lyric strings
* @param {*} res
* @returns {ParserOutput|string} The parsed object of the AlphaTex. Otherwise, an error message if an error occured
* @async
*/
router.get('/', async(req, res) => {
let texInput = req.query.tex;
let lyrics = req.query.lyrics;
if (!texInput) {
texInput = req.body.tex;
}
if (!lyrics) {
lyrics = req.body.lyrics;
}
if (!texInput || !lyrics) {
res.status(400).send();
} else {
let parsedTex = alphaProcesser.process(texInput, lyrics);
res.status(200).json(parsedTex);
}
});
/**
* @typedef {VisualizeGetJoinInput[]} VisualizeGetJoinInputArray Collection of sheet music to be joined
*/
/**
* @typedef {object} VisualizeGetJoinInput
* @property {string} tex The AlphaTex of this input
* @property {object} lyrics Relates Staff indexes to lyric strings.
*/
/**
* @typedef {object} VisualizeGetJoinPackage
* @property {string} parsedTex The parsed joined AlphaTex
* @property {object} lyrics Relates Staff indexes to lyric strings.
*/
/**
* Route for joining two pieces of sheet music
* WARNING: This is meant only for development purposes. It is not be enabled during production.
* @name visualize/get/join
* @function
* @inner
* @param {*} req
* @param {VisualizeGetJoinInputArray} req.body.pDataArray
* @param {*} res
* @returns {VisualizeGetJoinPackage|string} The joined, parsed AlphaTex and lyrics. Otherwise, an error message if an error occured
* @async
*/
router.get('/join', async(req, res) => {
let pDataArray = req.query.pDataArray;
if (!pDataArray) {
pDataArray = req.body.pDataArray;
}
if (!pDataArray || !pDataArray.length || pDataArray.length == 0) {
res.status(400).send();
} else {
let joinedObj = {
parsedTex: alphaProcesser.process(pDataArray[0].tex, pDataArray[0].lyrics),
lyrics: pDataArray[0].lyrics
};
for (let i = 1; i < pDataArray.length; i++) {
let pData = pDataArray[i];
let lyrics = pData.lyrics;
let parsedTex = alphaProcesser.process(pData.tex, lyrics);
joiner.join(joinedObj.parsedTex, parsedTex, joinedObj.lyrics, lyrics);
}
let parsedTex = alphaProcesser.process(pDataArray[0].tex, pDataArray[0].lyrics);
let converter = new ConvertToTex();
fs.writeFile('test.txt', converter.visitAlphaTexStructure(parsedTex.alphaTexStructure, pDataArray[0].lyrics), function (err) {
if (err) throw err;
console.log('Saved!');
});
res.status(200).json(joinedObj);
}
});
module.exports = router;