/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2025 Rainbownapkin and the TTN Community

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.

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

//Local Imports
const tokeCommandModel = require('../../schemas/tokebot/tokeCommandSchema');
const tokeModel = require('../../schemas/tokebot/tokeSchema');
const {userModel} = require('../../schemas/user/userSchema');


/**
 * Class containing global server-side tokebot logic
 */
class tokebot{
    /**
     * Instantiates a tokebot object
     * @param {channelManager} server - Parent Server Object
     * @param {chatHandler} chatHandler - Parent Chat Handler Object
     */
    constructor(server, chatHandler){
        /**
         * Parent Server Object
         */
        this.server = server;

        /**
         * Parent Chat Handler
         */
        this.chatHandler = chatHandler;

        /**
         * Toke Timer
         */
        this.tokeTimer = null;

        /**
         * Cooldown Timer
         */
        this.cooldownTimer = null;

        /**
         * Toke time
         */
        this.tokeTime = 60;

        /**
         * Cooldown Time
         */
        this.cooldownTime = 120;

        /**
         * Toke Counter
         */
        this.tokeCounter = 0;
        
        /**
         * Cooldown Counter
         */
        this.cooldownCounter = 0;

        /**
         * List of current tokers
         */
        this.tokers = new Map();

        //Load in toke commands from the DB
        this.refreshCommands();
    }

    /**
     * Reloads toke commands from DB into RAM-based toke command store
     */
    async refreshCommands(){
        //Pull Command Strings from DB
        this.tokeCommands = await tokeCommandModel.getCommandStrings();
    }

    /**
     * Processes toke commands from Command Pre-Processor
     * @param {Object} commandObj - Object representing a single given command/chat request, passed down from the Command Pre-Processor
     * @returns {Boolean} True if the toke is an invalid toke command (tells Command Pre-Processor to send command as chat)
     */
    tokeProcessor(commandObj){
        //Check for site-wide toke commands
        if(this.tokeCommands.indexOf(commandObj.argumentArray[0].toLowerCase()) != -1){
            //Seems lame to set a bool in an if statement but this would've made a really ugly turinary
            var foundToke = true;
        }else if(commandObj.argumentArray[0].toLowerCase() == 'r'){
            //Find the users active channel
            const activeChan = this.server.activeChannels.get(commandObj.socket.chan);
            
            //Combile site-wide and channel tokes into one list
            const tokeList = this.tokeCommands.concat(activeChan.tokeCommands);

            //Pick a random number between 0 and one less than the number of tokes
            const foundIndex = Math.round(Math.random() * (tokeList.length - 1));

            //Set override command argument 0 w/ the found toke
            commandObj.argumentArray[0] = tokeList[foundIndex];

            //throw toke flag
            var foundToke = true;
        }else{
            //Find the users active channel
            const activeChan = this.server.activeChannels.get(commandObj.socket.chan);

            //Check if they're using a channel-only toke
            //This should be safe to do without a null check but someone prove me wrong lmao
            var foundToke = (activeChan.tokeCommands.indexOf(commandObj.argumentArray[0].toLowerCase()) != -1);
        }


        //If we found a toke
        if(foundToke){
            //If there is no active toke or cooldown (new toke)
            if(this.tokeTimer == null && this.cooldownTimer == null){
                //Call-out toke start
                this.chatHandler.relayTokeCallout(`A group toke has been started by ${commandObj.socket.user.user} from #${commandObj.socket.chan}! We'll be taking a toke in 60 seconds - join in by posting !${commandObj.argumentArray[0]}`);
                //Set a full minute on our toke timer
                this.tokeCounter = this.tokeTime;

                //Add the toking user to the tokers map
                this.tokers.set(commandObj.socket.user.user, commandObj.argumentArray[0].toLowerCase());

                //kick-off the count-down
                this.tokeTimer = setTimeout(this.countdown.bind(this), 1000)
            //If the tokeTimer is popping but the cooldownTimer has fucked off (a toke is in progress)
            }else if(this.cooldownTimer == null){
                //look for user in tokers map
                const foundToker = this.tokers.get(commandObj.socket.user.user);

                //if the user has not yet joined the toke
                if(foundToker == null){
                    //Call-out toke join
                    this.chatHandler.relayTokeCallout(`${commandObj.socket.user.user} has joined the toke from #${commandObj.socket.chan}! Post !${commandObj.argumentArray[0]} to take part!`);

                    //Add the toking user to the tokers map
                    this.tokers.set(commandObj.socket.user.user, commandObj.argumentArray[0].toLowerCase());
                //If the user is already in the toke
                }else{
                    //Tell them to fuck off
                    this.chatHandler.relayTokeWhisper(commandObj.socket, "You're already taking part in this toke!");
                }

            //Otherwise (there isn't a toke timer, but there is a cooldown timer. AKA: we're in cooldown)
            }else{
                //if the cooldownTimer exists (we're cooling down the toke)
                this.chatHandler.relayTokeWhisper(commandObj.socket, `Please wait ${this.cooldownCounter} seconds before starting a new group toke.`);
            }

            //Toke command found, and there isn't any extra text, don't send as chat (re-create fore.st tokebot behaviour)
            return (commandObj.command != `!${commandObj.argumentArray[0]}` && commandObj.command != '!r');
        }else{
            //No toke found, send it down the line, because shaming the user is funny
            return true;
        }
    }

    /**
     * Called each second during the toke. Handles decrementing the timer variable, and countdown end logic.
     */
    countdown(){
        //If we're in the last three seconds
        if(this.tokeCounter <= 3 && this.tokeCounter > 0){
            //Callout the last three seconds
            this.chatHandler.relayTokeCallout(`${this.tokeCounter}...`);
        //if the toke is over
        }else if(this.tokeCounter < 0){
            //if we had multiple tokers
            if(this.tokers.size > 1){
                //call out the toke
                this.chatHandler.relayTokeCallout(`Take a toke ${Array.from(this.tokers.keys()).join(', ')}! ${this.tokers.size} tokers!`);
            //if we only had one toker
            }else{
                //call out the solo toke
                this.chatHandler.relayTokeCallout(`Take a toke ${Array.from(this.tokers.keys())[0]}.`);
            }

            //Asynchronously tattoo the toke into the users documents within the database so that tokebot doesn't have to wait or worry about DB transactions
            userModel.tattooToke(this.tokers);
            //Do the same for the global toke statistics collection
            tokeModel.tattooToke(this.tokers);

            //Set the toke cooldown
            this.cooldownCounter  = this.cooldownTime;
            this.cooldownTimer = setTimeout(this.cooldown.bind(this), 1000);

            //Empty out the tokers array
            this.tokers = new Map;

            //Null out our timer
            this.tokeTimer = null;

            //return the function before it can continue
            return;
        }

        //Decrement toke time
        this.tokeCounter--;
        //try again in another second
        this.tokeTimer = setTimeout(this.countdown.bind(this), 1000)
    }

    /**
     * This method seems to be a vestage from a bygone era. We should remove it after documenting shit.
     * I would now, but I don't want to break shit in a comment-only commit.
     */
    async asyncFinisher(){
        //Grab a copy of the tokers map before it gets cleared out
        const tokers = this.tokers;

        //we need to wait for this so we don't send used tokes pre-maturely
        await userModel.tattooToke(tokers);
    }

    /**
     * Runs every second for 60 seconds after a toke
     */
    cooldown(){
        //If the cooldown timer isn't over
        if(this.cooldownCounter > 0){
            //Decrement toke time
            this.cooldownCounter--;
            //try again in another second
            this.cooldownTimer = setTimeout(this.cooldown.bind(this), 1000);
        //If the cooldown is over
        }else{
            //Null out the cooldown timer
            this.cooldownTimer = null;
        }
    }

    /**
     * Resets toke cooldowns early upon authorized request
     */
    resetToke(){
        //Set cooldown to 0
        this.cooldownCounter = 0;

        //Null out the timer
        this.cooldownTimer = null;
    }

}

module.exports = tokebot;