#import bpy
from random import seed
from random import uniform
import numpy as np
import cv2
# seed random number generator
seed(1)
"""
def test1():

 
    # make mesh
    vertices = [(1, 0, 0),(1,0,5),(0,1,0)]
    edges = []
    faces = []
    faces.append([0,1,2])
    faces.append([2,0,3])

    #new_mesh = bpy.data.meshes.new('new_mesh')
    #new_mesh.from_pydata(vertices, edges, faces)
    #knew_mesh.update()
    # make object from mesh
    #new_object = bpy.data.objects.new('new_object', new_mesh)
    # make collection
    #new_collection = bpy.data.collections.new('new_collection')
    #bpy.context.scene.collection.children.link(new_collection)
    # add object to scene collection
    #new_collection.objects.link(new_object)


def add_mesh(name, verts, faces, edges=None, col_name="Collection"):    
    if edges is None:
        edges = []
    mesh = bpy.data.meshes.new(name)
    obj = bpy.data.objects.new(mesh.name, mesh)
    col = bpy.data.collections.get(col_name)
    col.objects.link(obj)
    bpy.context.view_layer.objects.active = obj
    mesh.from_pydata(verts, edges, faces)
"""
def get_translation_between_points(pt1,pt2):
    transl = (pt2[0] - pt1[0], pt2[1] - pt1[1])
    return transl

class Geometry2d:
    def __init__(self, points=[]):
        self.points = points

    def reverse(self):
        self.points.reverse()

    def get_last_point(self):
        return self.points[-1]

    def get_first_point(self):
        return self.points[0]

    def remove_first_element(self):
        self.points.pop(0)

    def remove_last_element(self):
        self.points.pop()

    def translate_points(self, transl):
        points = self.points
        for i,p in enumerate(points):
            p = (p[0]+transl[0], p[1]+transl[1])
            points[i] = p

        return Geometry2d(points)

    def scale_points(self, scale):
        points = self.points
        for i,p in enumerate(points):
            p = (p[0]*scale, p[1]*scale)
            points[i] = p
        return Geometry2d(points)

    def flip_y(self):
        points = self.points
        for i,p in enumerate(self.points):
            points[i] = (p[0],-p[1])
        return Geometry2d(points)

    def draw(self):
        width = 300
        height = 300
        img = np.zeros((300,300))
        scale_obj = self.scale_points(30)
        draw_obj = scale_obj.flip_y()
        draw_obj = draw_obj.translate_points((int(width/2), int(height/2)))
        for point in draw_obj.points:
            print(point)
            point = (int(point[0]), int(point[1]))
            cv2.circle(img, point, 2, (255,0,0), 2)
        cv2.imshow("img", img)
        cv2.waitKey(0)


    def combine_at_first(self, geo2d_obj):
        new_geo2d_obj = Geometry2d()
        first_pt_obj1 = self.get_first_point()
        print("first_pt")
        print(first_pt_obj1)
        first_pt_obj2 = geo2d_obj.get_first_point()
        print("last pt")
        print(first_pt_obj2)
        transl = get_translation_between_points(first_pt_obj2, first_pt_obj1)

        print("transl")
        print(transl)
        transl_obj = geo2d_obj.translate_points(transl)
        print("translated points")
        print(transl_obj.points)
        transl_obj.points.pop(0)
        transl_obj.points = self.points + transl_obj.points
        return transl_obj

    def combine_at_last(self, geo2d_obj):
        first_pt_obj1 = self.get_last_point()
        print("first_pt")
        print(first_pt_obj1)
        first_pt_obj2 = geo2d_obj.get_first_point()
        print("last pt")
        print(first_pt_obj2)
        transl = get_translation_between_points(first_pt_obj2, first_pt_obj1)

        print("transl")
        print(transl)
        transl_obj = geo2d_obj.translate_points(transl)
        print("translated points")
        print(transl_obj.points)
        transl_obj.points.pop(0)
        transl_obj.points = self.points + transl_obj.points
        return transl_obj







        



        
        

"""

def generate_2d_corner(height, width, corner_point):
    p1 = (corner_point[0] + width, corner_point[1])
    p2 = corner_point
    p3 = (corner_point[0], corner_point[1]+height)


    
    
    
    

    
    verts = []
    # create bottom vertices
    for i in range(num_points):
        i = i/num_points*x_stop
        x = i+uniform(-step,step)
        y = -width/2
        z = uniform(0,z_limit)
        point = (x,y,z)
        verts.append(point)
        y = width/2
        point = (x,y,z)
        verts.append(point)
    
    faces = []
    
    for point in verts:
        print(point)
        
    # create faces
    num_points = len(verts)
    print(f'num points: {num_points}')
    for i in range(0,num_points-2, 2):
        faces.append([i+1, i, i+2, i+3])
    
    print("Faces:")
    print(faces)
    
    for face in faces:
        print(face)

    add_mesh("testsets", verts, faces)

    #verts = [( 1.0,  1.0,  0.0), 
     #    ( 1.0, -1.0,  0.0),
      #   (-1.0, -1.0,  0.0),
        # (-1.0,  1.0,  0.0),
         #]
    #faces = [[0, 1, 2, 3]]
    #add_mesh("myBeautifulMesh_1", verts, faces)

    #verts = [( 3.0,  1.0,  0.0), 
     #    ( 3.0, -1.0,  0.0),
      #   ( 2.0, -1.0,  0.0),
       #  ( 2.0,  1.0,  0.0),
        # ]
    #add_mesh("myBeautifulMesh_2", verts, faces)
"""


if __name__ == '__main__':
    geo2d_obj = Geometry2d([(1,0), (0,0),(0,1),(0,2), (0,3)])
    geo2d_obj1 = Geometry2d([(0,0), (1,0), (1,2), (2,1.5),(3,2), (3,0), (4,0)])
    geo2d_obj1 = geo2d_obj1.translate_points((4,4))
    geo_comb = geo2d_obj.combine_at_last(geo2d_obj1)
    geo_comb.draw()
     
    
    

