from openpyxl import Workbook
import cords
from PIL import Image, ImageDraw, ImageFont
import numpy as np

def gen_I_Mountain(wb):
    ## I Mountain image code
    I_Mountain = Image.open('./templets/I_mountain.png').convert('RGBA').resize((1920, 1080))
    I_Mountain_Mask = Image.open('./templets/mask/I_mountain_mask.png').convert('L')
    blank = Image.open('./templets/I_mountain.png').convert('RGBA').resize((1920, 1080))

    EF = Image.open('./.gen/EF.png')
    EF = cords.scale(EF,0.265)
    blank.paste(EF, cords.center_on(EF,(463,805)),EF)

    DI = Image.open('./.gen/DI.png')
    DI = cords.scale(DI,0.265)
    blank.paste(DI, cords.center_on(DI,(698,450)),DI)

    LI = Image.open('./.gen/LI.png')
    LI = cords.scale(LI,0.265)
    blank.paste(LI, cords.center_on(LI,(1472,802)),LI)

    #Mask and combine
    graph_comp = Image.composite(blank, I_Mountain, I_Mountain_Mask) #.save("./gen/I_mountain.png")
    draw = ImageDraw.Draw(graph_comp)
    font = ImageFont.truetype("./fonts/Asap_Condensed/AsapCondensed-Bold.ttf", 36)
    draw.text((442, 623),format_frendly(wb["I Mountain"]["A2"].value),(0,0,0),font=font)
    
    #Add box values

    if wb["I Mountain"]["B16"].value:
        icon = Image.open('./templets/icons/checkmark-svgrepo-com.png')
        icon = cords.scale(icon,0.035)
        graph_comp.paste(icon, cords.center_on(icon, (1125,365)),icon)
    else:
        icon = Image.open('./templets/icons/checkmark-svgrepo-com.png')
        icon = cords.scale(icon,0.035)
        graph_comp.paste(icon, cords.center_on(icon, (1237,365)),icon)

    if wb["I Mountain"]["B17"].value:
        icon = Image.open('./templets/icons/checkmark-svgrepo-com.png')
        icon = cords.scale(icon,0.035)
        graph_comp.paste(icon, cords.center_on(icon, (910,910)),icon)
    else:
        icon = Image.open('./templets/icons/checkmark-svgrepo-com.png')
        icon = cords.scale(icon,0.035)
        graph_comp.paste(icon, cords.center_on(icon, (1035,910)),icon)

    graph_comp.save("./gen/I_mountain.png")

    





def gen_O_Mountain(wb):
    ## O Mountain Image Code
    O_Mountain = Image.open('./templets/O_mountain.png').convert('RGBA').resize((1920, 1080))
    O_Mountain_Mask = Image.open('./templets/mask/O_mountain_mask.png').convert('L')
    O_Mountain_Mask1 = Image.open('./templets/mask/O_mountain_mask1.png').convert('L')
    O_Mountain_Mask2 = Image.open('./templets/mask/O_mountain_mask2.png').convert('L')
    blank = Image.open('./templets/O_mountain.png').convert('RGBA').resize((1920, 1080))
    blank2 = Image.new('RGBA',(1920,1080))

    K401 = Image.open('./.gen/401K_Catch_Up.png')
    K401 = cords.scale(K401,0.378)
    blank.paste(K401, cords.center_on(K401,(549,806)),K401)

    K401C = Image.open('./.gen/401K_Contribution.png')
    K401C = cords.scale(K401C,0.378)
    blank2.paste(K401C, cords.center_on(K401C,(384,806)),K401C)

    IRAC = Image.open('./.gen/Roth_Catch_Up.png')
    IRAC = cords.scale(IRAC,0.265)
    blank.paste(IRAC, cords.center_on(IRAC,(1172,855)),IRAC)

    IRA = Image.open('./.gen/Roth_IRA.png')
    IRA = cords.scale(IRA,0.265)
    blank2.paste(IRA, cords.center_on(IRA,(1421,855)),IRA)


    if wb["O Mountain"]["B16"].value:
        HSA = Image.open('./.gen/HSA_Contribution.png')
        HSA = cords.scale(HSA,0.265)
        blank2.paste(HSA, cords.center_on(HSA,(764,445)),HSA)
    else: 
        FSA = Image.open('./.gen/FSA_Contribution.png')
        FSA = cords.scale(FSA,0.265)
        blank2.paste(FSA, cords.center_on(FSA,(764,445)),FSA)

    


    #Mask and combine
    blank2 = Image.composite(blank2, Image.new('RGBA',(1920,1080)), O_Mountain_Mask2)
    blank = Image.alpha_composite(blank,blank2)

    
    graph_comp = Image.composite(blank, O_Mountain, O_Mountain_Mask)
    draw = ImageDraw.Draw(graph_comp)
    font = ImageFont.truetype("./fonts/Asap_Condensed/AsapCondensed-Bold.ttf", 36)

    draw.text((440, 595),format_frendly(wb["O Mountain"]["B6"].value),(0,0,0),font=font)
    draw.text((605, 595),format_frendly(wb["O Mountain"]["B8"].value),(0,0,0),font=font)

    draw.text((1485, 703),format_frendly(wb["O Mountain"]["B2"].value),(0,0,0),font=font)
    draw.text((1240, 703),format_frendly(wb["O Mountain"]["B4"].value),(0,0,0),font=font)

    graph_comp.save("./gen/O_mountian.png")



def gen_Oh_I_Mountain(wb):
    ## Oh I Mountain Image Code
    Oh_I_Mountain = Image.open('./templets/Oh_I_mountain.png').convert('RGBA').resize((1920, 1080))
    Oh_I_Mountain_Mask = Image.open('./templets/mask/Oh_I_mountain_mask.png').convert('L')
    blank = Image.open('./templets/Oh_I_mountain.png').convert('RGBA').resize((1920, 1080))

    Goal = Image.open('./.gen/Monthly_Income_Goal.png')
    Goal = cords.scale(Goal,0.68)
    blank.paste(Goal, cords.center_on(Goal,(946,673)),Goal)

    #Mask and combine
    Image.composite(blank, Oh_I_Mountain, Oh_I_Mountain_Mask).save("./gen/Oh_I_mountain.png")



def format_frendly(num):
    T = num / 1000
    return str(int(np.floor(T)))+"K"
