import math
import time

import ezdxf

import core
from drawer import WindFlowDrawer


class WindBridgeDrawer:
    def __init__(self, msp, center, tun1_id, tun2_id, layer_id, route, gap, color=0, is_cross=True):
        self.msp = msp
        self.gap = gap
        self.route = route
        self.is_cross = is_cross
        self.color = color
        self.layer_id = layer_id
        self.tun2_id = tun2_id
        self.tun1_id = tun1_id
        self.center = center

    def draw_wind_bridge_drawer(self):
        width = self.gap * 1.7

        line_top_middle = self.center[0], self.center[1] + width / 2
        line_top_left = line_top_middle[0] - width * 1.25 / 2, line_top_middle[1]
        line_top_left_2 = line_top_left[0] - math.cos(math.pi / 6) * width * 0.625, line_top_left[1] + math.sin(
            math.pi / 6) * width * 0.625
        line_top_right = core.symmetric_point(line_top_left, line_top_middle)
        line_top_right_2 = core.symmetric_point(line_top_left_2, (line_top_middle[0], line_top_middle[1] + math.sin(
            math.pi / 6) * width * 0.625))

        line_bottom_middle = self.center[0], self.center[1] - width / 2

        line_bottom_left = line_bottom_middle[0] - width * 1.25 / 2, line_bottom_middle[1]

        line_bottom_left_2 = line_bottom_left[0] - math.cos(math.pi / 6) * width * 0.625, line_bottom_left[
            1] - math.sin(
            math.pi / 6) * width * 0.625

        line_bottom_right = core.symmetric_point(line_bottom_left, line_bottom_middle)

        line_bottom_right_2 = core.symmetric_point(line_bottom_left_2,
                                                   (line_bottom_middle[0], line_bottom_middle[1] - math.sin(
                                                       math.pi / 6) * width * 0.625))

        line_top_left = core.rotate_point_around_another(line_top_left, self.center, self.route)
        line_top_left_2 = core.rotate_point_around_another(line_top_left_2, self.center, self.route)
        line_top_right = core.rotate_point_around_another(line_top_right, self.center, self.route)
        line_top_right_2 = core.rotate_point_around_another(line_top_right_2, self.center, self.route)
        line_bottom_left = core.rotate_point_around_another(line_bottom_left, self.center, self.route)
        line_bottom_left_2 = core.rotate_point_around_another(line_bottom_left_2, self.center, self.route)
        line_bottom_right = core.rotate_point_around_another(line_bottom_right, self.center, self.route)
        line_bottom_right_2 = core.rotate_point_around_another(line_bottom_right_2, self.center, self.route)

        self.msp.add_line(line_top_left, line_top_left_2)
        self.msp.add_line(line_top_right, line_top_left)
        self.msp.add_line(line_top_right, line_top_right_2)
        self.msp.add_line(line_bottom_left, line_bottom_right)
        self.msp.add_line(line_bottom_right, line_bottom_right_2)
        self.msp.add_line(line_bottom_left, line_bottom_left_2)


if __name__ == '__main__':
    doc = ezdxf.new()
    msp = doc.modelspace()
    wbd = WindBridgeDrawer(msp, (0, 0), 100, 10, 0, 0, 100)
    wbd.draw_wind_bridge_drawer()
    file_name = f'shaft{str(time.time())}.dxf'
    print("保存文件 " + file_name)
    doc.saveas(file_name)
