# nbi:hide_in
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(a, b, c, d, days, initial_rabbits, initial_foxes):

    def function(s, t):
        x, y = s
        dydt = [
            a * x - b * x * y, # x(t) → Liebres
            -c * y + d * x * y # y(t) → Zorros
        ]
        
        return dydt
    
    t = np.arange(0, days, 0.01)
    s0 = [initial_rabbits, initial_foxes]
    sol = odeint(function, s0, t)

    #Graphic details
    fig, axes = plt.subplots(1, 2, figsize=(15, 10))
    
    ax = axes[0]
    
    ax.plot(t, sol[:, 0], label='Liebres(t)')
    ax.plot(t, sol[:, 1], label='Zorros(t)')
    
    if days <= 30:
        step = 1
        rotation = "horizontal"
    elif days <= 150:
        step = 5
        rotation = "vertical"
    else:
        step = 10
        rotation = "vertical"
    
    ax.set_xticklabels(np.arange(0, days + 1, step, dtype=int), rotation=rotation)
    ax.set_xticks(np.arange(0, days + 1, step))
        
    ax.set_xlim([0, days])
    ax.set_ylim([0, max(max(sol[:, 0]), max(sol[:, 1])) * 1.05])
    ax.set_xlabel('Tiempo')
    ax.set_ylabel('Unidades')
    ax.legend(loc='best')
    ax.grid()
    
    
    ax = axes[1]
    
    ax.plot(sol[:, 0], sol[:, 1], label='Zorros en función de las Liebres')

    ax.set_xlim([0, max(sol[:, 0]) * 1.05])
    ax.set_ylim([0, max(sol[:, 1]) * 1.05])
    ax.set_xlabel('Liebres')
    ax.set_ylabel('Zorros')
    ax.legend(loc='best')
    ax.grid()
    
    plt.tight_layout()
    plt.show()

interact(main, a=FloatSlider(min=0, max=24, step=0.01, value=1, layout=Layout(width='99%')),
               b=FloatSlider(min=0, max=24, step=0.01, value=1, layout=Layout(width='99%')),
               c=FloatSlider(min=0, max=24, step=0.01, value=1, layout=Layout(width='99%')),
               d=FloatSlider(min=0, max=24, step=0.01, value=1, layout=Layout(width='99%')),
               initial_rabbits=FloatSlider(min=0 , max=100, step=1, value=2, layout=Layout(width='99%')),
               initial_foxes=FloatSlider(min=0 , max=100, step=1, value=1, layout=Layout(width='99%')),
               days=FloatSlider(min=0 ,max=365 , step=10, value=15, layout=Layout(width='99%')),
        );