import numpy as np
import matplotlib.pyplot as plt
import fillesing as fl

def Analytical(r = 0.1, T_s = 22, T_0 = 83, t_f = 50, t_0 = 0):

    tid = np.arange(0, t_f, 0.1)
    temp = T_s + (T_0 - T_s) * np.exp(-r * tid)

    return tid, temp

def plot_Analytical(r = 0.1, T_s = 22, T_0 = 83, t_f = 50, t_0 = 0):
    x_points, y_points = Analytical(r = r, T_s = T_s, T_0 = T_0, t_f = t_f, t_0 = t_0)
    plt.plot(x_points, y_points, "r", label = "Analytic")
    plt.legend()
    plt.xlabel("Time, [min]")
    plt.ylabel("Temperature, [°C]")


def Euler(h, r=0.1, T_s=22, T_0=83, T_f=10, t_0=0, t_f=50):
    N = int((t_f - t_0) / h)  # number of steps

    T_points = np.zeros(N)  # temperaturlista
    T_points[0] = T_0

    def f(T, T_s=T_s, r=r):
        return -r * (T - T_s)

    for n in range(len(T_points) - 1):
        T_points[n + 1] = T_points[n] + h * f(T_points[n])

    t_points = np.linspace(t_0, t_f, N)

    return t_points, T_points

def plot_Euler(h, r=0.1, T_s=22, T_0=83, T_f=10, t_0=0, t_f=50):
    x_points, y_points = Euler(h, r=r, T_s=T_s, T_0=T_0, T_f=T_f, t_0=t_0, t_f=t_f)
    plt.plot(x_points, y_points, "g", label="Eulers")
    plt.legend()
    plt.xlabel("Time, [min]")
    plt.ylabel("Temperature, [°C]")


def plot_Analytical_Euler(h,r=0.1, T_s=22, T_0 = 83, T_f=10, t_0=0, t_f=50,
                          show_experimental_data = False, show_analytical = True,
                          show_numeric = True):  #h steglengde
    if show_analytical:
        plot_Analytical(r = r, T_s = T_s, T_0 = T_0, t_f = t_f, t_0 = 0)

    if show_numeric:
        plot_Euler(h, r=r, T_s=T_s, T_0=T_0, T_f=T_f, t_0=t_0, t_f=t_f)

    if show_experimental_data:
        tid, temp = fl.les_real_cup()
        plt.scatter(tid, temp, label = 'Experimental data')

    plt.xlim(t_0,t_f)