from .miscFuncs import logThis
import git              # Used by stopServer()
import re, json

class Funcs():
    def __init__(self,bot):
        self.bot = bot

    def getName(self,userID):
        user = self.bot.database["users"].find_one({"_id":userID})

        if user != None:
            return user["user name"]
        elif userID == "Gwendolyn":
            return userID
        else:
            logThis("Couldn't find user "+userID)
            return userID

    def getID(self,userName):
        user = self.bot.database["users"].find_one({"user name":re.compile(userName, re.IGNORECASE)})

        if user != None:
            return user["_id"]
        else:
            logThis("Couldn't find user "+userName)
            return None

    def deleteGame(self, gameType,channel):
        self.bot.database[gameType].delete_one({"_id":channel})

    def stopServer(self):
        self.bot.database["trivia questions"].delete_many({})
        self.bot.database["blackjack games"].delete_many({})
        self.bot.database["werewolf games"].delete_many({})

        g = git.cmd.Git("")
        g.pull()

    def fiarReactionTest(self,channel,message,user):
        game = self.bot.database["4 in a row games"].find_one({"_id":str(channel.id)})

        with open("resources/games/oldImages/fourInARow"+str(channel.id), "r") as f:
            oldImage = int(f.read())

        if message.id == oldImage:
            logThis("They reacted to the fourinarow game")
            turn = game["turn"]
            if user == game["players"][turn]:
                return True, turn+1
            else:
                logThis("It wasn't their turn")
                return False, 0
        else:
            return False, 0

    def monopolyReactionTest(self,channel,message):
        try:
            with open("resources/games/oldImages/monopoly"+str(channel.id), "r") as f:
                oldImage = int(f.read())
        except:
            return False

        if message.id == oldImage:
            logThis("They reacted to the monopoly game")
            return True
        else:
            return False

    def hangmanReactionTest(self, channel,message):
        try:
            with open("resources/games/oldImages/hangman"+str(channel.id), "r") as f:
                oldMessages = f.read().splitlines()
        except:
            return False
        gameMessage = False

        for oldMessage in oldMessages:
            oldMessageID = int(oldMessage)
            if message.id == oldMessageID:
                logThis("They reacted to the hangman game")
                gameMessage = True

        return gameMessage

    def bedreNetflixReactionTest(self,channel,message):
        try:
            with open("resources/bedreNetflix/oldMessage"+str(channel.id),"r") as f:
                data = json.load(f)
        except:
            return False, None, None
        if data["messageID"] == message.id:
            if "imdbIds" in data:
                return True, True, data["imdbIds"]
            else:
                return True, False, data["imdbNames"]
        else:
            return False, None, None