import json
import math

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

# Draws the whole thing
def drawImage(channel):
    logThis("Drawing four in a row board")
    with open("resources/games/games.json", "r") as f:
        data = json.load(f)

    board = data["4 in a row games"][channel]["board"]

    border = 40
    gridBorder = 40
    cornerSize = 100
    outlineSize = 20
    bottomBorder = 110
    exampleCircles = 100
    w, h = 2800,2000
    backgroundColor = (128,128,128,255)
    boardColor = (0,0,170)
    placeSize = 285
    white = (255,255,255,160)
    winBarColor = (140,160,140,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 data["4 in a row games"][channel]["players"][0] == "Gwendolyn":
        player1 = "Gwendolyn"
    else:
        player1 = getName(data["4 in a row games"][channel]["players"][0])

    if data["4 in a row games"][channel]["players"][1] == "Gwendolyn":
        player2 = "Gwendolyn"
    else:
        player2 = getName(data["4 in a row games"][channel]["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=(0,0,0),width=outlineSize)
    d.ellipse([(w-(border+cornerSize),h-(border+cornerSize)),(w-border,h-border)],fill=boardColor,outline=(0,0,0),width=outlineSize)
    d.ellipse([(border,h-(border+cornerSize)),(border+cornerSize,h-border)],fill=boardColor,outline=(0,0,0),width=outlineSize)
    d.ellipse([(w-(border+cornerSize),border),(w-border,border+cornerSize)],fill=boardColor,outline=(0,0,0),width=outlineSize)
    # - Rectangle:
    d.rectangle([(border+math.floor(cornerSize/2),border),(w-(border+math.floor(cornerSize/2)),h-border)],fill=boardColor,outline=(0,0,0),width=outlineSize)
    d.rectangle([(border,border+math.floor(cornerSize/2)),(w-border,h-(border+math.floor(cornerSize/2)))],fill=boardColor,outline=(0,0,0),width=outlineSize)
    # - 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.ellipse([(border+outlineSize,border+outlineSize),(border+cornerSize-outlineSize,border+cornerSize-outlineSize)],fill=boardColor)
    d.ellipse([(w-(border+cornerSize)+outlineSize,h-(border+cornerSize)+outlineSize),(w-border-outlineSize,h-border-outlineSize)],fill=boardColor)
    d.ellipse([(border+outlineSize,h-(border+cornerSize)+outlineSize),(border+cornerSize-outlineSize,h-border-outlineSize)],fill=boardColor)
    d.ellipse([(w-(border+cornerSize)+outlineSize,border+outlineSize),(w-border-outlineSize,border+cornerSize-outlineSize)],fill=boardColor)

    for line in range(len(board)):
        for place in range(len(board[line])):
            piece = board[line][place]

            if piece == 1:
                pieceColor = (255,255,0)
            elif piece == 2:
                pieceColor = (200,0,0)
            else:
                pieceColor = backgroundColor

            startx = pieceStartx + placeGridSize[0]*place
            starty = pieceStarty + placeGridSize[1]*line

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

    if data["4 in a row games"][channel]["winner"] != 0:
        coordinates = data["4 in a row games"][channel]["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 data["4 in a row games"][channel]["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 data["4 in a row games"][channel]["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 data["4 in a row games"][channel]["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 -= border
            starty -= gridBorder + border

        elif data["4 in a row games"][channel]["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 + border
            starty -= gridBorder + border
        
        
        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=(255,255,0),outline=(0,0,0),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=(255,0,0),outline=(0,0,0),width=3)
    d.text((w-border-textWidth,exampleHeight),player2,font=fnt,fill=(0,0,0))


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