import math, json

from funcs import logThis

from PIL import Image, ImageDraw

w, h = 1440, 1440
largeSpace = 191
smallSpace = math.floor((w - 2*largeSpace)/9)
avatarSize = 50
avatarHalf = math.floor(avatarSize/2)

def drawImage(channel):
    logThis("Drawing monopoly board for "+channel)
    with open("resources/games/monopolyGames.json", "r") as f:
        data = json.load(f)

    board = Image.open("resources/games/monopolyBoard.png")
    d = ImageDraw.Draw(board,"RGBA")

    for key, value in list(data[channel]["players"].items()):
        logThis("Drawing "+key)
        try:
            x, y = getPosition(value["position"])
        except:
            logThis("Error getting position (error code 1641)")
        print(str(x)+", "+str(y))
        d.ellipse([(x-avatarHalf,y-avatarHalf),(x+avatarHalf,y+avatarHalf)],fill=(255,0,0))

    board.save("resources/games/monopolyBoards/monopolyBoard"+channel+".png")


def getPosition(positionNumber):
    print(positionNumber)

    x, y = 0, 0
    if positionNumber == 0 or positionNumber >= 30:
        x = math.floor(largeSpace/2)
    elif positionNumber > 0 and positionNumber < 10:
        x = math.floor(largeSpace - (smallSpace/2)) + (smallSpace*positionNumber)
    elif positionNumber >= 10 and positionNumber <= 20:
        x = w - math.floor(largeSpace/2)
    elif positionNumber > 20 and positionNumber < 30:
        x = w - math.floor(largeSpace - (smallSpace/2)) - (smallSpace*(positionNumber - 20))

    if positionNumber >= 0 and positionNumber <= 20:
        y = math.floor(largeSpace/2)
    elif positionNumber > 10 and positionNumber < 20:
        y = math.floor(largeSpace - (smallSpace/2)) + (smallSpace*(positionNumber-10))
    elif positionNumber >= 20 and positionNumber <= 30:
        y = h - math.floor(largeSpace/2)
    elif positionNumber > 30:
        y = h - math.floor(largeSpace - (smallSpace/2)) - (smallSpace*(positionNumber - 30))

    return x, y


