## TODO Maybe switch to complex numbers ?

from math import radians, degrees, cos, sin
import gui

class Point:
    def __init__(self,x,y, o = None):
        self.x = x
        self.y = y
        if not o:
            self.o = Point(x,y,True)

class Line:
    def __init__(self,p,q,mid, width = None, colour = None):
        self.p = p
        self.q = q
        self.mid = mid
        if colour:
            self.colour = [colour]
        else:
            self.colour = [gui.DL_NOP()]
        if width:
            self.width = [gui.DL_LINE_WIDTH(width)]
        else:
            self.width = [gui.DL_NOP()]
        self.gui_l = [self.colour,self.width,[gui.DL_VERTEX2F(self.p.x + self.mid.x, self.p.y + self.mid.y)],[gui.DL_VERTEX2F(self.q.x + self.mid.x, self.q.y + self.mid.y)]]

    def redraw(self):
        self.gui_l[2][0] = gui.DL_VERTEX2F(self.p.x + self.mid.x, self.p.y + self.mid.y)
        self.gui_l[3][0] = gui.DL_VERTEX2F(self.q.x + self.mid.x, self.q.y + self.mid.y)

    def set_colour(self,colour):
        self.colour[0] = colour

    def set_width(self,width):
        self.width[0] = [gui.DL_LINE_WIDTH(width)]


class Circle:
    pass
    #TODO

class Rectangle:
    pass

class Object:
    def __init__(self,name,midpoint):
        self.name = name
        self.mid = midpoint
        self.angle = 0
        self.lines = []
        self.points = []
        self.gui_l = [gui.SUBLIST,
            [gui.DL_BEGIN(gui.PRIM_LINES)],
            [gui.DL_END()]
        ]

        # Maybe get rid off
        self.following = []
        self.following_x = 0
        self.following_y = 0
        self.following_deg = 0
        self.following_rad = 0

    # TODO
    def add_relative(self,points):
        pass


    def follow_angle(self,angle, measurment):
        self.angle_follow

    def folow_axis(self, x = None, y = None):
        if x:
            self.following.append(self.trans_x)
        if y:
            self.following.append(self.trans_y)

    def trans_x(self):
        pass

    def trans_y(self):
        pass

    def rot_d(self):
        pass

    def rot_r(self):
        pass

    def add_line(self,points, width = None, colour = None):
        for idx, point in enumerate(points):
            if idx != len(points) -1:
                self.lines.append(Line(point, points[idx+1], self.mid, width, colour))
                # self.gui_l.append(self.lines[-1].gui_l[0])
                # self.gui_l.append(self.lines[-1].gui_l[1])
                self.gui_l.insert(len(self.gui_l)-1 , self.lines[-1].gui_l[0])
                self.gui_l.insert(len(self.gui_l)-1 , self.lines[-1].gui_l[1])
                self.gui_l.insert(len(self.gui_l)-1 , self.lines[-1].gui_l[2])
                self.gui_l.insert(len(self.gui_l)-1 , self.lines[-1].gui_l[3])

    def redraw(self):
        for line in self.lines:
            line.redraw()

    def vsync(self,list):
        # for func in self.following:
        #     func()
        self.redraw()

        # if self.angle:
        #     set_angle()


    def translate(self,x,y):
        self.mid.x += x
        self.mid.y += y
        # for point in self.points:
        #     point.x += x
        #     point.y += y


    def set_angle(self, Radians = None, Degrees = None):
        if Radians:
            self.angle = Radians
        elif Degrees:
            self.angle = radians(Degrees)

        for point in self.points:
            point.x = ( cos(self.angle) * (point.o.x)) + (sin(self.angle) * (point.o.y))
            point.y = (-sin(self.angle) * (point.o.x)) + (cos(self.angle) * (point.o.y))
            #line.q.x = ( cos(self.angle) * (line.q.x-self.mid.x)) + (sin(self.angle) * (line.q.y-self.mid.y)) + self.mid.x
            #line.p.y = (-sin(self.angle) * (line.q.x-self.mid.x)) + (cos(self.angle) * (line.q.y-self.mid.y)) + self.mid.y

    def rotate(self, Radians = None, Degrees = None):
        if Radians:
            self.angle += Radians
        elif Degrees:
            self.angle += Radians(Degrees)

        for point in self.points:
            point.x = ( cos(self.angle) * (point.o.x)) + (sin(self.angle) * (point.o.y))
            point.y = (-sin(self.angle) * (point.o.x)) + (cos(self.angle) * (point.o.y))
