# Path: lasercut/helper.py
# class ObjectProperties(object):
#     def __init__(self, **kwargs):
#         self.obj_class = str(type(self).__name__)
#         for k, v in kwargs.items():
#             if not hasattr(self, "_allowed") or str(k) in self._allowed:
#                 setattr(self, k, v)
#             #else:
#             #    FreeCAD.Console.PrintWarning(str(k) + " is not allowed for " + str(type(self)))
# 
# def sort_quad_vertex(list_edges, reverse):
#     list_points = []
#     if not reverse:
#         list_points = [list_edges[0].Vertexes[0].Point, list_edges[0].Vertexes[1].Point]
#     else:
#         list_points = [list_edges[0].Vertexes[1].Point, list_edges[0].Vertexes[0].Point]
#     for edge in list_edges[1:-1]:
#         vertex1 = edge.Vertexes[0].Point
#         vertex2 = edge.Vertexes[1].Point
# 
#         if compare_freecad_vector(vertex1, list_points[-1]):
#             list_points.append(vertex2)
#         elif compare_freecad_vector(vertex2, list_points[-1]):
#             list_points.append(vertex1)
#         else:
#             return None
# 
#     return list_points
# 
# def biggest_area_faces(freecad_shape):
#     sorted_list = sort_area_shape_faces(freecad_shape)
#     biggest_area_face = sorted_list[-1]
# #       contains : 0:normal, 1:area mm2, 2; list of faces
#     return biggest_area_face
# 
# def sort_area_shape_list(faces_list):
#     return sort_area_face_common(faces_list, compare_freecad_vector)
# 
# def compare_value(value1, value2, epsilon=10e-6):
#     value = value1 - value2
#     if math.fabs(value) < epsilon:
#         return True
#     return False


import FreeCAD
import Part
import collections
from lasercut.helper import ObjectProperties, sort_quad_vertex, biggest_area_faces, sort_area_shape_list, compare_value
    # order faces by normals
    sub_areas_face = sort_area_shape_list(area_faces[2])
    # TODO : check if normals at opposite
    #list_edges_face1 = Part.__sortEdges__(area_faces[2][0].Edges)
    #list_edges_face2 = Part.__sortEdges__(area_faces[2][1].Edges)
    list_edges_face1 = Part.__sortEdges__(sub_areas_face[0][2][0].Edges)
    list_edges_face2 = Part.__sortEdges__(sub_areas_face[1][2][0].Edges)

    list_pts_face1 = sort_quad_vertex(list_edges_face1, False)
    if list_pts_face1 is None:
        list_pts_face1 = sort_quad_vertex(list_edges_face1, True)
    if list_pts_face1 is None:
        raise ValueError("Error sorting vertex")

    list_pts_face2 = sort_quad_vertex(list_edges_face2, False)
    if list_pts_face2 is None:
        list_pts_face2 = sort_quad_vertex(list_edges_face2, True)
    if list_pts_face2 is None:
        raise ValueError("Error sorting vertex")

    min_array = []
    for vec1 in list_pts_face1:
        tab_diff = []
        for vec2 in list_pts_face2:
            tab_diff.append(vec1.sub(vec2).Length)
        min_array.append(min(tab_diff))
    # print "min_array " + str(min_array)
    counter_list = collections.Counter(min_array)
    thickness_occ = counter_list.most_common(1)


# Path: lasercut/helper.py
# class ObjectProperties(object):
#     def __init__(self, **kwargs):
#         self.obj_class = str(type(self).__name__)
#         for k, v in kwargs.items():
#             if not hasattr(self, "_allowed") or str(k) in self._allowed:
#                 setattr(self, k, v)
#             #else:
#             #    FreeCAD.Console.PrintWarning(str(k) + " is not allowed for " + str(type(self)))
# 
# def sort_quad_vertex(list_edges, reverse):
#     list_points = []
#     if not reverse:
#         list_points = [list_edges[0].Vertexes[0].Point, list_edges[0].Vertexes[1].Point]
#     else:
#         list_points = [list_edges[0].Vertexes[1].Point, list_edges[0].Vertexes[0].Point]
#     for edge in list_edges[1:-1]:
#         vertex1 = edge.Vertexes[0].Point
#         vertex2 = edge.Vertexes[1].Point
# 
#         if compare_freecad_vector(vertex1, list_points[-1]):
#             list_points.append(vertex2)
#         elif compare_freecad_vector(vertex2, list_points[-1]):
#             list_points.append(vertex1)
#         else:
#             return None
# 
#     return list_points
# 
# def biggest_area_faces(freecad_shape):
#     sorted_list = sort_area_shape_faces(freecad_shape)
#     biggest_area_face = sorted_list[-1]
# #       contains : 0:normal, 1:area mm2, 2; list of faces
#     return biggest_area_face
# 
# def sort_area_shape_list(faces_list):
#     return sort_area_face_common(faces_list, compare_freecad_vector)
# 
# def compare_value(value1, value2, epsilon=10e-6):
#     value = value1 - value2
#     if math.fabs(value) < epsilon:
#         return True
#     return False


# Path: lasercut/material.py
import FreeCAD
import Part
import collections
from lasercut.helper import ObjectProperties, sort_quad_vertex, biggest_area_faces, sort_area_shape_list, compare_value

    # order faces by normals
    sub_areas_face = sort_area_shape_list(area_faces[2])
    # TODO : check if normals at opposite
    #list_edges_face1 = Part.__sortEdges__(area_faces[2][0].Edges)
    #list_edges_face2 = Part.__sortEdges__(area_faces[2][1].Edges)
    list_edges_face1 = Part.__sortEdges__(sub_areas_face[0][2][0].Edges)
    list_edges_face2 = Part.__sortEdges__(sub_areas_face[1][2][0].Edges)

    list_pts_face1 = sort_quad_vertex(list_edges_face1, False)
    if list_pts_face1 is None:
        list_pts_face1 = sort_quad_vertex(list_edges_face1, True)
    if list_pts_face1 is None:
        raise ValueError("Error sorting vertex")

    list_pts_face2 = sort_quad_vertex(list_edges_face2, False)
    if list_pts_face2 is None:
        list_pts_face2 = sort_quad_vertex(list_edges_face2, True)
    if list_pts_face2 is None:
        raise ValueError("Error sorting vertex")

    min_array = []
    for vec1 in list_pts_face1:
        tab_diff = []
        for vec2 in list_pts_face2:
            tab_diff.append(vec1.sub(vec2).Length)
        min_array.append(min(tab_diff))
    # print "min_array " + str(min_array)
    counter_list = collections.Counter(min_array)
    thickness_occ = counter_list.most_common(1)


    return thickness_occ[0][0]