import plotly.graph_objects as go
from PIL import Image, ImageOps

def gauge():
    labels = ['Red','Amber','Green','Blank']
    values = [20, 20, 60, 40]

    colors = ['#158b90', 
              '#f8981d', 
              '#f93f17', 
              '#000000']

    # Use `hole` to create a donut-like pie chart
    fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.75)])

    fig.update_traces(textinfo='none', 
                      textfont_size=20,
                      rotation=77.14,
                      sort=False,
                    marker=dict(colors=colors))
    
    fig.update_layout(paper_bgcolor='lightblue'        )

    fig.update(layout_showlegend=False)


    fig.update_layout(
    annotations=[dict(text='Target 65 BD', x=0.5, y=0.5,
                      font_size=25, showarrow=False, xanchor="center"),
                      dict(text='95%', x=0.5, y=0.58,
                      font_size=35, showarrow=False, xanchor="center"),

                      dict(text='0', x=0.0, y=0.0,
                      font_size=5, showarrow=False, xanchor="center"),

                      dict(text='0', x=1.0, y=1.0,
                      font_size=5, showarrow=False, xanchor="center"),

                      dict(text='0%', x=0.1, y=0.15,
                      font_size=20, showarrow=False, xanchor="right"),
                      dict(text='100%', x=0.90, y=0.15,
                      font_size=20, showarrow=False, xanchor="left"),
                      ])
    
    # pct = 0.95
    arrow = Image.open("arrow.png").rotate(90, expand=True)
    fig.add_layout_image(source=arrow, x=0.5, y=0.5, sizex=0.1, sizey=0.1, xanchor="center", yanchor="middle")

    arrow2 = Image.open("arrow.png")
    fig.add_layout_image(source=arrow2, x=0.7, y=0.5, sizex=0.1, sizey=0.1, xanchor="center", yanchor="middle")


    #rot = -(360/14*5)
    #arrow = arrow.rotate(90, expand=True)
    #arrow.save("arrow2.png")
    #fig.add_layout_image(source=arrow, x=0.5, y=0.5, sizex=0.60, sizey=0.60, xanchor="center", yanchor="middle")

    #fig.add_shape(type="circle", x0=0, y0=0, x1=1, y1=1, fillcolor="#000000")


    #fig.show()
    fig.write_image("fig1.png", scale=4)


def donut():
    labels = ['DMIRS','Other Agencies','Applicant','Parallel']
    values = [37, 157, 1, 21]

    colors = ['#9ac4c7', '#b4afbb', '#cc8671', '#a9cada']

    # Use `hole` to create a donut-like pie chart
    fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.6)])

    fig.update_traces(textinfo='value', textfont_size=20,
                    marker=dict(colors=colors, line=dict(color='#FFFFFF', width=3)))

    fig.update(layout_showlegend=False)
                    

    #fig.show()
    fig.write_image("fig1.png", width=600, height=600, scale=4)


if __name__ == "__main__":
    gauge()
    img = Image.open("fig1.png")
    inv = ImageOps.invert(img.convert("RGB"))
    print(img.size)
    print(inv.getbbox())
    out = img.crop(inv.getbbox())
    out.save("fig2.png")
