# -*- coding: utf-8 -*-
"""图 1-3　价值驱动的软件项目管理框架 —— 教材第 7 页

按「图内要素」逐项还原「屋顶＋横梁＋三柱」式框架图（全部细线矩形拼合、**框间无箭头**）：
· 顶部大三角形人字形屋顶（顶点居中），三角形内居中标注「组织战略和目标」，
  两条斜边向下延伸至横梁两端
· 屋顶正下方一条横贯全宽的扁长矩形横梁，居中写「价值交付系统」
· 横梁下方并排三个等高纵向框（左／中／右），顶边紧贴横梁底边：
  - 左框：标题「八大绩效域」＋ 干系人／团队／度量／交付成果 四个绩效域
  - 右框：标题「八大绩效域」＋ 规划／开发方法和生命周期／项目工作／不确定性 四个绩效域
  - 中列自下而上三个框：项目生命周期（4 阶段横排）／五大过程组（3＋2 两行）／
    十大知识领域（3 列 × 4 行，末行「项目整合管理」单占一行居左中）
"""
import os
import sys

import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch, Polygon, Rectangle

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

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'assets',
                   '图1-3-价值驱动框架.png')

BEAM = '#d3e8fb'       # 横梁填充（略深于柱子）
WHITE = 'white'


def frame(ax, x0, y0, x1, y1, fc=FILL, ec=NAV, lw=1.4, r=1.8, z=3):
    ax.add_patch(FancyBboxPatch((x0, y0), x1 - x0, y1 - y0,
                                boxstyle=f'round,pad=0,rounding_size={r}',
                                facecolor=fc, edgecolor=ec, linewidth=lw, zorder=z))


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

    # ── 屋顶 ──
    ax.add_patch(Polygon([(60, 97), (8, 74.5), (112, 74.5)], closed=True,
                         facecolor=FILL, edgecolor=NAV, lw=1.6, zorder=2))
    ax.text(60, 84.6, '组织战略和目标', ha='center', va='center', color=NAV,
            fontsize=15.5, fontweight='bold', zorder=5)

    # ── 横梁 ──
    ax.add_patch(Rectangle((8, 66.8), 104, 7.0, facecolor=BEAM, edgecolor=NAV,
                           lw=1.4, zorder=3))
    ax.text(60, 70.3, '价值交付系统', ha='center', va='center', color=NAV,
            fontsize=14.0, fontweight='bold', zorder=5)

    # ── 左柱 ──
    frame(ax, 6, 8, 32, 66.8)
    ax.text(19, 61.8, '八大绩效域', ha='center', va='center', color=NAV,
            fontsize=11.5, fontweight='bold', zorder=5)
    ax.plot([7.6, 30.4], [58.2, 58.2], color=LINE, lw=1.0, zorder=4)
    LEFT = ['干系人绩效域', '团队绩效域', '度量绩效域', '交付成果绩效域']
    for i, s in enumerate(LEFT):
        ax.text(19, 51.6 - i * 11.5, s, ha='center', va='center', color=INK,
                fontsize=9.4, zorder=5)
        if i < 3:
            yy = 45.85 - i * 11.5
            ax.plot([8.6, 29.4], [yy, yy], color=LINE, lw=0.7, zorder=4, alpha=0.85)

    # ── 右柱 ──
    frame(ax, 88, 8, 114, 66.8)
    ax.text(101, 61.8, '八大绩效域', ha='center', va='center', color=NAV,
            fontsize=11.5, fontweight='bold', zorder=5)
    ax.plot([89.6, 112.4], [58.2, 58.2], color=LINE, lw=1.0, zorder=4)
    RIGHT = ['规划绩效域', '开发方法和生命周期绩效域', '项目工作绩效域', '不确定性绩效域']
    for i, s in enumerate(RIGHT):
        ax.text(101, 51.6 - i * 11.5, s, ha='center', va='center', color=INK,
                fontsize=9.4, zorder=5)
        if i < 3:
            yy = 45.85 - i * 11.5
            ax.plot([90.6, 111.4], [yy, yy], color=LINE, lw=0.7, zorder=4, alpha=0.85)

    # ── 中列一：项目生命周期 ──
    frame(ax, 40, 50.5, 80, 66.8)
    ax.text(60, 62.6, '项目生命周期', ha='center', va='center', color=NAV,
            fontsize=11.5, fontweight='bold', zorder=5)
    ax.plot([41.6, 78.4], [59.2, 59.2], color=LINE, lw=1.0, zorder=4)
    for x, s in zip((45.2, 55.0, 64.8, 74.6),
                    ('启动阶段', '组织与准备阶段', '执行阶段', '结束阶段')):
        ax.text(x, 54.8, s, ha='center', va='center', color=INK, fontsize=7.9, zorder=5)

    # ── 中列二：五大过程组 ──
    frame(ax, 40, 31.5, 80, 48.5)
    ax.text(60, 44.6, '五大过程组', ha='center', va='center', color=NAV,
            fontsize=11.5, fontweight='bold', zorder=5)
    ax.plot([41.6, 78.4], [41.2, 41.2], color=LINE, lw=1.0, zorder=4)
    for x, s in zip((46.5, 60.0, 73.5), ('启动过程组', '规划过程组', '执行过程组')):
        ax.text(x, 37.9, s, ha='center', va='center', color=INK, fontsize=8.6, zorder=5)
    for x, s in zip((53.0, 67.0), ('监控过程组', '收尾过程组')):
        ax.text(x, 33.7, s, ha='center', va='center', color=INK, fontsize=8.6, zorder=5)

    # ── 中列三：十大知识领域（3 列 × 4 行）──
    frame(ax, 40, 8, 80, 29.5)
    ax.text(60, 25.9, '十大知识领域', ha='center', va='center', color=NAV,
            fontsize=11.5, fontweight='bold', zorder=5)
    ax.plot([41.6, 78.4], [22.7, 22.7], color=LINE, lw=1.0, zorder=4)
    GRID = [['项目采购管理', '项目范围管理', '项目进度管理'],
            ['项目成本管理', '项目质量管理', '项目资源管理'],
            ['项目干系人管理', '项目沟通管理', '项目风险管理'],
            ['项目整合管理', '', '']]
    for r, row in enumerate(GRID):
        for c, s in enumerate(row):
            if s:
                ax.text(45.6 + c * 14.2, 19.7 - r * 3.25, s, ha='center',
                        va='center', color=INK, fontsize=7.4, zorder=5)

    save(fig, OUT)


if __name__ == '__main__':
    main()
