from kikit.plugin import ToolingPlugin
import pcbnew

class CustomTooling(ToolingPlugin):
    def buildTooling(self, panel):
        board = panel.board

        # panelBBox() -> (xmin, ymin, xmax, ymax) 
        xmin, ymin, xmax, ymax = panel.panelBBox()

        min_x = pcbnew.ToMM(xmin)
        min_y = pcbnew.ToMM(ymin)
        max_x = pcbnew.ToMM(xmax)
        max_y = pcbnew.ToMM(ymax)

        margin_x_mm = 10
        margin_y_mm = 2.5

        center_x = (min_x + max_x) / 2
        center_y = (min_y + max_y) / 2

        holes = [
            (min_x + margin_x_mm, min_y + margin_y_mm),         # top left
            (center_x, min_y + margin_y_mm),                    # top center
            (max_x - margin_x_mm, min_y + margin_y_mm),         # top right

            (min_x + margin_x_mm, max_y - margin_y_mm),      # bottom left
            (center_x, max_y - margin_y_mm),                 # bottom center
            (max_x - margin_x_mm, max_y - margin_y_mm),      # bottom right
        ]

        hole_d = pcbnew.FromMM(3.172)

        for x_mm, y_mm in holes:
            fp = pcbnew.FOOTPRINT(board)
            fp.SetReference("")

            pos = pcbnew.VECTOR2I(
                pcbnew.FromMM(x_mm),
                pcbnew.FromMM(y_mm)
            )

            fp.SetPosition(pos)

            pad = pcbnew.PAD(fp)
            pad.SetShape(pcbnew.PAD_SHAPE_CIRCLE)
            pad.SetAttribute(pcbnew.PAD_ATTRIB_NPTH)

            pad.SetSize(pcbnew.VECTOR2I(hole_d, hole_d))
            pad.SetDrillSize(pcbnew.VECTOR2I(hole_d, hole_d))
            pad.SetPosition(pos)

            fp.Add(pad)
            board.Add(fp)
        
        # =========================
        # SCREEN RECTANGLE
        # =========================

        screen_w = 153.4
        screen_h = 87.0

        # Panel center
        center_x = (min_x + max_x) / 2
        center_y = (min_y + max_y) / 2

        # Screen rectangle corners
        screen_x0 = center_x - (screen_w / 2)
        screen_y0 = center_y - (screen_h / 2)

        screen_x1 = center_x + (screen_w / 2)
        screen_y1 = center_y + (screen_h / 2)

        screen = pcbnew.PCB_SHAPE(board)
        screen.SetShape(pcbnew.SHAPE_T_RECT)
        screen.SetLayer(pcbnew.Dwgs_User)

        screen.SetStart(
            pcbnew.VECTOR2I(
                pcbnew.FromMM(screen_x0),
                pcbnew.FromMM(screen_y0)
            )
        )

        screen.SetEnd(
            pcbnew.VECTOR2I(
                pcbnew.FromMM(screen_x1),
                pcbnew.FromMM(screen_y1)
            )
        )

        screen.SetWidth(pcbnew.FromMM(0.2))

        board.Add(screen)

        # =========================
        # FIXTURE RECTANGLE
        # =========================

        fixture_w = 200.0
        fixture_h = 130.0

        # Fixture rectangle corners
        fixture_x0 = center_x - (fixture_w / 2)
        fixture_y0 = center_y - (fixture_h / 2)

        fixture_x1 = center_x + (fixture_w / 2)
        fixture_y1 = center_y + (fixture_h / 2)

        fixture = pcbnew.PCB_SHAPE(board)
        fixture.SetShape(pcbnew.SHAPE_T_RECT)
        fixture.SetLayer(pcbnew.Dwgs_User)

        fixture.SetStart(
            pcbnew.VECTOR2I(
                pcbnew.FromMM(fixture_x0),
                pcbnew.FromMM(fixture_y0)
            )
        )

        fixture.SetEnd(
            pcbnew.VECTOR2I(
                pcbnew.FromMM(fixture_x1),
                pcbnew.FromMM(fixture_y1)
            )
        )

        fixture.SetWidth(pcbnew.FromMM(0.2))

        board.Add(fixture)

