# -*- coding: utf-8 -*-
"""图 8-8　过度分配的资源 —— 教材第 202 页

Project“报表”功能下的“过度分配的资源”报表。按「图内要素」①～⑦ 逐项还原：
① 选项卡：文件、任务、资源、报表（选中）、项目、视图、帮助、报表设计，右侧搜索框
② 功能区两组：项目——比较项目；查看报表——新建报表、仪表板、资源（下拉箭头展开中）、
   成本、进行中、入门、自定义、最近
③ 左侧报表列表：甘特图、跟踪甘特图、里程碑日期总…、里程碑总成、任务窗体、任务分配状况、
   任务工作表、日程表、日历（图标带边框高亮）
④ 报表主标题“过度分配的资源”
⑤ 左块“工作状态／过度分配的资源的工作状态。”：柱状图 0—800 工时（每 100 一档），
   唯一资源“项目经理-张军”，两根深色柱标注 616 工时／48 工时，图例：实际工时、剩余工时
⑥ 右块“过度分配／分配经过度分配的资源的剩余工时。要解决资源过度分配，请使用”
   ＋黑框按钮“工作组规划器视图”；其下柱状图 0—9 工时（每 1 一档），横轴按日
   24/11/23—25/3/3，仅最右端 25/2/28 与 25/3/3 两根接近顶格的细柱，
   图例：资源过度分配（项目经理-张军）
⑦ 图题居中置于图下方
"""
import os
import sys
import datetime as dt

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

import matplotlib.pyplot as plt  # noqa: E402
from matplotlib.patches import Polygon, Rectangle  # noqa: E402

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'assets',
                   '图8-8-过度分配的资源.png')

GRID = '#cbd5e0'
FRAME = '#a0aec0'
BG = '#f7fafc'
BLACK = '#1a1a1a'
GREY = '#4a5568'
BAR1 = '#1a202c'
BAR2 = '#4a5568'
RED = '#c53030'

TABS = ['文件', '任务', '资源', '报表', '项目', '视图', '帮助']


def rect(ax, x0, y0, x1, y1, fc='white', ec=GRID, lw=0.6, z=2):
    ax.add_patch(Rectangle((x0, y0), x1 - x0, y1 - y0, facecolor=fc,
                           edgecolor=ec, linewidth=lw, zorder=z))


def txt(ax, x, y, s, fs=6.2, ha='left', va='center', color=INK, bold=False, z=6):
    ax.text(x, y, s, fontsize=fs, ha=ha, va=va, color=color, zorder=z,
            fontweight='bold' if bold else 'normal')


def btn(ax, x, y_top, w, h, label, fs=5.0, dark=False):
    rect(ax, x, y_top - h, x + w, y_top,
         fc=(NAV if dark else 'white'), ec=(NAV if dark else '#94a3b8'),
         lw=(0.9 if dark else 0.7), z=3)
    txt(ax, x + w / 2, y_top - h / 2, label, fs=fs, ha='center',
        color=('white' if dark else INK), bold=dark, z=5)
    if label == '资源':
        ax.add_patch(Polygon([[x + w - 1.9, y_top - h / 2 + 0.35],
                              [x + w - 0.7, y_top - h / 2 + 0.35],
                              [x + w - 1.3, y_top - h / 2 - 0.55]],
                             closed=True, facecolor='white', edgecolor='none', zorder=6))


def draw_chrome(ax):
    ax.add_patch(Rectangle((0.5, 5.0), 99.0, 93.8, facecolor='white',
                           edgecolor='#2d3748', linewidth=1.2, zorder=1))
    rect(ax, 0.62, 95.9, 99.38, 98.6, fc='#f7fafc', ec='#cbd5e0', lw=0.6, z=2)
    txt(ax, 1.9, 97.25, '教务管理系统-资源管理实验.mpp   —   Project Professional',
        fs=5.4, color=GREY, z=3)
    txt(ax, 98.6, 97.25, '—   □   ×', fs=5.8, ha='right', color=GREY, z=3)

    rect(ax, 0.62, 92.4, 99.38, 95.9, fc='white', ec=FRAME, lw=0.7, z=1)
    x = 1.9
    for t in TABS:
        active = (t == '报表')
        txt(ax, x, 94.15, t, fs=6.2, ha='center', bold=active,
            color=(NAV if active else INK), z=6)
        if active:
            ax.plot([x - 2.2, x + 2.2], [92.75, 92.75], color=NAV, lw=2.2, zorder=6)
        x += 5.3
    txt(ax, 40.0, 94.15, '报表设计', fs=6.2, color=GREY, z=6)
    rect(ax, 80.0, 93.0, 98.4, 95.2, fc='white', ec='#cbd5e0', lw=0.6, z=4)
    ax.add_patch(plt.Circle((82.0, 94.1), 0.42, fill=False, ec='#718096', lw=0.7, zorder=5))
    ax.plot([82.35, 82.9], [93.75, 93.25], color='#718096', lw=0.7, zorder=5)
    txt(ax, 84.0, 94.1, '操作说明搜索', fs=5.0, color='#718096', z=5)

    rect(ax, 0.62, 82.0, 99.38, 92.4, fc=BG, ec=FRAME, lw=0.7, z=1)
    ax.plot([12.2, 12.2], [82.6, 91.6], color='#e2e8f0', lw=0.8, zorder=2)
    txt(ax, 2.0, 83.1, '项目', fs=4.8, color='#718096', z=5)
    btn(ax, 2.2, 91.2, 8.4, 6.4, '比较项目', fs=5.0)

    txt(ax, 13.2, 83.1, '查看报表', fs=4.8, color='#718096', z=5)
    items = ['新建报表', '仪表板', '资源', '成本', '进行中', '入门', '自定义', '最近']
    for k, lab in enumerate(items):
        btn(ax, 13.6 + k * 9.6, 91.2, 8.8, 6.4, lab, fs=4.9, dark=(lab == '资源'))


def draw_report_list(ax):
    rect(ax, 0.62, 8.0, 14.0, 82.0, fc='white', ec=FRAME, lw=0.7, z=2)
    items = ['甘特图', '跟踪甘特图', '里程碑日期总…', '里程碑总成', '任务窗体',
             '任务分配状况', '任务工作表', '日程表', '日历']
    h = 7.5
    for i, it in enumerate(items):
        yt = 82.0 - i * h
        ax.plot([0.62, 14.0], [yt - h, yt - h], color=GRID, lw=0.6, zorder=4)
        hl = (it == '日历')
        rect(ax, 1.5, yt - h + 1.1, 5.6, yt - 1.1, fc='white',
             ec=(BLACK if hl else '#94a3b8'), lw=(1.6 if hl else 0.5), z=5)
        ax.plot([2.0, 5.6], [yt - 2.3, yt - 2.3], color='#a0aec0', lw=0.5, zorder=5)
        ax.plot([2.0, 5.0], [yt - 3.3, yt - 3.3], color='#a0aec0', lw=0.5, zorder=5)
        ax.plot([2.0, 6.4], [yt - 4.3, yt - 4.3], color='#cbd5e0', lw=0.5, zorder=5)
        txt(ax, 7.6, yt - h / 2, it, fs=5.2, z=6)
    rect(ax, 1.5, 8.2, 5.6, 12.1, fc='white', ec='#94a3b8', lw=0.5, z=4)


def chart_left(ax):
    X0, X1 = 20.5, 44.0
    Y0, Y1 = 24.5, 71.0
    vmax = 800
    y_of = lambda v: Y0 + (Y1 - Y0) * v / vmax

    for v in range(0, vmax + 1, 100):
        y = y_of(v)
        ax.plot([X0, X1], [y, y], color='#eef2f7', lw=0.4, zorder=2)
        txt(ax, X0 - 0.7, y, str(v), fs=3.5, ha='right', color=GREY, z=5)
    ax.plot([X0, X0], [Y0, Y1], color=GREY, lw=0.7, zorder=4)
    ax.plot([X0, X1], [Y0, Y0], color=GREY, lw=0.7, zorder=4)

    cx = (X0 + X1) / 2
    bw = 3.4
    ax.add_patch(Rectangle((cx - bw - 0.7, Y0), bw, y_of(616) - Y0,
                           facecolor=BAR1, edgecolor='none', zorder=5))
    ax.add_patch(Rectangle((cx + 0.7, Y0), bw, y_of(48) - Y0,
                           facecolor=BAR2, edgecolor='none', zorder=5))
    txt(ax, cx - bw / 2 - 0.7, y_of(616) + 1.4, '616 工时', fs=4.4, ha='center', bold=True, z=7)
    txt(ax, cx + bw / 2 + 0.7, y_of(48) + 1.4, '48 工时', fs=4.4, ha='center', bold=True, z=7)
    txt(ax, cx, Y0 - 1.3, '项目经理-张军', fs=4.4, ha='center', z=6)

    # 图例
    lx, ly = 20.5, 19.6
    ax.add_patch(Rectangle((lx, ly - 0.7), 1.4, 1.4, facecolor=BAR1, edgecolor='none', zorder=6))
    txt(ax, lx + 2.1, ly, '实际工时', fs=4.2, z=6)
    ax.add_patch(Rectangle((lx + 10.0, ly - 0.7), 1.4, 1.4, facecolor=BAR2, edgecolor='none', zorder=6))
    txt(ax, lx + 12.1, ly, '剩余工时', fs=4.2, z=6)


def chart_right(ax):
    X0, X1 = 49.0, 97.5
    Y0, Y1 = 15.0, 64.0
    vmax = 9
    y_of = lambda v: Y0 + (Y1 - Y0) * v / vmax

    d0 = dt.date(2024, 11, 23)
    nday = (dt.date(2025, 3, 3) - d0).days        # 100 → 101 天
    dw = (X1 - X0) / (nday + 1)
    x_of = lambda d: X0 + d * dw

    for v in range(0, vmax + 1):
        y = y_of(v)
        ax.plot([X0, X1], [y, y], color='#eef2f7', lw=0.4, zorder=2)
        txt(ax, X0 - 0.7, y, str(v), fs=3.5, ha='right', color=GREY, z=5)
    ax.plot([X0, X0], [Y0, Y1], color=GREY, lw=0.7, zorder=4)
    ax.plot([X0, X1], [Y0, Y0], color=GREY, lw=0.7, zorder=4)

    # 逐日细网格 + 每周日期刻度（逐日文字在印刷尺寸下不可读，改为日网格＋周标注）
    for d in range(0, nday + 1):
        x = x_of(d)
        ax.plot([x, x], [Y0, Y1], color='#f8fafc', lw=0.35, zorder=2)
        if d % 7 == 0:
            ax.plot([x, x], [Y0, Y0 + 0.9], color=GREY, lw=0.5, zorder=5)
            lab = (d0 + dt.timedelta(days=d)).strftime('%y/%m/%d')
            ax.text(x, Y0 - 0.5, lab, rotation=90, ha='right', va='top',
                    fontsize=3.8, color=GREY, zorder=6)

    # 两根接近顶格的细柱：25/2/28、25/3/3
    for dd in (97, 100):
        x = x_of(dd)
        ax.add_patch(Rectangle((x - 0.45, Y0), 0.9, y_of(9) - Y0,
                               facecolor=BLACK, edgecolor='none', zorder=6))

    # 图例
    lx, ly = 50.0, 66.2
    ax.add_patch(Rectangle((lx, ly - 0.7), 1.4, 1.4, facecolor=BLACK, edgecolor='none', zorder=6))
    txt(ax, lx + 2.1, ly, '资源过度分配（项目经理-张军）', fs=4.2, z=6)


def main():
    setup()
    fig, ax = canvas(15.2, 10.4, xlim=(0, 100), ylim=(0, 100))
    draw_chrome(ax)
    draw_report_list(ax)

    # 报表页面
    rect(ax, 14.6, 6.0, 99.38, 82.0, fc='white', ec=FRAME, lw=0.8, z=2)
    txt(ax, 17.0, 79.6, '过度分配的资源', fs=12.0, va='top', bold=True, color=BLACK, z=6)
    ax.plot([17.0, 97.0], [76.6, 76.6], color=BLACK, lw=0.9, zorder=6)

    # ⑤ 工作状态
    txt(ax, 17.0, 75.0, '工作状态', fs=6.8, bold=True, z=6)
    txt(ax, 17.0, 73.2, '过度分配的资源的工作状态。', fs=5.2, color=GREY, z=6)
    chart_left(ax)

    # ⑥ 过度分配
    txt(ax, 47.0, 75.0, '过度分配', fs=6.8, bold=True, z=6)
    sent = '分配经过度分配的资源的剩余工时。要解决资源过度分配，请使用'
    txt(ax, 47.0, 73.2, sent, fs=4.9, color=GREY, z=6)
    # 带黑框的按钮样式文字块（下一步要点的控件）
    bx0, bw = 65.6, 14.6
    rect(ax, bx0, 71.9, bx0 + bw, 74.6, fc=FILL, ec=BLACK, lw=1.3, z=8)
    txt(ax, bx0 + bw / 2, 73.25, '工作组规划器视图', fs=4.9, ha='center', bold=True, z=9)
    chart_right(ax)

    caption(ax, '图 8-8　过度分配的资源', fs=10.5, y=1.2)
    save(fig, OUT)


if __name__ == '__main__':
    main()
