import json

from PIL import Image, ImageDraw, ImageFont

def drawImage(channel):
    with open("resources/games/games.json", "r") as f:
        data = json.load(f)

    fnt = ImageFont.truetype('resources/futura-bold.ttf', 50)
    fntSmol = ImageFont.truetype('resources/futura-bold.ttf', 40)

    table = Image.open("resources/games/blackjackTable.png")
    placement = [2,1,3,0,4]
    textImage = ImageDraw.Draw(table)
    hands = data["blackjack games"][channel]["user hands"]

    dealerBusted = data["blackjack games"][channel]["dealer busted"]
    dealerBlackjack = data["blackjack games"][channel]["dealer blackjack"]
    
    if data["blackjack games"][channel]["all standing"] == False:
        dealerHand = drawHand(data["blackjack games"][channel]["dealer hand"],True,False,False)
    else:
        dealerHand = drawHand(data["blackjack games"][channel]["dealer hand"],False,dealerBusted,dealerBlackjack)

    table.paste(dealerHand,(800,20),dealerHand)

    for x in range(len(hands)):
        key, value = list(hands.items())[x]
        userHand = drawHand(value["hand"],False,value["busted"],value["blackjack"])

        if value["split"]:
            table.paste(userHand,(32+(384*placement[x]),560),userHand)
            userOtherHand = drawHand(value["other hand"]["hand"],False,value["other hand"]["busted"],value["other hand"]["blackjack"])
            table.paste(userOtherHand,(32+(384*placement[x]),700),userOtherHand)
        else:
            table.paste(userHand,(32+(384*placement[x]),680),userHand)

        textWidth = fnt.getsize(key)[0]
        if textWidth < 360:
            textImage.text((32+(384*placement[x])+117-int(textWidth/2)-3,1010-3),key,fill=(0,0,0), font=fnt)
            textImage.text((32+(384*placement[x])+117-int(textWidth/2)+3,1010-3),key,fill=(0,0,0), font=fnt)
            textImage.text((32+(384*placement[x])+117-int(textWidth/2)-3,1010+3),key,fill=(0,0,0), font=fnt)
            textImage.text((32+(384*placement[x])+117-int(textWidth/2)+3,1010+3),key,fill=(0,0,0), font=fnt)
            textImage.text((32+(384*placement[x])+117-int(textWidth/2),1005),key,fill=(255,255,255), font=fnt)
        else:
            textWidth = fntSmol.getsize(key)[0]
            textImage.text((32+(384*placement[x])+117-int(textWidth/2)-3,1010-3),key,fill=(0,0,0), font=fntSmol)
            textImage.text((32+(384*placement[x])+117-int(textWidth/2)+3,1010-3),key,fill=(0,0,0), font=fntSmol)
            textImage.text((32+(384*placement[x])+117-int(textWidth/2)-3,1010+3),key,fill=(0,0,0), font=fntSmol)
            textImage.text((32+(384*placement[x])+117-int(textWidth/2)+3,1010+3),key,fill=(0,0,0), font=fntSmol)
            textImage.text((32+(384*placement[x])+117-int(textWidth/2),1005),key,fill=(255,255,255), font=fntSmol)

    table.save("resources/games/blackjackTables/blackjackTable"+channel+".png")

    return

def drawHand(hand, dealer, busted, blackjack):
    fnt = ImageFont.truetype('resources/futura-bold.ttf', 200)
    fnt2 = ImageFont.truetype('resources/futura-bold.ttf', 120)
    length = len(hand)
    background = Image.new("RGBA", (691+(125*(length-1)),1065),(0,0,0,0))
    textImage = ImageDraw.Draw(background)

    if dealer:
        img = Image.open("resources/games/cards/"+hand[0].upper()+".png")
        background.paste(img,(0,0),img)
        img = Image.open("resources/games/cards/red_back.png")
        background.paste(img,(125,0),img)
    else:
        for x in range(length):
            img = Image.open("resources/games/cards/"+hand[x].upper()+".png")
            background.paste(img,(x*125,0),img)
    
    w, h = background.size
    textHeight = 290

    if busted:
        textWidth = fnt.getsize("BUSTED")[0]
        textImage.text((int(w/2)-int(textWidth/2)-10,textHeight+20-10),"BUSTED",fill=(0,0,0), font=fnt)
        textImage.text((int(w/2)-int(textWidth/2)+10,textHeight+20-10),"BUSTED",fill=(0,0,0), font=fnt)
        textImage.text((int(w/2)-int(textWidth/2)-10,textHeight+20+10),"BUSTED",fill=(0,0,0), font=fnt)
        textImage.text((int(w/2)-int(textWidth/2)+10,textHeight+20+10),"BUSTED",fill=(0,0,0), font=fnt)
        textImage.text((int(w/2)-int(textWidth/2)-5,textHeight-5),"BUSTED",fill=(255,255,255), font=fnt)
        textImage.text((int(w/2)-int(textWidth/2)+5,textHeight-5),"BUSTED",fill=(255,255,255), font=fnt)
        textImage.text((int(w/2)-int(textWidth/2)-5,textHeight+5),"BUSTED",fill=(255,255,225), font=fnt)
        textImage.text((int(w/2)-int(textWidth/2)+5,textHeight+5),"BUSTED",fill=(255,255,255), font=fnt)
        textImage.text((int(w/2)-int(textWidth/2),textHeight),"BUSTED",fill=(255,50,50), font=fnt)
    elif blackjack:
        textWidth = fnt2.getsize("BLACKJACK")[0]
        textImage.text((int(w/2)-int(textWidth/2)-6,textHeight+20-6),"BLACKJACK",fill=(0,0,0), font=fnt2)
        textImage.text((int(w/2)-int(textWidth/2)+6,textHeight+20-6),"BLACKJACK",fill=(0,0,0), font=fnt2)
        textImage.text((int(w/2)-int(textWidth/2)-6,textHeight+20+6),"BLACKJACK",fill=(0,0,0), font=fnt2)
        textImage.text((int(w/2)-int(textWidth/2)+6,textHeight+20+6),"BLACKJACK",fill=(0,0,0), font=fnt2)
        textImage.text((int(w/2)-int(textWidth/2)-3,textHeight-3),"BLACKJACK",fill=(255,255,255), font=fnt2)
        textImage.text((int(w/2)-int(textWidth/2)+3,textHeight-3),"BLACKJACK",fill=(255,255,255), font=fnt2)
        textImage.text((int(w/2)-int(textWidth/2)-3,textHeight+3),"BLACKJACK",fill=(255,255,255), font=fnt2)
        textImage.text((int(w/2)-int(textWidth/2)+3,textHeight+3),"BLACKJACK",fill=(255,255,255), font=fnt2)
        textImage.text((int(w/2)-int(textWidth/2),textHeight),"BLACKJACK",fill=(155,123,0), font=fnt2)
    
    return background.resize((int(w/3.5),int(h/3.5)))
