# -*- coding: utf-8 -*-
"""图 7-4　详细设计审查流程图示例 —— 教材第 153 页

按「图内要素」逐项还原（12 个节点、两行排布、矩形为处理步骤、菱形为判断）：
上排自左至右 1 项目需求 → 2 需求确认 → 3 开发原图 → 4 原图审批通过否（菱形）；
4 的「否」回到 3 底边，「是」向下再向左进入第二行右端的 5；
第二行自右至左 5 变更控制 → 6 详细设计 → 7 卖方确认；
7 左侧引线向左折出、沿左侧向下、再向右进入 8 设计包开发审查/批准否（菱形）左顶点；
8 顶端「否」竖直回到 7 底边；8 的「是」向右进入 9 QA审查/批准否（菱形）；
9 顶端「否」向上再向左汇入 8→7 的同一条竖直回线；
9 的「是」向右进入 10 通过审批反馈给卖方 → 11 签发详细设计 → 12 下订单。
"""
import os
import sys

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _draw import setup, canvas, box, arrow, line, save, INK, NAV, ACC, FILL, LINE

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'assets',
                   '图7-4-详细设计审查流程图示例.png')

BW, BH = 14.0, 10.0          # 矩形节点宽高
DHW, DHH = 8.5, 7.5          # 菱形半宽／半高


def rect(ax, x, ytop, num, name, w=BW, h=BH, fs=10.5):
    """处理步骤节点：第一行序号、第二行名称。"""
    box(ax, x, ytop, w, h, f'{num}\n{name}', fs=fs, radius=1.0, lw=1.3)
    return (x + w / 2, ytop - h / 2)


def dia(ax, cx, cy, num, name, hw=DHW, hh=DHH, fs=9.8):
    """判断节点（菱形）：第一行序号、第二行名称（名称内用 \\n 手动换行）。"""
    p = Polygon([(cx, cy + hh), (cx + hw, cy), (cx, cy - hh), (cx - hw, cy)],
                closed=True, facecolor=FILL, edgecolor=LINE, linewidth=1.3, zorder=2)
    ax.add_patch(p)
    ax.text(cx, cy, f'{num}\n{name}', ha='center', va='center', color=NAV,
            fontsize=fs, linespacing=1.4, zorder=3)


def poly_arrow(ax, pts, label=None, lpos=None, ls='-'):
    """折线连线，最后一段带箭头。"""
    for i in range(len(pts) - 2):
        line(ax, pts[i], pts[i + 1], color=ACC, lw=1.3, ls=ls)
    arrow(ax, pts[-2], pts[-1], color=ACC, lw=1.4)
    if label:
        ax.text(lpos[0], lpos[1], label, ha='center', va='center',
                fontsize=10.5, color=INK)


def main():
    setup()
    fig, ax = canvas(12.0, 8.2)

    # ── 第一行：1 → 2 → 3 → 4（菱形）──
    c1 = rect(ax, 6.0, 86.0, 1, '项目需求')
    c2 = rect(ax, 25.0, 86.0, 2, '需求确认')
    c3 = rect(ax, 44.0, 86.0, 3, '开发原图')
    d4 = (80.0, 68.0)
    dia(ax, d4[0], d4[1], 4, '原图审批通\n过否')

    arrow(ax, (20.0, 81.0), (25.0, 81.0))
    arrow(ax, (39.0, 81.0), (44.0, 81.0))
    # 3 → 4：向右再向下，进入菱形上顶点
    poly_arrow(ax, [(58.0, 81.0), (80.0, 81.0), (80.0, 75.5)])

    # 4「否」：左顶点向左，再向上回到 3 的底边
    poly_arrow(ax, [(71.5, 68.0), (51.0, 68.0), (51.0, 76.0)],
               '否', (60.5, 69.4))
    # 4「是」：下顶点向下，再向左进入 5 的右边（与 5 的中线齐平）
    poly_arrow(ax, [(80.0, 60.5), (80.0, 51.0), (65.0, 51.0)],
               '是', (72.0, 52.6))

    # ── 第二行（自右至左）：5 → 6 → 7 ──
    c5 = rect(ax, 51.0, 56.0, 5, '变更控制')
    c6 = rect(ax, 30.0, 56.0, 6, '详细设计')
    c7 = rect(ax, 9.0, 56.0, 7, '卖方确认')

    arrow(ax, (51.0, 51.0), (44.0, 51.0))
    arrow(ax, (30.0, 51.0), (23.0, 51.0))

    # 7 左侧 → 沿左侧下行 → 8 左顶点
    poly_arrow(ax, [(9.0, 51.0), (4.0, 51.0), (4.0, 34.0), (10.5, 34.0)])

    # ── 判断节点 8、9 ──
    d8 = (19.0, 34.0)
    d9 = (48.0, 34.0)
    dia(ax, d8[0], d8[1], 8, '设计包开发\n审查/批准否')
    dia(ax, d9[0], d9[1], 9, 'QA审查/批\n准否')

    # 8 顶端「否」竖直回到 7 底边
    poly_arrow(ax, [(19.0, 41.5), (19.0, 46.0)], '否', (16.4, 43.9))
    # 8「是」→ 9
    poly_arrow(ax, [(27.5, 34.0), (39.5, 34.0)], '是', (33.5, 36.0))
    # 9 顶端「否」向上再向左，汇入 8→7 的回线（标注置于该横线下方，避开 6 号框底边）
    poly_arrow(ax, [(48.0, 41.5), (48.0, 44.0), (19.0, 44.0)], '否', (33.5, 43.2))
    # 9「是」→ 10
    poly_arrow(ax, [(56.5, 34.0), (70.0, 34.0)], '是', (62.5, 36.0))

    # ── 10 → 11 → 12 ──
    rect(ax, 70.0, 39.0, 10, '通过审批反馈\n给卖方', w=15.0, h=10.0, fs=9.8)
    rect(ax, 70.0, 19.0, 11, '签发详细设计', w=15.0, h=10.0)
    rect(ax, 47.0, 19.0, 12, '下订单', w=15.0, h=10.0)

    arrow(ax, (77.5, 29.0), (77.5, 19.0))
    arrow(ax, (70.0, 14.0), (62.0, 14.0))

    save(fig, OUT)


if __name__ == '__main__':
    main()
