from gk_coord import *
import gk_transformable
import gk_transforms
import gk_shape_info

class gds_vector(gk_transformable.gds_transformable):
    """ positioned vector, consisting of a point and an angle """
    def __init__(self, position, angle):
        self.position = position
        self.__angle__ = angle

    def move(self, coordinate):
        self.position = coord2(self.position[0] + coordinate[0], self.position[1] + coordinate[1])
        return self

    def move_copy(self, coordinate):
        return gds_vector((self.position[0] + coordinate[0], self.position[1] + coordinate[1]), self.__angle__)

    def transform(self, transformation):
        self.position = transformation.apply_to_coord(self.position)
        self.__angle__ = transformation.apply_to_angle_deg(self.__angle__)
        return self

    def transform_copy(self, transformation):
        P = gds_vector(self.position, self.__angle__)
        return P.transform(transformation)

    def set_position(self, value):
        if isinstance(value, coord2):
            self.__position__ = value
        elif isinstance(value, tuple):
            self.__position__ = coord2(value[0], value[1])
        else:
            raise TypeError("Wrong type of coordinate for gds_vector: ", str(type(value)))
    def get_position(self):
        return self.__position__
    position = property(get_position, set_position)

    def get_x(self):
        return self.position.x
    x = property(get_x)
    
    def get_y(self):
        return self.position.y
    y = property(get_y)

    def get_angle_rad(self):
        return DEG2RAD * self.__angle__
    def set_angle_rad(self, value):
        self.__angle__ = (RAD2DEG * value)%360.0
    angle_rad = property(get_angle_rad, set_angle_rad)
    
    def get_angle_deg(self):
        return self.__angle__
    def set_angle_deg(self, value):
        self.__angle__ = value % 360.0
    angle_deg = property(get_angle_deg, set_angle_deg)
    
    def cos(self):
        return cos(DEG2RAD*self.__angle__)
    def sin(self):
        return sin(DEG2RAD*self.__angle__)
    def tan(self):
        return tan(DEG2RAD*self.__angle__)
    
    def flip(self):
        #gives port in other direction
        return gds_vector(self.position, (self.__angle__ + 180.0 ) % 360.0)

    def __getitem__(self, key):
        # behave like a coordinate for e.g. size_info
        if key == 0:
            return self.position[0]
        if key == 1:
            return self.position[1]
        else:
            raise IndexError("gds_vector supports only subscription[0] and [1], not " + str(key))


def transformation_from_vector(vector):
    return gk_transforms.gds_rotation((0.0, 0.0), vector.angle_deg) + gk_transforms.gds_translation(vector.position)

def vector_from_two_points(point1, point2):
    return gds_vector(point1, gk_shape_info.angle_deg(point2, point1))