import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler

from plot_helpers import CO, arrowhead, bold_ticks
from splineA import splineA


def fig6_1():
    a = 0
    b = 4
    xd = np.array([0, 1, 2, 3, 4], dtype=float)
    yd = np.array([-2, 2, 0, 0.5, 3], dtype=float)
    nx = 100
    x = np.linspace(0, 4, nx)
    y = np.zeros(nx)
    for ix in range(nx):
        y[ix] = splineA(xd, yd, x[ix])

    plt.close("all")
    # get(gcf)
    fig, ax = plt.subplots(figsize=(8.8, 3.8))
    fig.subplots_adjust(left=0.03, right=0.98, bottom=0.08, top=0.98)
    ax.set_prop_cycle(cycler(color=CO))

    ax.plot(x, y, "b", linewidth=2)

    ax.plot([-0.5, 4.5], [-3, -3], "k", linewidth=2)
    arrowhead(ax, [4.1, 4.5], [-3, -3], "k", scale=12, linewidth=1)
    ax.plot([-0.2, -0.2], [-3.5, 3.5], "k", linewidth=2)
    arrowhead(ax, [-0.2, -0.2], [3.1, 3.8], "k", scale=12, linewidth=1)
    ax.set_xlim(-0.5, 4.5)
    ax.set_ylim(-4.2, 4)

    yf = -3.8
    x0 = 0.1
    lwr = 1
    ax.text(xd[0] - x0, yf + 0.02, r"$x_1$", fontsize=23)
    ax.plot([xd[0], xd[0]], [-3, yd[0]], "--b", linewidth=lwr)

    ax.text(xd[1] - x0, yf + 0.02, r"$x_2$", fontsize=23)
    ax.plot([xd[1], xd[1]], [-3, yd[1]], "--b", linewidth=lwr)

    ax.text(xd[2] - x0, yf + 0.02, r"$x_3$", fontsize=23)
    ax.plot([xd[2], xd[2]], [-3, yd[2]], "--b", linewidth=lwr)

    ax.text(xd[3] - x0, yf + 0.02, r"$x_4$", fontsize=23)
    ax.plot([xd[3], xd[3]], [-3, yd[3]], "--b", linewidth=lwr)

    ax.text(xd[4] - x0, yf + 0.02, r"$x_5$", fontsize=23)
    ax.plot([xd[4], xd[4]], [-3, yd[4]], "--b", linewidth=lwr)

    ax.text(4.4, yf + 0.02, r"$x$", fontsize=23)
    ax.text(-0.4, 3.6, r"$y$", fontsize=23)

    ax.set_axis_off()
    bold_ticks(ax, 14)
    plt.show()

    # plt.savefig('/Users/mark/Desktop/integ1.eps')


if __name__ == "__main__":
    fig6_1()
