import matplotlib.pyplot as plt

def currency_fmt(num):
    return ("$"+("{:0,.2f}".format(float(num))))[:-3] # Cents are dropped from diplay

def make_basic_greenred(name, v1, v2):
    title = (name)
    width = 0.2
    bottom = 0

    #setup plot
    fig, ax = plt.subplots()
    fig.patch.set_facecolor((0,0,0,0))
    ax.set_facecolor((0,0,0,0))
    
    #make fist bar and offset the bottom line by it
    p = ax.bar(title, [v1], width, bottom=bottom, align='center',color='tab:green')
    ax.bar_label(p, label_type='center', fmt=currency_fmt)
    bottom += v1

    #make second bar graph 
    p = ax.bar(title, [v2], width, bottom=bottom, align='center', color='tab:red')
    ax.bar_label(p, label_type='center', fmt=currency_fmt)

    return fig

#Version of green red graph with no labels 
def make_basic_greenred_NL(name, v1, v2):
    title = (name)
    width = 0.2
    bottom = 0

    #setup plot
    fig, ax = plt.subplots()
    fig.patch.set_facecolor((0,0,0,0))
    ax.set_facecolor((0,0,0,0))
    
    #make fist bar and offset the bottom line by it
    p = ax.bar(title, [v1], width, bottom=bottom, align='center',color='tab:green')
    bottom += v1

    #make second bar graph 
    p = ax.bar(title, [v2], width, bottom=bottom, align='center', color='tab:red')

    return fig
    

### I Mountain Grahps

# Generates graph for Emergency Fund
def gen_EF(wb):
    fig = make_basic_greenred("Emergency Fund",wb["I Mountain"]["A2"].value,wb["I Mountain"]["C2"].value)

    fig.savefig('./.gen/EF.png',dpi=300)


# Generates graph for Life Insurance Tracker
def gen_LI(wb):
    fig = make_basic_greenred("Life Insurance Tracker",wb["I Mountain"]["A7"].value,wb["I Mountain"]["C7"].value)

    fig.savefig('./.gen/LI.png',dpi=300)

# Generates graph for Disability Insurance Tracker
def gen_DI(wb):
    fig = make_basic_greenred("Disability Insurance Tracker",wb["I Mountain"]["A11"].value,wb["I Mountain"]["C11"].value)

    fig.savefig('./.gen/DI.png',dpi=300)


### O Mountain Graphs 

# Generates graph for Roth IRA
def gen_Roth_IRA(wb):
    fig = make_basic_greenred("Roth IRA",wb["O Mountain"]["A2"].value,wb["O Mountain"]["C2"].value)

    fig.savefig('./.gen/Roth_IRA.png',dpi=300)

# Generates graph for Roth Catch Up
def gen_Roth_Catch_Up(wb):
    fig = make_basic_greenred("Roth Catch Up",wb["O Mountain"]["A4"].value,wb["O Mountain"]["C4"].value)

    fig.savefig('./.gen/Roth_Catch_Up.png',dpi=300)

# Generates graph for 401K contribution
def gen_401K_Contribution(wb):
    fig = make_basic_greenred("401K Contribution",wb["O Mountain"]["A6"].value,wb["O Mountain"]["C6"].value)

    fig.savefig('./.gen/401K_Contribution.png',dpi=300)

# Generates graph for 401K contribution Catch Up
def gen_401K_Catch_Up(wb):
    fig = make_basic_greenred("401K Catch Up",wb["O Mountain"]["A8"].value,wb["O Mountain"]["C8"].value)

    fig.savefig('./.gen/401K_Catch_Up.png',dpi=300)

# Generates graph for HSA Contribution
def gen_HSA_Contribution(wb):
    fig = make_basic_greenred("HSA Contribution",wb["O Mountain"]["A10"].value,wb["O Mountain"]["C10"].value)

    fig.savefig('./.gen/HSA_Contribution.png',dpi=300)

# Generates graph for FSA Contribution
def gen_FSA_Contribution(wb):
    fig = make_basic_greenred("FSA Contribution",wb["O Mountain"]["A12"].value,wb["O Mountain"]["C12"].value)

    fig.savefig('./.gen/FSA_Contribution.png',dpi=300)


### Oh I Mountain

# Generates main Oh I Mountain Graph
def gen_Oh_I_Mountain(wb):
    
    title = "Monthly Income Goal"
    width = 0.2
    bottom = 0

    #setup plot
    fig, ax = plt.subplots()
    fig.patch.set_facecolor((0,0,0,0))
    ax.set_facecolor((0,0,0,0))

    #Gets values
    values = [
        [wb["Oh I Mountain"]["A6"].value,"cornflowerblue"],
        [wb["Oh I Mountain"]["B6"].value,"orange"],
        [wb["Oh I Mountain"]["C6"].value,"mediumorchid"],
        [wb["Oh I Mountain"]["D6"].value,"gold"],
        [wb["Oh I Mountain"]["E6"].value,"darkturquoise"],
        [wb["Oh I Mountain"]["F6"].value,"limegreen"],
        [wb["Oh I Mountain"]["G6"].value,"royalblue"],
        [wb["Oh I Mountain"]["C2"].value,"red"]
    ]

    for value in values: 
        p = ax.bar(title, value[0], width, bottom=bottom, align='center',color=value[1])
        ax.bar_label(p, label_type='center', fmt=currency_fmt)
        bottom += value[0]
        
    fig.savefig('./.gen/Monthly_Income_Goal.png',dpi=300)

