import random

from funcs import logThis
from .loveletterDraw import DrawLove

class LoveLetter():
    def __init__(self,bot):
        self.bot = bot
        self.draw = DrawLove(bot)

    # Parses command
    def parseLove(self, command, channel, user, userchannel):
        commands = command.lower().split()
        game = self.bot.database["loveletter games"].find_one({"_id":channel})

        if command == "" or command == " ":
            logThis(str(user)+" created a Love Letter game with loveStart(). ")
            return self.loveStart(channel)

        # If using a command with no game, return error
        elif game == None:
            return "There's no game in this channel", False, False

        # Joining the game
        elif commands[0] == "join":
            if game["round"] == 0:
                if user not in game["player hands"]:
                    # Deal cards
                    card = game["deck"].pop()
                    username = self.bot.funcs.getName(user)
                    game["player hands"][username] = card
                    game["discard piles"][username] = []
                    game["user channel"][username] = userchannel # Used for direct (private) messages to the users
                    self.bot.database["loveletter games"].replace_one({"_id":channel},game)
                    return username+" joined the game! Type \"!loveletter begin\" once all players have joined.", False, False
                else:
                    return "You're already playing!", False, False
            else:
                return "It's too late to join", False, False

        # Beginning the game
        elif commands[0] == "begin":
            if user in game["player hands"]:
                if len(game["player hands"]) > 1:
                    pass
                else:
                    return "AI functionality hasn't been implemented yet. Get another player to join!", False, False
            else:
                return "You can't begin a game, when you're not a player!", False, False

        # Stopping the game
        elif commands[0] == "stop":
            if user in game["player hands"]:
                return "Ending game.", False, False
            else:
                return "You can't end a game where you're not a player.", False, False

        else:
            return "I didn't get that. Use \"!loveletter\" to start a game or \"!loveletter join\" to join a game. ", False, False

    # Starts the game
    def loveStart(self, channel):
        game = self.bot.database["loveletter games"].find_one({"_id":channel})

        if game == None:
            
            
            deck = [1,1,1,1,1, 2,2, 3,3, 4,4, 5,5, 6, 7, 8]
            random.shuffle(deck)
            cardAside = deck[0]
            del deck[0] # The card that is set aside

            newGame = {"_id":channel,"player hands": {},"discard piles":{},"deck":deck,"round":0,"cardAside":cardAside}
            
            self.bot.database["loveletter games"].insert_one(newGame)

            return ""
        else:
            logThis("There's already a Love Letter game going on in this channel",str(channel))
            return "There's already a Love Letter game going on in this channel"

