# -*- coding: utf-8 -*-
"""图 5-28　排列活动顺序（按教材图意 Python 重绘，未裁用课本照片）
Project 窗口：任务选项卡 →「日程」组「链接任务」按钮高亮（②），任务表中两行被选中（①），
右侧甘特区两条任务条之间建立「默认 FS 关系」箭头。
"""
import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _draw import setup, canvas, box, arrow, line, INK, NAV, ACC, FILL, LINE, save
from matplotlib.patches import Rectangle

HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, '..', 'assets', '图5-28-排列活动顺序.png')

TASKS = [
    ('1.教务管理系统', 0, 1), ('1.1系统需求', 1, 1), ('1.1.1需求收集', 2, 1),
    ('1.1.1.1调研问卷设计', 3, 0), ('1.1.1.2访谈', 3, 0), ('1.1.1.3需求讨论会', 3, 0),
]
SEL = {3, 4}

setup()
fig, ax = canvas(11.0, 4.4, xlim=(0, 100), ylim=(29, 100))

# 窗口（底边随表格高度收紧，避免大片空白）
TBL_BOTTOM = 63.0 - 4.0 - 4.4 * len(TASKS)
WIN_BOTTOM = TBL_BOTTOM - 2.5
ax.add_patch(Rectangle((1, WIN_BOTTOM), 98, 96 - (WIN_BOTTOM - 2), fill=True, fc='white', ec='#4a5568', lw=1.2, zorder=1))
ax.text(2.5, 94.5, '教务管理系统-进度管理实验.mpp  -  Project Professional', fontsize=6.8, color=INK, va='center')

# 选项卡
cx = 3.0
for name, act in [('文件', 0), ('任务', 1), ('资源', 0), ('报表', 0), ('项目', 0), ('视图', 0), ('帮助', 0)]:
    w = 4.2 if len(name) <= 2 else 6.6
    box(ax, cx, 86.0, w, 4.4, name, fc=(FILL if act else 'white'), ec=(ACC if act else LINE),
        fs=6.4, bold=bool(act), radius=0.5, lw=1.0)
    cx += w + 0.5

# 功能区
ax.add_patch(Rectangle((2, 66), 96, 17.5, fill=True, fc='#f7fafc', ec='#cbd5e0', lw=0.9, zorder=2))
for name, x in [('粘贴', 4), ('剪切', 9), ('复制', 14), ('格式刷', 19.5), ('字体', 30), ('日程', 48), ('任务', 72)]:
    ax.text(x, 81.0, name, fontsize=5.0, color='#4a5568', va='center', zorder=3)
ax.plot([26.5, 26.5], [66.5, 82.8], color='#e2e8f0', lw=0.8, zorder=2)
ax.plot([46.0, 46.0], [66.5, 82.8], color='#e2e8f0', lw=0.8, zorder=2)
ax.plot([69.5, 69.5], [66.5, 82.8], color='#e2e8f0', lw=0.8, zorder=2)

box(ax, 48.0, 78.0, 12.0, 4.2, '链接任务', fc='#bee3f8', ec=ACC, fs=6.2, bold=True, radius=0.5, lw=1.2)
ax.text(54.0, 74.4, '将带有日期的任务\n添加到日程表', fontsize=4.6, color='#4a5568', ha='center', va='top', linespacing=1.4)
ax.annotate('', xy=(56.0, 75.6), xytext=(60.5, 71.6),
            arrowprops=dict(arrowstyle='-|>', color=INK, lw=1.0))
ax.text(61.0, 71.0, '②', fontsize=8, color='#c53030', fontweight='bold')

# 任务表
x0, top = 3.0, 63.0
tw = 64.0
hh = 4.0
rh = 4.4
n = len(TASKS)
table_h = hh + rh * n
ax.add_patch(Rectangle((x0, top - table_h), tw, table_h, fill=True, fc='white', ec='#4a5568', lw=1.0, zorder=2))
ax.add_patch(Rectangle((x0, top - hh), tw, hh, fill=True, fc='#edf2f7', ec='#4a5568', lw=0.8, zorder=3))
for name, xx, ww in [('任务模式', 0, 12), ('任务名称', 12, 34), ('资源名称', 46, 18)]:
    ax.text(x0 + xx + 0.9, top - hh / 2, name, fontsize=5.2, color=INK, va='center', zorder=4)
    ax.plot([x0 + xx, x0 + xx], [top - table_h, top], color='#cbd5e0', lw=0.7, zorder=4)
for i, (name, lvl, sm) in enumerate(TASKS):
    yy = top - hh - rh * i
    if i in SEL:
        ax.add_patch(Rectangle((x0, yy - rh), tw, rh, fill=True, fc='#e2e8f0', ec='none', zorder=3))
    ax.text(x0 + 13.0, yy - rh / 2, ('▶ ' if sm else '') + name, fontsize=5.2,
            color=INK, va='center', zorder=4)
ax.text(x0 + 1.2, top - hh - rh * 3 - rh / 2, '①', fontsize=8, color='#c53030',
        fontweight='bold', va='center', zorder=5)

# 甘特区
gx = 69.0
gw = 29.0
ax.add_patch(Rectangle((gx, top - table_h), gw, table_h, fill=True, fc='white', ec='#4a5568', lw=1.0, zorder=2))
ax.add_patch(Rectangle((gx, top - hh), gw, hh, fill=True, fc='#edf2f7', ec='#4a5568', lw=0.8, zorder=3))
for lab, xx in [('2024年11月4日', 0.10), ('2024年11月11日', 0.62)]:
    ax.text(gx + gw * xx, top - hh / 2, lab, fontsize=4.2, color='#4a5568', va='center', zorder=4)
for i in range(n):
    yy = top - hh - rh * i
    ax.plot([gx, gx + gw], [yy, yy], color='#edf2f7', lw=0.6, zorder=2)
bar_y1 = top - hh - rh * 3 - rh / 2
bar_y2 = top - hh - rh * 4 - rh / 2
ax.add_patch(Rectangle((gx + 1.2, bar_y1 - 0.8), 9.4, 1.6, fc=ACC, ec=ACC, zorder=4))
ax.add_patch(Rectangle((gx + 10.6, bar_y2 - 0.8), 6.8, 1.6, fc=ACC, ec=ACC, zorder=4))
line(ax, (gx + 10.6, bar_y1), (gx + 10.6, bar_y2 + 0.9), color=INK, lw=1.0)
arrow(ax, (gx + 10.6, bar_y2 + 0.9), (gx + 10.6, bar_y2 + 0.2), color=INK, lw=1.0, style='-|>')
ax.text(gx + 18.2, bar_y1 - 0.4, '分析人员-李明', fontsize=4.2, color='#4a5568', va='center')
ax.text(gx + 2.0, bar_y2 - 2.6, '默认FS关系', fontsize=4.6, color='#c53030', va='center')
ax.text(gx + 18.2, bar_y2 - 1.2, '分析人员-王飞', fontsize=4.2, color='#4a5568', va='center')

save(fig, OUT)
