/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024  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 {userModel} = require('../../schemas/userSchema');
const statModel = require('../../schemas/statSchema');
const statSchema = require('../../schemas/statSchema');


module.exports = class tokebot{
    constructor(server, chatHandler){
        //Set parents
        this.server = server;
        this.chatHandler = chatHandler;

        //Set timeouts to null
        this.tokeTimer = null;
        this.cooldownTimer = null;

        //Set start times
        this.tokeTime = 60;
        this.cooldownTime = 120;

        //Create counter variable
        this.tokeCounter = 0;
        this.cooldownCounter = 0;

        //Create tokers list
        this.tokers = new Map();

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

    async refreshCommands(){
        //Pull Command Strings from DB
        this.tokeCommands = await tokeCommandModel.getCommandStrings();
    }

    tokeProcessor(commandObj){
        //Check for site-wide toke commands
        if(this.tokeCommands.indexOf(commandObj.argumentArray[0]) != -1){
            //Seems lame to set a bool in an if statement but this would've made a really ugly turinary
            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]) != -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]);

                //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]);
                //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, don't send as chat
            return false;
        }else{
            //No toke found, send it down the line, because shaming the user is funny
            return true;
        }
    }

    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 stat schema
            statSchema.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)
    }

    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;
        }
    }

}
