# -*- coding: utf-8 -*-
"""图 9-6　促进干系人有效参与 —— 教材第 224 页

逐项对照图内要素：
· 6 个矩形文本框沿椭圆轨迹围成一圈，带箭头细线首尾相接，构成顺时针闭环；
· 位置与框内文字：正上方「识别」；右上「理解」；右下「分析」；
  正下方「优先级排序」（框较扁长）；左下「参与」；左上「监督」；
· 连线与箭头方向（顺时针）：
  ① 识别右端 →（弧线）→ 理解上端；② 理解底端 →（竖直线）→ 分析顶端；
  ③ 分析底端 →（弧线）→ 优先级排序右端；④ 优先级排序左端 →（弧线）→ 参与底端；
  ⑤ 参与顶端 →（竖直线）→ 监督底端；⑥ 监督顶端 →（弧线）→ 识别左端，闭合；
· 六条连线均带编号 ①–⑥；图下方居中为图题。
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _draw import setup, canvas, save, caption, arrow, box, NAV, ACC, FILL  # noqa: E402

import matplotlib.pyplot as plt                          # noqa: E402
from matplotlib.patches import Circle                    # noqa: E402

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'assets',
                   '图9-6-促进干系人有效参与.png')


def badge(ax, x, y, n, r=2.3):
    """编号圆牌（①–⑥）：白底蓝边＋数字。"""
    ax.add_patch(Circle((x, y), r, facecolor='white', edgecolor=ACC,
                        linewidth=1.3, zorder=6))
    ax.text(x, y - 0.05, str(n), fontsize=8.2, ha='center', va='center',
            color=ACC, fontweight='bold', zorder=7)


def main():
    setup()
    fig, ax = canvas(10.0, 8.4, xlim=(0, 100), ylim=(0, 84))

    # ════ 六个框（顺时针：上→右上→右下→下→左下→左上）════
    C = {}
    C['识别'] = box(ax, 40, 78.5, 20, 9, '识别', fs=12)
    C['理解'] = box(ax, 71.5, 60.5, 19, 9, '理解', fs=12)
    C['分析'] = box(ax, 71.5, 34.5, 19, 9, '分析', fs=12)
    C['优先级排序'] = box(ax, 36.5, 16.5, 27, 9, '优先级排序', fs=12)
    C['参与'] = box(ax, 9.5, 34.5, 19, 9, '参与', fs=12)
    C['监督'] = box(ax, 9.5, 60.5, 19, 9, '监督', fs=12)

    # ════ 六条带箭头的连线（顺时针闭环）════
    # ① 识别右端 → 理解上端（弧线）
    arrow(ax, (60, 74), (77.0, 61.0), lw=1.5, rad=0.20)
    # ② 理解底端 → 分析顶端（竖直线段）
    arrow(ax, (81, 51.5), (81, 34.5), lw=1.5)
    # ③ 分析底端 → 优先级排序右端（弧线）
    arrow(ax, (81, 25.5), (63.5, 12), lw=1.5, rad=0.22)
    # ④ 优先级排序左端 → 参与底端（弧线）
    arrow(ax, (36.5, 12), (19, 25.5), lw=1.5, rad=0.22)
    # ⑤ 参与顶端 → 监督底端（竖直线段）
    arrow(ax, (19, 34.5), (19, 51.5), lw=1.5)
    # ⑥ 监督顶端 → 识别左端（弧线）
    arrow(ax, (19, 60.5), (40, 74), lw=1.5, rad=0.22)

    # ════ 编号 ════
    badge(ax, 65.0, 71.6, 1)
    badge(ax, 85.6, 43.0, 2)
    badge(ax, 78.4, 17.4, 3)
    badge(ax, 22.2, 17.4, 4)
    badge(ax, 13.6, 43.0, 5)
    badge(ax, 34.0, 71.6, 6)

    # ════ 图题 ════
    caption(ax, '图 9-6　促进干系人有效参与', fs=10.5, y=1.2)

    save(fig, OUT)


if __name__ == '__main__':
    main()
