import math

from PIL import Image, ImageDraw, ImageFont
from funcs import logThis

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

    # Draws the whole thing
    def drawImage(self, channel):
        logThis("Drawing four in a row board")
        game = self.bot.database["4 in a row games"].find_one({"_id":channel})

        board = game["board"]

        border = 40
        gridBorder = 40
        cornerSize = 300
        boardOutlineSize = 10
        pieceOutlineSize = 10
        emptyOutlineSize = 0
        bottomBorder = 110
        exampleCircles = 100
        w, h = 2800,2400
        backgroundColor = (230,230,234,255)
        boardOutlineColor = (0,0,0)
        pieceOutlineColor = (244,244,248)
        emptyOutlineColor = (0,0,0)
        player1Color = (254,74,73)
        player2Color = (254,215,102)
        boardColor = (42,183,202)
        placeSize = 300
        white = (255,255,255,160)
        winBarColor = (250,250,250,255)

        fnt = ImageFont.truetype('resources/futura-bold.ttf', exampleCircles)

        boardSize = [w-(2*(border+gridBorder)),h-(2*(border+gridBorder))]
        placeGridSize = [math.floor(boardSize[0]/7),math.floor(boardSize[1]/6)]
        pieceStartx = (border+gridBorder)+math.floor(placeGridSize[0]/2)-math.floor(placeSize/2)
        pieceStarty = (border+gridBorder)+math.floor(placeGridSize[1]/2)-math.floor(placeSize/2)

        if game["players"][0] == "Gwendolyn":
            player1 = "Gwendolyn"
        else:
            player1 = self.bot.funcs.getName(game["players"][0])

        if game["players"][1] == "Gwendolyn":
            player2 = "Gwendolyn"
        else:
            player2 = self.bot.funcs.getName(game["players"][1])


        background = Image.new("RGB", (w,h+bottomBorder),backgroundColor)
        d = ImageDraw.Draw(background,"RGBA")

        # This whole part was the easiest way to make a rectangle with rounded corners and an outline
        # - Corners:
        d.ellipse([(border,border),(border+cornerSize,border+cornerSize)],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
        d.ellipse([(w-(border+cornerSize),h-(border+cornerSize)),(w-border,h-border)],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
        d.ellipse([(border,h-(border+cornerSize)),(border+cornerSize,h-border)],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
        d.ellipse([(w-(border+cornerSize),border),(w-border,border+cornerSize)],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
        # - Rectangle:
        d.rectangle([(border+math.floor(cornerSize/2),border),(w-(border+math.floor(cornerSize/2)),h-border)],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
        d.rectangle([(border,border+math.floor(cornerSize/2)),(w-border,h-(border+math.floor(cornerSize/2)))],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
        # - Removing outline on the inside:
        d.rectangle([(border+math.floor(cornerSize/2),border+math.floor(cornerSize/2)),(w-(border+math.floor(cornerSize/2)),h-(border+math.floor(cornerSize/2)))],fill=boardColor)
        d.rectangle([(border+math.floor(cornerSize/2),border+boardOutlineSize),(w-(border+math.floor(cornerSize/2)),h-(border+boardOutlineSize))],fill=boardColor)
        d.rectangle([(border+boardOutlineSize,border+math.floor(cornerSize/2)),(w-(border+boardOutlineSize),h-(border+math.floor(cornerSize/2)))],fill=boardColor)

        for x, line in enumerate(board):
            for y, piece in enumerate(line):

                if piece == 1:
                    pieceColor = player1Color
                    outlineWidth = pieceOutlineSize
                    outlineColor = pieceOutlineColor
                elif piece == 2:
                    pieceColor = player2Color
                    outlineWidth = pieceOutlineSize
                    outlineColor = pieceOutlineColor
                else:
                    pieceColor = backgroundColor
                    outlineWidth = emptyOutlineSize
                    outlineColor = emptyOutlineColor

                startx = pieceStartx + placeGridSize[0]*y
                starty = pieceStarty + placeGridSize[1]*x

                d.ellipse([(startx,starty),(startx+placeSize,starty+placeSize)],fill=pieceColor,outline=outlineColor,width=outlineWidth)

        if game["winner"] != 0:
            coordinates = game["win coordinates"]
            startx = border + placeGridSize[0]*coordinates[1] + gridBorder
            starty = border + placeGridSize[1]*coordinates[0] + gridBorder
            a = (placeGridSize[0]*4-gridBorder-border)**2
            b = (placeGridSize[1]*4-gridBorder-border)**2
            diagonalLength = (math.sqrt(a+b))/placeGridSize[0]
            diagonalAngle = math.degrees(math.atan(placeGridSize[1]/placeGridSize[0]))

            if game["win direction"] == "h":
                winBar = Image.new("RGBA",(placeGridSize[0]*4,placeGridSize[1]),(0,0,0,0))
                winD = ImageDraw.Draw(winBar)
                winD.ellipse([(0,0),(placeGridSize[0],placeGridSize[1])],fill=white)
                winD.ellipse([((placeGridSize[0]*3),0),(placeGridSize[0]*4,placeGridSize[1])],fill=white)
                winD.rectangle([(int(placeGridSize[0]*0.5),0),(int(placeGridSize[0]*3.5),placeGridSize[1])],fill=white)

            elif game["win direction"] == "v":
                winBar = Image.new("RGBA",(placeGridSize[0],placeGridSize[1]*4),(0,0,0,0))
                winD = ImageDraw.Draw(winBar)
                winD.ellipse([(0,0),(placeGridSize[0],placeGridSize[1])],fill=white)
                winD.ellipse([(0,(placeGridSize[1]*3)),(placeGridSize[0],placeGridSize[1]*4)],fill=white)
                winD.rectangle([0,(int(placeGridSize[1]*0.5)),(placeGridSize[0],int(placeGridSize[1]*3.5))],fill=white)

            elif game["win direction"] == "r":
                winBar = Image.new("RGBA",(int(placeGridSize[0]*diagonalLength),placeGridSize[1]),(0,0,0,0))
                winD = ImageDraw.Draw(winBar)
                winD.ellipse([(0,0),(placeGridSize[0],placeGridSize[1])],fill=white)
                winD.ellipse([((placeGridSize[0]*(diagonalLength-1)),0),(placeGridSize[0]*diagonalLength,placeGridSize[1])],fill=white)
                winD.rectangle([(int(placeGridSize[0]*0.5),0),(int(placeGridSize[0]*(diagonalLength-0.5)),placeGridSize[1])],fill=white)
                winBar = winBar.rotate(-diagonalAngle,expand=1)
                startx -= 90
                starty -= 100

            elif game["win direction"] == "l":
                winBar = Image.new("RGBA",(int(placeGridSize[0]*diagonalLength),placeGridSize[1]),(0,0,0,0))
                winD = ImageDraw.Draw(winBar)
                winD.ellipse([(0,0),(placeGridSize[0],placeGridSize[1])],fill=white)
                winD.ellipse([((placeGridSize[0]*(diagonalLength-1)),0),(placeGridSize[0]*diagonalLength,placeGridSize[1])],fill=white)
                winD.rectangle([(int(placeGridSize[0]*0.5),0),(int(placeGridSize[0]*(diagonalLength-0.5)),placeGridSize[1])],fill=white)
                winBar = winBar.rotate(diagonalAngle,expand=1)
                startx -= placeGridSize[0]*3 + 90
                starty -= gridBorder + 60


            mask = winBar.copy()#.convert("L")
            #mask.putalpha(128)
            #mask.save("test.png")

            winBarImage = Image.new("RGBA",mask.size,color=winBarColor)
            background.paste(winBarImage,(startx,starty),mask)

        # Bottom
        textPadding = 20

        exampleHeight = h - border + int((bottomBorder+border)/2) - int(exampleCircles/2)
        d.ellipse([(border,exampleHeight),(border+exampleCircles),(exampleHeight+exampleCircles)],fill=player1Color,outline=boardOutlineColor,width=3)
        d.text((border+exampleCircles+textPadding,exampleHeight),player1,font=fnt,fill=(0,0,0))

        textWidth = fnt.getsize(player2)[0]
        d.ellipse([(w-border-exampleCircles-textWidth-textPadding,exampleHeight),(w-border-textWidth-textPadding),(exampleHeight+exampleCircles)],fill=player2Color,outline=boardOutlineColor,width=3)
        d.text((w-border-textWidth,exampleHeight),player2,font=fnt,fill=(0,0,0))


        background.save("resources/games/4InARowBoards/board"+channel+".png")
