# -*- coding: utf-8 -*-
"""图 5-24　设置里程碑：Project「任务信息」对话框（高级选项卡：工期 0 个工作日、
勾选「标记为里程碑」），背景为任务工作表。按教材截图重绘界面要素。"""
import sys, os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _draw import setup, canvas, save
from matplotlib.patches import Rectangle

setup()
fig, ax = canvas(11.4, 7.5, xlim=(0, 114), ylim=(0, 75))

INK, NAVY, GREY = '#1a202c', '#1a365d', '#4a5568'
EDGE, HDR, CHROME = '#8fa8bf', '#dbe6f0', '#eef3f8'


def field(x0, x1, y, h, text, fs=8.2, align='left', dd=False, bold=False):
    ax.add_patch(Rectangle((x0, y), x1 - x0, h, facecolor='white', edgecolor=EDGE,
                           lw=0.8, zorder=8))
    ax.text(x0 + 1.0 if align == 'left' else (x0 + x1) / 2, y + h / 2, text,
            va='center', ha=align, fontsize=fs, color=INK, zorder=9,
            fontweight='bold' if bold else 'normal')
    if dd:
        ax.plot([x1 - 1.7], [y + h / 2], marker='v', ms=2.6, color=GREY, zorder=9)


def label(x, y, text, fs=8.2):
    ax.text(x, y, text, va='center', ha='left', fontsize=fs, color=INK, zorder=9)


def checkbox(x, y, text, checked=False, fs=8.0, bold=False):
    ax.add_patch(Rectangle((x, y - 0.8), 1.7, 1.7, facecolor='white', edgecolor=GREY,
                           lw=0.8, zorder=9))
    if checked:
        ax.plot([x + 0.35, x + 0.8, x + 1.45], [y, y - 0.55, y + 0.75],
                color='#1a365d', lw=1.2, zorder=10)
    ax.text(x + 2.3, y, text, va='center', fontsize=fs, color=INK, zorder=9,
            fontweight='bold' if bold else 'normal')


# ---------- 背景窗口 ----------
ax.add_patch(Rectangle((1, 1), 112, 72.5, facecolor='white', edgecolor=EDGE, lw=1.3, zorder=1))
ax.add_patch(Rectangle((1, 68.5), 112, 5.0, facecolor=CHROME, edgecolor=EDGE, lw=0.9, zorder=2))
ax.text(3.2, 71.0, '实验3-进度管理实验.mpp - Project Professional', va='center',
        fontsize=9.3, color=GREY, zorder=3)
# 选项卡
ax.add_patch(Rectangle((1, 63.8), 112, 4.7, facecolor='white', edgecolor=EDGE, lw=0.9, zorder=2))
x = 4.0
for t in ['文件', '任务', '资源', '报表', '项目', '视图', '帮助', '甘特图格式']:
    w = len(t) * 2.35 + 2.0
    ax.text(x, 66.2, t, va='center', fontsize=10.2, color=NAVY if t == '任务' else INK,
            fontweight='bold' if t == '任务' else 'normal', zorder=4)
    x += w + 1.4
ax.text(99.0, 66.2, '操作说明搜索', va='center', fontsize=8.0, color=GREY, zorder=4)

# 功能区
ax.add_patch(Rectangle((1, 50.0), 112, 13.8, facecolor='white', edgecolor=EDGE, lw=0.9, zorder=2))
for gx in (22.0, 47.0, 72.0, 92.0):
    ax.plot([gx, gx], [51.0, 62.8], color='#c9d6e2', lw=0.9, zorder=3)
ribbon = [('日程', ['链接任务', '取消链接', '后续任务']),
          ('任务', ['手动安排', '自动安排', '检查', '移动']),
          ('插入', ['任务', '摘要', '里程碑', '可交付成果']),
          ('属性', ['信息', '备注'])]
bx = 3.0
for gname, items in ribbon:
    ax.text(bx, 51.0, gname, fontsize=7.6, color=GREY, va='bottom', zorder=4)
    for i, it in enumerate(items):
        ax.text(bx + (i % 4) * 5.6, 58.4 - (i // 4) * 3.6, it, fontsize=6.8,
                color=INK, va='center', zorder=4)
    bx += 24.0

# 背景任务表
TX = 3.0
ax.add_patch(Rectangle((TX, 4.0), 34.0, 45.0, facecolor='white', edgecolor=EDGE, lw=0.8, zorder=2))
ax.add_patch(Rectangle((TX, 44.0), 34.0, 5.0, facecolor=HDR, edgecolor=EDGE, lw=0.8, zorder=3))
ax.text(TX + 2.0, 46.5, '任务名称', fontsize=8.0, va='center', color=INK, zorder=4)
bg_tasks = [(1, '1.教务管理系统', 1, True), (2, '1.1系统需求', 2, True),
            (3, '1.1.1需求收集', 3, True), (4, '1.1.1.1调研问卷设计', 4, False),
            (5, '1.1.1.2访谈', 4, False), (6, '1.1.1.3需求讨论会', 4, False),
            (7, '1.1.2需求分析', 3, True), (8, '1.1.2.1功能需求分析', 4, False),
            (9, '1.1.2.2非功能需求分析', 4, False), (10, '1.1.2.3需求规格说明书', 4, False),
            (11, '完成系统需求工作', 1, False), (12, '1.2系统设计', 2, True),
            (13, '1.2.1系统架构设计', 3, False), (14, '1.2.2数据库设计', 3, True),
            (15, '完成系统设计工作', 1, False), (16, '1.3系统开发', 2, True)]
RH = 2.6
for i, (num, name, lvl, summary) in enumerate(bg_tasks):
    top = 44.0 - i * RH
    if num == 11:   # 当前任务行（高亮）
        ax.add_patch(Rectangle((TX, top - RH), 34.0, RH, facecolor='#cfd9e4',
                               edgecolor='none', zorder=2.5))
    ax.plot([TX, TX + 34.0], [top - RH, top - RH], color='#e2e8f0', lw=0.5, zorder=3)
    tx = TX + 4.5 + (lvl - 1) * 3.2
    if summary:
        ax.text(tx - 1.3, top - RH / 2, '◢', fontsize=3.6, color=NAVY, va='center', zorder=4)
    ax.text(tx, top - RH / 2, name, fontsize=6.4, va='center', color=INK, zorder=4,
            fontweight='bold' if summary or num == 11 else 'normal')

# ---------- 「任务信息」对话框 ----------
DX0, DX1, DY0, DY1 = 38.0, 109.0, 3.0, 57.0
ax.add_patch(Rectangle((DX0, DY0), DX1 - DX0, DY1 - DY0, facecolor='#fbfdff',
                       edgecolor='#4a6b8a', lw=1.4, zorder=6))
ax.add_patch(Rectangle((DX0, DY1 - 4.2), DX1 - DX0, 4.2, facecolor='#dde7f0',
                       edgecolor='#4a6b8a', lw=1.0, zorder=7))
ax.text(DX0 + 1.6, DY1 - 2.1, '任务信息', fontsize=9.0, va='center', color=INK, zorder=8)
ax.text(DX1 - 2.4, DY1 - 2.1, '×', fontsize=11.0, va='center', ha='center',
        color=GREY, zorder=8)

# 选项卡
tab_y = DY1 - 7.2
ax.plot([DX0 + 1.0, DX1 - 1.0], [tab_y, tab_y], color=EDGE, lw=0.9, zorder=8)
tabx = DX0 + 2.2
for t in ['常规', '前置任务', '资源', '高级', '备注', '自定义域']:
    tw = len(t) * 1.55 + 1.6
    if t == '高级':
        ax.add_patch(Rectangle((tabx - 0.8, tab_y), tw, 3.4, facecolor='#fbfdff',
                               edgecolor='#4a6b8a', lw=1.2, zorder=9))
        ax.text(tabx + tw / 2 - 0.8, tab_y + 1.7, t, fontsize=8.4, ha='center',
                va='center', color=INK, zorder=10)
    else:
        ax.text(tabx + tw / 2 - 0.8, tab_y + 1.7, t, fontsize=8.2, ha='center',
                va='center', color=GREY, zorder=10)
    tabx += tw + 1.9

# 名称 + 工期
label(DX0 + 2.0, 48.5, '名称(N)：')
field(DX0 + 12.5, DX0 + 40.0, 46.9, 3.2, '完成系统需求工作')
label(DX0 + 41.0, 48.5, '工期(D)：')
field(DX0 + 48.5, DX0 + 56.5, 46.9, 3.2, '0 个工作日', fs=7.2)
ax.add_patch(Rectangle((DX0 + 56.5, 46.9), 2.2, 3.2, facecolor='white', edgecolor=EDGE,
                       lw=0.8, zorder=8))
ax.plot([DX0 + 57.6], [48.9], marker='^', ms=2.4, color=GREY, zorder=9)
ax.plot([DX0 + 57.6], [47.9], marker='v', ms=2.4, color=GREY, zorder=9)
checkbox(DX0 + 60.2, 48.5, '估计(E)', checked=False, fs=7.8)

# 任务限制
ax.text(DX0 + 2.0, 43.2, '任务限制', fontsize=8.2, color=GREY, zorder=9)
label(DX0 + 3.0, 39.9, '最后期限(L)：')
field(DX0 + 12.5, DX0 + 42.0, 38.3, 3.2, 'NA', dd=True)

# 限制类型 / 限制日期
label(DX0 + 3.0, 35.4, '限制类型(F)：')
field(DX0 + 12.5, DX0 + 39.5, 33.8, 3.2, '不得早于...开始', dd=True)
label(DX0 + 40.5, 35.4, '限制日期(T)：')
field(DX0 + 50.0, DX0 + 66.5, 33.8, 3.2, '2024年11月26日', fs=7.4, dd=True)

# 任务类型
label(DX0 + 3.0, 30.9, '任务类型(Y)：')
field(DX0 + 12.5, DX0 + 39.5, 29.3, 3.2, '固定单位', dd=True)
checkbox(DX0 + 41.2, 30.9, '投入比导向(O)', checked=False, fs=7.6)

# 日历
label(DX0 + 3.0, 26.4, '日历(A)：')
field(DX0 + 12.5, DX0 + 39.5, 24.8, 3.2, '无', dd=True)
checkbox(DX0 + 41.2, 26.4, '排定日程时忽略资源日历(G)', checked=False, fs=7.6)

# WBS 码
label(DX0 + 3.0, 21.9, 'WBS 码(W)：')
field(DX0 + 12.5, DX0 + 39.5, 20.3, 3.2, '1.2')

# 挣值方法
label(DX0 + 3.0, 17.4, '挣值方法(V)：')
field(DX0 + 20.0, DX0 + 41.0, 15.8, 3.2, '完成百分比', dd=True)

# 标记为里程碑（教材重点）
ax.add_patch(Rectangle((DX0 + 1.4, 9.6), 30.0, 5.0, facecolor='none',
                       edgecolor='#2b6cb0', lw=1.2, zorder=10))
ax.add_patch(Rectangle((DX0 + 2.6, 10.9), 1.9, 1.9, facecolor='white',
                       edgecolor=GREY, lw=0.9, zorder=10))
ax.plot([DX0 + 3.0, DX0 + 3.55, DX0 + 4.35], [11.8, 11.2, 12.6],
        color=NAVY, lw=1.4, zorder=11)
ax.text(DX0 + 5.6, 11.85, '标记为里程碑(M)', fontsize=8.4, va='center',
        color=INK, zorder=11, fontweight='bold')

# 按钮
for bx0, bw, txt in ((DX0 + 1.6, 9.0, '帮助(H)'),
                     (DX1 - 21.0, 9.0, '确定'),
                     (DX1 - 10.5, 9.0, '取消')):
    ax.add_patch(Rectangle((bx0, DY0 + 1.2), bw, 3.2, facecolor='#f1f5f9',
                           edgecolor='#4a6b8a', lw=0.9, zorder=9))
    ax.text(bx0 + bw / 2, DY0 + 2.8, txt, ha='center', va='center', fontsize=8.0,
            color=INK, zorder=10)

save(fig, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
                       'assets', '图5-24-设置里程碑.png'))
