# nbi:hide_in
# General Purpose
import numpy as np
from matplotlib import pyplot as plt
from scipy.integrate import odeint

# Jupyter Specifics
from ipywidgets.widgets import interact, IntSlider, FloatSlider, Layout

%matplotlib inline

def main(sueldo_inicial, tasa_ahorro, tasa_gasto_extra, gasto_fijo_diario, dias):
    ca = tasa_ahorro * sueldo_inicial
    
    def function(c, t):
        if c <= ca:
            tasa_salida = 0
        else:
            tasa_salida = tasa_gasto_extra * (c - ca)
        return -gasto_fijo_diario - tasa_salida
    
    t = np.linspace(0, dias, dias*10)
    
    y = odeint(function, sueldo_inicial, t)
    
    #Graphic details
    fig, ax = plt.subplots(figsize=(15, 10))    
    
    ax.plot((0, dias), (ca, ca), label='Colchon de ahorro')
    ax.plot(t, y, label='Capital(t)')
    
    if dias <= 60:
        step = 1
        rotation = "horizontal"
    elif dias <= 300:
        step = 5
        rotation = "vertical"
    else:
        step = 10
        rotation = "vertical"
    
    ax.set_xticklabels(np.arange(0, dias + 1, step, dtype=np.int), rotation=rotation)
    ax.set_xticks(np.arange(0, dias + 1, step))
    
    ax.set_yticks(np.arange(0, sueldo_inicial * 1.1, sueldo_inicial / 20))
    
    ax.set_xlim([0, dias])
    ax.set_ylim([0, sueldo_inicial * 1.1])
    ax.set_xlabel('Días')
    ax.set_ylabel('Capital $')
    ax.legend(loc='best')
    ax.grid()
    
    plt.show()

interact(main, sueldo_inicial=IntSlider(min=0, max=25000, step=500, value=15000, layout=Layout(width='99%')),
               tasa_ahorro=FloatSlider(min=0, max=1, step=0.01, value=0.2, layout=Layout(width='99%')),
               tasa_gasto_extra=FloatSlider(min=0, max=1, step=0.005, value=0.3, layout=Layout(width='99%')),
               gasto_fijo_diario=IntSlider(min=1, max=1000, step=1, value=100, layout=Layout(width='99%')),
               dias=IntSlider(min=1, max=600, step=5, value=30, layout=Layout(width='99%'))
        );