import json
import math

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

# 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 = 10
    w, h = 2800,2000
    backgroundColor = (128,128,128,255)
    boardColor = (0,0,170)

    placeSize = 270
    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)


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

    # 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)

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