# -*- coding: utf-8 -*-
"""图 1-7　软件项目管理知识领域全貌 —— 教材第 13 页

按 p13「图内要素」逐项还原（横向流程示意图）：
  ① 上、下各一条横贯全图的细长矩形条（左右短竖线收口）：
     上条「如何实现综合管理最优（整合管理）」、下条「如何应对风险（风险管理）」
     —— 两条长条不串接箭头，表示整合管理统摄全流程、风险管理贯穿全流程；
  ② 夹在两长条之间、自左至右的流程带：
     竖长细高框（竖排单列「需要做什么（范围管理）」）
     → 右向空心块箭头 → 虚线大框（内嵌三个实线小框：什么时候做（进度管理）／
       花多少钱做（成本管理）／按什么要求做（质量管理））
     → 右向空心块箭头 → 竖立细长菱形（竖排「需要什么资源」）
     → 右向空心块箭头 → 虚线大框（内嵌 内部（资源管理）／外部（采购管理），两框间留白较大）
     → 右向空心块箭头 → 竖立细长菱形（竖排「如何交涉与协调」）
     → 右向空心块箭头 → 虚线大框（内嵌 如何获得干系人支持（干系人管理）／如何沟通（沟通管理））；
  ③ 所有连接件均为空心块箭头、一律指向右，全图无向左或循环的回指箭头；
  ④ 层级为「实线小框嵌在虚线大框内」；左端竖框与两个菱形为竖排文字。
"""
import os
import sys

from matplotlib.patches import Polygon, Rectangle, FancyBboxPatch

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

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'assets',
                   '图1-7-知识领域全貌.png')

CY = 51.0                        # 流程带中线
CAP_Y = 3.0
STRIP_TOP = (91.0, 96.0)         # 上长条 (下沿, 上沿)
STRIP_BOT = (7.0, 12.0)          # 下长条
X0, X1 = 4.0, 152.0              # 两条长条的横向范围（横贯全图）


def vtext(ax, x, y, s, fs=9.2, color=NAV, lh=1.18):
    """竖排文字：每字一行，自上而下读。"""
    ax.text(x, y, '\n'.join(s), ha='center', va='center', color=color,
            fontsize=fs, linespacing=lh, zorder=5)


def block_arrow(ax, xa, xb, yc, th=8.0, hh=14.0, hl=4.6):
    """右向空心块箭头（白底黑框），两端各留 0.4 不贴框。"""
    x0, x1 = xa + 0.4, xb - 0.4
    xh = x1 - hl
    pts = [(x0, yc - th / 2), (xh, yc - th / 2), (xh, yc - hh / 2), (x1, yc),
           (xh, yc + hh / 2), (xh, yc + th / 2), (x0, yc + th / 2)]
    ax.add_patch(Polygon(pts, closed=True, facecolor='white', edgecolor=INK,
                         linewidth=1.25, joinstyle='miter', zorder=3))


def dash_box(ax, x0, y0, w, h):
    """虚线大框（二级容器，不填充）。"""
    ax.add_patch(Rectangle((x0, y0), w, h, facecolor='none', edgecolor=INK,
                           linewidth=1.05, linestyle=(0, (4, 3)), zorder=2))


def solid_box(ax, x0, y0, w, h, lines, fs=9.6):
    """实线小框（一级内容）：浅蓝填充 + 居中多行文字。"""
    ax.add_patch(FancyBboxPatch((x0, y0), w, h, boxstyle='round,pad=0,rounding_size=0.8',
                                facecolor=FILL, edgecolor=LINE, linewidth=1.2, zorder=3))
    ax.text(x0 + w / 2, y0 + h / 2, '\n'.join(lines), ha='center', va='center',
            color=NAV, fontsize=fs, linespacing=1.45, zorder=4)


def diamond(ax, cx, cy, hw, hh, text, fs=9.2):
    """竖立细长菱形（高约为宽的 4 倍），内为竖排文字。"""
    pts = [(cx, cy + hh), (cx + hw, cy), (cx, cy - hh), (cx - hw, cy)]
    ax.add_patch(Polygon(pts, closed=True, facecolor=FILL, edgecolor=LINE,
                         linewidth=1.2, zorder=3))
    vtext(ax, cx, cy, text, fs=fs)


def main():
    setup()
    fig, ax = canvas(13.0, 8.33, xlim=(0, 156), ylim=(0, 100))

    # ── ① 上下两条横贯全图的细长矩形条 ──
    for (yb, yt), s in ((STRIP_TOP, '如何实现综合管理最优（整合管理）'),
                        (STRIP_BOT, '如何应对风险（风险管理）')):
        ax.add_patch(Rectangle((X0, yb), X1 - X0, yt - yb, facecolor=FILL,
                               edgecolor=INK, linewidth=1.2, zorder=2))
        ax.text((X0 + X1) / 2, (yb + yt) / 2, s, ha='center', va='center',
                color=NAV, fontsize=11.5, zorder=3)

    # ── 第一：竖长细高框「需要做什么（范围管理）」（竖排单列）──
    ax.add_patch(FancyBboxPatch((6.0, 25.0), 12.0, 52.0,
                                boxstyle='round,pad=0,rounding_size=1.0',
                                facecolor=FILL, edgecolor=LINE, linewidth=1.2, zorder=3))
    vtext(ax, 12.0, CY, '需要做什么（范围管理）', fs=9.0)
    block_arrow(ax, 18.0, 27.0, CY)

    # ── 第三：虚线框 + 三个实线小框（进度／成本／质量）──
    dash_box(ax, 27.0, 22.0, 25.0, 58.0)
    solid_box(ax, 29.0, 62.0, 21.0, 14.0, ['什么时候做', '（进度管理）'])
    solid_box(ax, 29.0, 44.0, 21.0, 14.0, ['花多少钱做', '（成本管理）'])
    solid_box(ax, 29.0, 26.0, 21.0, 14.0, ['按什么要求做', '（质量管理）'])
    block_arrow(ax, 52.0, 61.0, CY)

    # ── 第五：细长菱形「需要什么资源」──
    diamond(ax, 67.0, CY, 6.0, 24.0, '需要什么资源')
    block_arrow(ax, 73.0, 82.0, CY)

    # ── 第七：虚线框 + 内部／外部（两框之间留白约一个框高）──
    dash_box(ax, 82.0, 23.0, 19.0, 56.0)
    solid_box(ax, 84.0, 57.0, 15.0, 16.0, ['内部', '（资源管理）'])
    solid_box(ax, 84.0, 29.0, 15.0, 16.0, ['外部', '（采购管理）'])
    block_arrow(ax, 101.0, 110.0, CY)

    # ── 第九：细长菱形「如何交涉与协调」──
    diamond(ax, 116.0, CY, 6.0, 24.0, '如何交涉与协调')
    block_arrow(ax, 122.0, 131.0, CY)

    # ── 第十一：虚线框 + 干系人／沟通 ──
    dash_box(ax, 131.0, 23.0, 20.0, 56.0)
    solid_box(ax, 133.0, 57.0, 16.0, 16.0, ['如何获得干系人支', '持（干系人管理）'], fs=8.8)
    solid_box(ax, 133.0, 29.0, 16.0, 16.0, ['如何沟通', '（沟通管理）'], fs=9.2)

    # ── 图题 ──
    ax.text((X0 + X1) / 2, CAP_Y, '图 1-7　软件项目管理知识领域全貌',
            ha='center', va='center', color='#4a5568', fontsize=10.5)

    save(fig, OUT)


if __name__ == '__main__':
    main()
