import math
import time
from core import rotate_point_around_another
from ezdxf import math as mt

import ezdxf


class ShaftDrawer:
    def __init__(self, msp, gap, center, route, color):
        self.msp = msp
        self.gap = gap
        self.center = center
        self.route = route
        self.color = color

    def calculate_dodecagon_points(self):
        # 存储结果的列表
        points = []
        points_rotated = []
        Cx, Cy = self.center
        r = 1 / 2 * self.gap
        # 正十二边形有12个顶点，每个顶点间隔30度
        for i in range(12):
            # 将角度从度转换为弧度
            theta = math.radians(30 * i)

            # 计算x和y坐标
            x = Cx + r * math.cos(theta)
            y = Cy + r * math.sin(theta)

            # 将坐标添加到列表中
            points.append((x, y))
        return points

    def calculate_outside_octagon_points(self):
        # 存储结果的列表
        points = []
        Cx, Cy = self.center
        r = 1 / 4 * self.gap
        # 正八边形有8个顶点，每个顶点间隔45度
        for i in range(8):
            # 将角度从度转换为弧度
            theta = math.radians(45 * i)

            # 计算x和y坐标
            x = Cx + r * math.cos(theta)
            y = Cy + r * math.sin(theta)
            x_y_ = rotate_point_around_another((x, y), self.center, math.radians(12.5))
            # 将坐标添加到列表中
            points.append(x_y_)

        return points

    def calculate_inner_octagon_points(self):
        # 存储结果的列表
        points = []
        Cx, Cy = self.center
        r = 1 / 8 * self.gap
        # 正八边形有8个顶点，每个顶点间隔45度
        for i in range(8):
            # 将角度从度转换为弧度
            theta = math.radians(45 * i)

            # 计算x和y坐标
            x = Cx + r * math.cos(theta)
            y = Cy + r * math.sin(theta)
            # 将坐标添加到列表中
            points.append((x, y))

        return points

    def draw_polygon(self, points):
        i = 0
        while i < len(points) - 1:
            self.msp.add_line(points[i], points[i + 1], {"color": self.color})
            i = i + 1
        self.msp.add_line(points[-1], points[0], {
            "color": self.color,
        })

    def draw_shaft_drawer(self):
        points12 = self.calculate_dodecagon_points()
        self.draw_polygon(points12)

        points8_out = self.calculate_outside_octagon_points()
        self.draw_polygon(points8_out)

        points8_in = self.calculate_inner_octagon_points()
        self.draw_polygon(points8_in)
        # for i,points8 in enumerate(points8_in):
        #     msp.add_text(text=str(i),dxfattribs={'insert':points8})

        carc = mt.arc.ConstructionArc()
        b = carc.from_3p(start_point=points8_in[1], def_point=points8_in[3], end_point=points8_in[5])
        self.msp.add_arc(center=b.center, radius=b.radius, start_angle=b.start_angle, end_angle=b.end_angle)
        hatch = self.msp.add_hatch(color=self.color)
        edge_path = hatch.paths.add_edge_path()
        edge_path.add_arc(center=b.center, radius=b.radius, start_angle=b.start_angle, end_angle=b.end_angle)


if __name__ == '__main__':
    doc = ezdxf.new()
    msp = doc.modelspace()
    sd = ShaftDrawer(msp, 1000, (10, 10), 10)
    sd.draw_shaft_drawer()
    doc.saveas(f'shaft{str(time.time())}.dxf')
