import matplotlib.pyplot as plt
import numpy as np
import math
from algorithm.planner.utils.car_utils import Car_C

PI = np.pi


class Arrow:
    def __init__(self, x, y, theta, L, c):
        angle = np.deg2rad(30)
        d = 0.3 * L
        w = 2

        x_start = x
        y_start = y
        x_end = x + L * np.cos(theta)
        y_end = y + L * np.sin(theta)

        theta_hat_L = theta + PI - angle
        theta_hat_R = theta + PI + angle

        x_hat_start = x_end
        x_hat_end_L = x_hat_start + d * np.cos(theta_hat_L)
        x_hat_end_R = x_hat_start + d * np.cos(theta_hat_R)

        y_hat_start = y_end
        y_hat_end_L = y_hat_start + d * np.sin(theta_hat_L)
        y_hat_end_R = y_hat_start + d * np.sin(theta_hat_R)

        plt.plot([x_start, x_end], [y_start, y_end], color=c, linewidth=w)
        plt.plot([x_hat_start, x_hat_end_L],
                 [y_hat_start, y_hat_end_L], color=c, linewidth=w)
        plt.plot([x_hat_start, x_hat_end_R],
                 [y_hat_start, y_hat_end_R], color=c, linewidth=w)


class Car:
    def __init__(self, x, y, yaw, w, L):
        theta_B = PI + yaw

        xB = x + L / 4 * np.cos(theta_B)
        yB = y + L / 4 * np.sin(theta_B)

        theta_BL = theta_B + PI / 2
        theta_BR = theta_B - PI / 2

        x_BL = xB + w / 2 * np.cos(theta_BL)        # Bottom-Left vertex
        y_BL = yB + w / 2 * np.sin(theta_BL)
        x_BR = xB + w / 2 * np.cos(theta_BR)        # Bottom-Right vertex
        y_BR = yB + w / 2 * np.sin(theta_BR)

        x_FL = x_BL + L * np.cos(yaw)               # Front-Left vertex
        y_FL = y_BL + L * np.sin(yaw)
        x_FR = x_BR + L * np.cos(yaw)               # Front-Right vertex
        y_FR = y_BR + L * np.sin(yaw)

        plt.plot([x_BL, x_BR, x_FR, x_FL, x_BL],
                 [y_BL, y_BR, y_FR, y_FL, y_BL],
                 linewidth=1, color='black')

        Arrow(x, y, yaw, L / 2, 'black')


def draw_car(x, y, yaw, steer, color='black', extended_car=True):

    if extended_car:
        car = np.array([[-Car_C.RB, -Car_C.RB, Car_C.RF, Car_C.RF, -Car_C.RB, Car_C.ACTUAL_RF, Car_C.ACTUAL_RF, -Car_C.ACTUAL_RB, -Car_C.ACTUAL_RB],
                    [Car_C.W / 2, -Car_C.W / 2, -Car_C.W / 2, Car_C.W / 2, Car_C.W / 2, Car_C.W/2, -Car_C.W/2, -Car_C.W/2, Car_C.W/2]])
    else:
        car = np.array([[-Car_C.RB, -Car_C.RB, Car_C.RF, Car_C.RF, -Car_C.RB],
                    [Car_C.W / 2, -Car_C.W / 2, -Car_C.W / 2, Car_C.W / 2, Car_C.W / 2]])

    wheel = np.array([[-Car_C.TR, -Car_C.TR, Car_C.TR, Car_C.TR, -Car_C.TR],
                      [Car_C.TW / 4, -Car_C.TW / 4, -Car_C.TW / 4, Car_C.TW / 4, Car_C.TW / 4]])

    rlWheel = wheel.copy()
    rrWheel = wheel.copy()
    frWheel = wheel.copy()
    flWheel = wheel.copy()

    Rot1 = np.array([[math.cos(yaw), -math.sin(yaw)],
                     [math.sin(yaw), math.cos(yaw)]])

    Rot2 = np.array([[math.cos(steer), math.sin(steer)],
                     [-math.sin(steer), math.cos(steer)]])

    frWheel = np.dot(Rot2, frWheel)
    flWheel = np.dot(Rot2, flWheel)

    frWheel += np.array([[Car_C.WB], [-Car_C.WD / 2]])
    flWheel += np.array([[Car_C.WB], [Car_C.WD / 2]])
    rrWheel[1, :] -= Car_C.WD / 2
    rlWheel[1, :] += Car_C.WD / 2

    frWheel = np.dot(Rot1, frWheel)
    flWheel = np.dot(Rot1, flWheel)

    rrWheel = np.dot(Rot1, rrWheel)
    rlWheel = np.dot(Rot1, rlWheel)
    car = np.dot(Rot1, car)

    frWheel += np.array([[x], [y]])
    flWheel += np.array([[x], [y]])
    rrWheel += np.array([[x], [y]])
    rlWheel += np.array([[x], [y]])
    car += np.array([[x], [y]])

    plt.plot(car[0, :], car[1, :], color)
    plt.plot(frWheel[0, :], frWheel[1, :], color)
    plt.plot(rrWheel[0, :], rrWheel[1, :], color)
    plt.plot(flWheel[0, :], flWheel[1, :], color)
    plt.plot(rlWheel[0, :], rlWheel[1, :], color)
    Arrow(x, y, yaw, Car_C.WB * 0.8, color)


[(74, 'numpy.array', 'np.array', (['[[-Car_C.TR, -Car_C.TR, Car_C.TR, Car_C.TR, -Car_C.TR], [Car_C.TW / 4, -\n    Car_C.TW / 4, -Car_C.TW / 4, Car_C.TW / 4, Car_C.TW / 4]]'], {}), True, 'import numpy as np\n'), (88, 'numpy.dot', 'np.dot', (['Rot2', 'frWheel'], {}), True, 'import numpy as np\n'), (89, 'numpy.dot', 'np.dot', (['Rot2', 'flWheel'], {}), True, 'import numpy as np\n'), (91, 'numpy.array', 'np.array', (['[[Car_C.WB], [-Car_C.WD / 2]]'], {}), True, 'import numpy as np\n'), (92, 'numpy.array', 'np.array', (['[[Car_C.WB], [Car_C.WD / 2]]'], {}), True, 'import numpy as np\n'), (96, 'numpy.dot', 'np.dot', (['Rot1', 'frWheel'], {}), True, 'import numpy as np\n'), (97, 'numpy.dot', 'np.dot', (['Rot1', 'flWheel'], {}), True, 'import numpy as np\n'), (99, 'numpy.dot', 'np.dot', (['Rot1', 'rrWheel'], {}), True, 'import numpy as np\n'), (100, 'numpy.dot', 'np.dot', (['Rot1', 'rlWheel'], {}), True, 'import numpy as np\n'), (101, 'numpy.dot', 'np.dot', (['Rot1', 'car'], {}), True, 'import numpy as np\n'), (103, 'numpy.array', 'np.array', (['[[x], [y]]'], {}), True, 'import numpy as np\n'), (104, 'numpy.array', 'np.array', (['[[x], [y]]'], {}), True, 'import numpy as np\n'), (105, 'numpy.array', 'np.array', (['[[x], [y]]'], {}), True, 'import numpy as np\n'), (106, 'numpy.array', 'np.array', (['[[x], [y]]'], {}), True, 'import numpy as np\n'), (107, 'numpy.array', 'np.array', (['[[x], [y]]'], {}), True, 'import numpy as np\n'), (109, 'matplotlib.pyplot.plot', 'plt.plot', (['car[(0), :]', 'car[(1), :]', 'color'], {}), True, 'import matplotlib.pyplot as plt\n'), (110, 'matplotlib.pyplot.plot', 'plt.plot', (['frWheel[(0), :]', 'frWheel[(1), :]', 'color'], {}), True, 'import matplotlib.pyplot as plt\n'), (111, 'matplotlib.pyplot.plot', 'plt.plot', (['rrWheel[(0), :]', 'rrWheel[(1), :]', 'color'], {}), True, 'import matplotlib.pyplot as plt\n'), (112, 'matplotlib.pyplot.plot', 'plt.plot', (['flWheel[(0), :]', 'flWheel[(1), :]', 'color'], {}), True, 'import matplotlib.pyplot as plt\n'), (113, 'matplotlib.pyplot.plot', 'plt.plot', (['rlWheel[(0), :]', 'rlWheel[(1), :]', 'color'], {}), True, 'import matplotlib.pyplot as plt\n'), (11, 'numpy.deg2rad', 'np.deg2rad', (['(30)'], {}), True, 'import numpy as np\n'), (31, 'matplotlib.pyplot.plot', 'plt.plot', (['[x_start, x_end]', '[y_start, y_end]'], {'color': 'c', 'linewidth': 'w'}), True, 'import matplotlib.pyplot as plt\n'), (32, 'matplotlib.pyplot.plot', 'plt.plot', (['[x_hat_start, x_hat_end_L]', '[y_hat_start, y_hat_end_L]'], {'color': 'c', 'linewidth': 'w'}), True, 'import matplotlib.pyplot as plt\n'), (34, 'matplotlib.pyplot.plot', 'plt.plot', (['[x_hat_start, x_hat_end_R]', '[y_hat_start, y_hat_end_R]'], {'color': 'c', 'linewidth': 'w'}), True, 'import matplotlib.pyplot as plt\n'), (58, 'matplotlib.pyplot.plot', 'plt.plot', (['[x_BL, x_BR, x_FR, x_FL, x_BL]', '[y_BL, y_BR, y_FR, y_FL, y_BL]'], {'linewidth': '(1)', 'color': '"""black"""'}), True, 'import matplotlib.pyplot as plt\n'), (68, 'numpy.array', 'np.array', (['[[-Car_C.RB, -Car_C.RB, Car_C.RF, Car_C.RF, -Car_C.RB, Car_C.ACTUAL_RF,\n    Car_C.ACTUAL_RF, -Car_C.ACTUAL_RB, -Car_C.ACTUAL_RB], [Car_C.W / 2, -\n    Car_C.W / 2, -Car_C.W / 2, Car_C.W / 2, Car_C.W / 2, Car_C.W / 2, -\n    Car_C.W / 2, -Car_C.W / 2, Car_C.W / 2]]'], {}), True, 'import numpy as np\n'), (71, 'numpy.array', 'np.array', (['[[-Car_C.RB, -Car_C.RB, Car_C.RF, Car_C.RF, -Car_C.RB], [Car_C.W / 2, -\n    Car_C.W / 2, -Car_C.W / 2, Car_C.W / 2, Car_C.W / 2]]'], {}), True, 'import numpy as np\n'), (17, 'numpy.cos', 'np.cos', (['theta'], {}), True, 'import numpy as np\n'), (18, 'numpy.sin', 'np.sin', (['theta'], {}), True, 'import numpy as np\n'), (24, 'numpy.cos', 'np.cos', (['theta_hat_L'], {}), True, 'import numpy as np\n'), (25, 'numpy.cos', 'np.cos', (['theta_hat_R'], {}), True, 'import numpy as np\n'), (28, 'numpy.sin', 'np.sin', (['theta_hat_L'], {}), True, 'import numpy as np\n'), (29, 'numpy.sin', 'np.sin', (['theta_hat_R'], {}), True, 'import numpy as np\n'), (42, 'numpy.cos', 'np.cos', (['theta_B'], {}), True, 'import numpy as np\n'), (43, 'numpy.sin', 'np.sin', (['theta_B'], {}), True, 'import numpy as np\n'), (48, 'numpy.cos', 'np.cos', (['theta_BL'], {}), True, 'import numpy as np\n'), (49, 'numpy.sin', 'np.sin', (['theta_BL'], {}), True, 'import numpy as np\n'), (50, 'numpy.cos', 'np.cos', (['theta_BR'], {}), True, 'import numpy as np\n'), (51, 'numpy.sin', 'np.sin', (['theta_BR'], {}), True, 'import numpy as np\n'), (53, 'numpy.cos', 'np.cos', (['yaw'], {}), True, 'import numpy as np\n'), (54, 'numpy.sin', 'np.sin', (['yaw'], {}), True, 'import numpy as np\n'), (55, 'numpy.cos', 'np.cos', (['yaw'], {}), True, 'import numpy as np\n'), (56, 'numpy.sin', 'np.sin', (['yaw'], {}), True, 'import numpy as np\n'), (82, 'math.cos', 'math.cos', (['yaw'], {}), False, 'import math\n'), (83, 'math.sin', 'math.sin', (['yaw'], {}), False, 'import math\n'), (83, 'math.cos', 'math.cos', (['yaw'], {}), False, 'import math\n'), (85, 'math.cos', 'math.cos', (['steer'], {}), False, 'import math\n'), (85, 'math.sin', 'math.sin', (['steer'], {}), False, 'import math\n'), (86, 'math.cos', 'math.cos', (['steer'], {}), False, 'import math\n'), (82, 'math.sin', 'math.sin', (['yaw'], {}), False, 'import math\n'), (86, 'math.sin', 'math.sin', (['steer'], {}), False, 'import math\n')]