const fs = require('fs');
const path = require('path');

const notesDir = path.join(__dirname, '../assets/note');

if (!fs.existsSync(notesDir)) {
    fs.mkdirSync(notesDir, { recursive: true });
    }

    module.exports = {
        info: {
                path: '/note/:UUID',
                        title: 'Note API',
                                desc: 'API for creating and retrieving notes',
                                        example_url: [
                                                    { method: 'GET', query: '/note/:UUID', desc: 'Retrieve a note' },
                                                                { method: 'PUT', query: '/note/:UUID', desc: 'Create or update a note' }
                                                                        ]
                                                                            },
                                                                                methods: {
                                                                                        get: (req, res) => {
                                                                                                    const uuid = req.params.UUID;

                                                                                                                if (!uuid || uuid === ':UUID' || uuid.length > 36) {
                                                                                                                                res.redirect(`./${require('uuid').v4()}`);
                                                                                                                                                return;
                                                                                                                                                            }

                                                                                                                                                                        const filePath = path.join(notesDir, `${uuid}.txt`);
                                                                                                                                                                                    const text = fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf8') : '';

                                                                                                                                                                                                if (fs.existsSync(filePath + '.raw')) {
                                                                                                                                                                                                                const rawFilePath = fs.readFileSync(filePath + '.raw', 'utf8');

                                                                                                                                                                                                                                if (fs.existsSync(rawFilePath)) {
                                                                                                                                                                                                                                                    res.set('content-type', 'text/plain');
                                                                                                                                                                                                                                                                        res.end(fs.readFileSync(rawFilePath, 'utf8'));
                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                            } else {
                                                                                                                                                                                                                                                                                                                                res.status(404).end();
                                                                                                                                                                                                                                                                                                                                                    return;
                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                            if (req.query.raw == 'true' || !/^Mozilla/.test(req.headers['user-agent'])) {
                                                                                                                                                                                                                                                                                                                                                                                                            res.set('content-type', 'text/plain');
                                                                                                                                                                                                                                                                                                                                                                                                                            res.end(text);
                                                                                                                                                                                                                                                                                                                                                                                                                                            return;
                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                    res.set('content-type', 'text/html');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                res.end(`<!--
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                @Author: DC-Nam
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                -->
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.32.1/min/vs/editor/editor.main.min.css">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.32.1/min/vs/loader.min.js"></script>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <style>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    body {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            margin: 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    padding: 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            font-family: Arial, sans-serif;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    #content {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            width: 100%;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    height: 100vh;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            overflow: hidden;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    border-top: 1px solid #d4a373;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            #editor {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    width: 100%;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            height: 100%;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    border: none;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            h3 {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    color: #3e2723;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            font-weight: bold;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    text-align: center;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            h6 {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    color: #6d4c41;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            font-weight: bold;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    text-align: center;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        </style>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <h3>Note</h3>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <h6>Sau khi chỉnh sửa thay đổi hãy đợi 1s để upload data</h6>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <div id="content">
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <div id="editor"></div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </div>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <script>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.32.1/min/vs' }});
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    require(['vs/editor/editor.main'], function () {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            const editor = monaco.editor.create(document.getElementById('editor'), {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        value: \`${text}\`,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    language: 'javascript',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                theme: 'vs-light',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            automaticLayout: true,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        scrollBeyondLastLine: false,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    wordWrap: 'on'
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            });

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    const put = () => fetch(location.href, {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                method: 'PUT',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            headers: {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            'content-type': 'text/plain; charset=utf-8',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        },
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    body: editor.getValue(),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            });

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    let putt;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            editor.onDidChangeModelContent(() => {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if (putt) clearTimeout(putt);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    putt = setTimeout(put, 1000);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            });

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    const u = new URL(location.href);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            u.searchParams.append('raw', 'true');

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    fetch(u.href, {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                method: 'GET',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            headers: { 'user-agent': 'fetch' }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }).then(r => r.text()).then(t => {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                editor.setValue(t);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        });
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            });
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </script>`);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    },
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            put: async (req, res) => {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        const chunks = [];

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    req.on('data', chunk => chunks.push(chunk));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                await new Promise(resolve => req.on('end', resolve));

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            const uuid = req.params.UUID;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        const filePath = path.join(notesDir, `${uuid}.txt`);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if (req.query.raw) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if (!fs.existsSync(filePath + '.raw')) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        fs.writeFileSync(filePath + '.raw', path.join(notesDir, `${req.query.raw}.txt`));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    } else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    fs.writeFileSync(filePath, Buffer.concat(chunks));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            res.end();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    },
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        },
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        };
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        