# -*- coding: utf-8 -*-
"""图 5-17　Project 生成的进度网络图（按照片示意重绘，节点数据取统一数据表）

节点框：任务名称 / 开始日期 / 完成时间 / 工期 / 标识号 / 资源
箭头：1.1.1.2 访谈 → 1.1.1.3 需求讨论会 →（分支）1.1.2.1 / 1.1.2.2 →（汇合）1.1.2.3
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, FancyArrowPatch

from _draw import setup, INK, NAV, ACC, LINE

# (标题, 开始日期, 完成日期, 工期, 标识号, 资源)
NODES = {
    'A': ("1.1.1.2访谈", "2024年11月15日", "2024年11月18日", "2个工作日", "6", "分析人员-王飞"),
    'B': ("1.1.1.3需求讨论会", "2024年11月19日", "2024年11月19日", "1个工作日", "7", "分析人员-李明"),
    'C': ("1.1.2.1功能需求分析", "2024年11月20日", "2024年11月25日", "4个工作日", "9", "分析人员-李明"),
    'D': ("1.1.2.2非功能需求分析", "2024年11月20日", "2024年11月22日", "3个工作日", "10", "分析人员-王飞"),
    'E': ("1.1.2.3需求规格说明书", "2024年11月26日", "2024年11月27日", "2个工作日", "11", "分析人员-李明"),
}

W, H = 36.0, 13.0
POS = {
    'A': (4.0, 78.0),
    'B': (50.0, 78.0),
    'C': (72.0, 50.0),
    'D': (72.0, 22.0),
    'E': (116.0, 50.0),
}


def node(ax, key, x, y):
    title, sd, fd, dur, ident, res = NODES[key]
    ax.add_patch(Rectangle((x, y - H), W, H, facecolor='white', edgecolor=NAV, lw=1.1, zorder=3))
    ax.plot([x + 0.4, x + W - 0.4], [y - 3.6, y - 3.6], color=LINE, lw=0.6, zorder=4)
    ax.text(x + 1.2, y - 1.9, title, ha='left', va='center', fontsize=8.2,
            color=NAV, fontweight='bold', zorder=5)
    lx, rx = x + 1.2, x + W - 1.2
    ax.text(lx, y - 5.5, f"开始日期：{sd}", ha='left', va='center', fontsize=6.5, color=INK, zorder=5)
    ax.text(rx, y - 5.5, f"标识号：{ident}", ha='right', va='center', fontsize=6.5, color=INK, zorder=5)
    ax.text(lx, y - 8.4, f"完成时间：{fd}", ha='left', va='center', fontsize=6.5, color=INK, zorder=5)
    ax.text(rx, y - 8.4, f"工期：{dur}", ha='right', va='center', fontsize=6.5, color=INK, zorder=5)
    ax.text(lx, y - 11.3, f"资源：{res}", ha='left', va='center', fontsize=6.5, color=INK, zorder=5)


def poly_arrow(ax, pts, color=ACC, lw=1.2):
    xs = [p[0] for p in pts]
    ys = [p[1] for p in pts]
    ax.plot(xs, ys, color=color, lw=lw, solid_capstyle='butt', zorder=2)
    ax.add_patch(FancyArrowPatch(pts[-2], pts[-1], arrowstyle='-|>', mutation_scale=11,
                                 linewidth=lw, color=color, shrinkA=0, shrinkB=0, zorder=2))


def main():
    setup()
    fig, ax = plt.subplots(figsize=(15.6, 7.6))
    ax.set_xlim(0, 160)
    ax.set_ylim(0, 84)
    ax.axis('off')

    ax.add_patch(Rectangle((2, 5), 155, 77, fill=False, lw=1.2, ec=NAV))

    for k, (x, y) in POS.items():
        node(ax, k, x, y)

    # A → B
    poly_arrow(ax, [(40.0, 71.5), (50.0, 71.5)])
    # B → 主干（下）→ C、D
    poly_arrow(ax, [(68.0, 65.0), (68.0, 43.5), (72.0, 43.5)])
    poly_arrow(ax, [(68.0, 43.5), (68.0, 15.5), (72.0, 15.5)])
    # C → E
    poly_arrow(ax, [(108.0, 43.5), (116.0, 43.5)])
    # D → E（绕行下方，从 E 底边进入）
    poly_arrow(ax, [(108.0, 15.5), (112.0, 15.5), (112.0, 30.0), (136.0, 30.0), (136.0, 37.0)])

    out = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..',
                                       'assets', '图5-17-Project生成的进度网络图.png'))
    fig.savefig(out)
    print('✅ 已绘制', os.path.basename(out), os.path.getsize(out) // 1024, 'KB')
    plt.close(fig)


if __name__ == '__main__':
    main()
