/* ----------------------------------------------------------------------------

 Question Server
 GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program. If not, see <https://www.gnu.org/licenses/>.

 ------------------------------------------------------------------------- */

import { Response } from 'express'

import logger from '../../../utils/logger'
import utils from '../../../utils/utils'
import { Request, SubmoduleData } from '../../../types/basicTypes'

interface Categories {
    [key: string]: {
        name: string
        color: string
    }
}

enum CardState {
    TODO = 'todo',
    INPROGRESS = 'inprogress',
    TESTING = 'testing',
    DONE = 'done',
    INPROD = 'inprod',
    NOTPOSSIBLE = 'notpossible',
}

interface Card {
    id: number
    name: string
    description: string
    category: string
    points: number
    state: CardState
    votes: number[]
}

type Columns = {
    [key in CardState]: {
        name: string
        clickable: boolean
    }
}

interface Groups {
    [key: string]: {
        name: string
        description: string
    }
}

interface Todos {
    categories: Categories
    cards: Card[]
    columns: Columns
    groups: Groups
}

const todosFile = 'data/todos.json'

function setup(data: SubmoduleData): void {
    const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data

    app.get('/voteTodo', (req: Request, res: Response) => {
        logger.LogReq(req)
        const userId = req.session.user.id
        const id: string = req.query.id
        const todos: Todos = utils.ReadJSON(todosFile)

        if (!id) {
            res.json({
                msg: 'id query undefined',
                result: 'not ok',
            })
        }

        const cardIndex = todos.cards.findIndex((currcard) => {
            return currcard.id === parseInt(id)
        })
        if (cardIndex === -1) {
            res.json({
                msg: 'card not found',
                result: 'not ok',
            })
            return
        }

        const ind = todos.cards[cardIndex].votes.indexOf(userId)
        if (ind === -1) {
            todos.cards[cardIndex].votes.push(userId)
        } else {
            todos.cards[cardIndex].votes.splice(ind, 1)
        }

        utils.WriteFile(JSON.stringify(todos, null, 2), todosFile)
        res.json({
            todos: todos,
            userId: userId,
            msg: 'updated',
            result: 'ok',
        })
    })

    app.get('/todos', (req: Request, res: Response) => {
        logger.LogReq(req)
        const userId = req.session.user.id
        const todos = utils.ReadJSON(todosFile)

        res.json({
            todos: todos,
            userId: userId,
            result: 'ok',
        })
    })
}

export default {
    setup: setup,
}
