# -*- coding: utf-8 -*-
"""图 5-13　资源平滑
左（虚线框）：开始 → E(8天/5人)、F(5天/3人)、G(3天/2人) 三项并行 → 结束；
粗黑箭头；
右（虚线框）：调整后 开始 → E(8天/5人) → 结束，同时 开始 → F(5天/3人) → G(3天/2人) → 结束。
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from _draw import setup, canvas, arrow, save, INK, FILL
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, FancyBboxPatch

setup()

BW, BH = 13.0, 9.6          # 活动框宽高


def act(ax, cx, cy, name, note=None, w=BW, h=BH):
    ax.add_patch(Rectangle((cx - w / 2, cy - h / 2), w, h,
                           facecolor=FILL, edgecolor=INK, lw=1.4, zorder=3))
    if note:
        ax.text(cx, cy + h * 0.18, name, ha='center', va='center',
                fontsize=11.5, color=INK, zorder=4)
        ax.text(cx, cy - h * 0.20, note, ha='center', va='center',
                fontsize=10.5, color=INK, zorder=4)
    else:
        ax.text(cx, cy, name, ha='center', va='center',
                fontsize=11.5, color=INK, zorder=4)


def anchor(c, t, w, h):
    """c/t 为 (cx, cy)；返回从 c 指向 t 时与 c 方框边框的交点。"""
    dx, dy = t[0] - c[0], t[1] - c[1]
    if dx == 0 and dy == 0:
        return c
    sx = (w / 2.0) / abs(dx) if dx else float('inf')
    sy = (h / 2.0) / abs(dy) if dy else float('inf')
    s = min(sx, sy)
    return (c[0] + dx * s, c[1] + dy * s)


def connect(ax, a, b, wa=BW, ha=BH, wb=BW, hb=BH, gap=0.6):
    p1 = anchor(a, b, wa, ha)
    p2 = anchor(b, a, wb, hb)
    dx, dy = p2[0] - p1[0], p2[1] - p1[1]
    d = (dx * dx + dy * dy) ** 0.5 or 1.0
    ux, uy = dx / d, dy / d
    arrow(ax, (p1[0] + ux * gap, p1[1] + uy * gap),
          (p2[0] - ux * gap, p2[1] - uy * gap), color=INK, lw=1.4)


def dashed(ax, x0, y0, x1, y1):
    p = FancyBboxPatch((x0, y0), x1 - x0, y1 - y0,
                       boxstyle='round,pad=0,rounding_size=2.0',
                       fill=False, ec=INK, lw=1.3, linestyle=(0, (5, 3)), zorder=1)
    ax.add_patch(p)


fig, ax = canvas(w=11.8, h=6.2, xlim=(0, 118), ylim=(12, 74))

dashed(ax, 2, 18, 57, 68)
dashed(ax, 62, 18, 116, 68)

# ---- 左：三项并行 ----
S1 = (9.0, 43.0)
E1 = (29.0, 59.0)
F1 = (29.0, 43.0)
G1 = (29.0, 27.0)
T1 = (49.0, 43.0)
act(ax, *S1, '开始')
act(ax, *E1, 'E', '8天/5人')
act(ax, *F1, 'F', '5天/3人')
act(ax, *G1, 'G', '3天/2人')
act(ax, *T1, '结束')

for node in (E1, F1, G1):
    connect(ax, S1, node)
    connect(ax, node, T1)

# ---- 中间粗黑箭头 ----
ax.annotate('', xy=(61.5, 43), xytext=(57.5, 43),
            arrowprops=dict(arrowstyle='-|>', color=INK, lw=6,
                            mutation_scale=24, shrinkA=0, shrinkB=0))

# ---- 右：E 与 F→G 并行 ----
AW, AH = 10.5, 8.6
S2 = (69.0, 34.0)
E2 = (89.0, 55.0)
F2 = (83.5, 24.0)
G2 = (99.0, 24.0)
T2 = (110.0, 40.0)
act(ax, *S2, '开始', w=AW, h=AH)
act(ax, *E2, 'E', '8天/5人', w=AW, h=AH)
act(ax, *F2, 'F', '5天/3人', w=AW, h=AH)
act(ax, *G2, 'G', '3天/2人', w=AW, h=AH)
act(ax, *T2, '结束', w=AW, h=AH)

connect(ax, S2, E2, AW, AH, AW, AH)
connect(ax, S2, F2, AW, AH, AW, AH)
connect(ax, F2, G2, AW, AH, AW, AH)
connect(ax, G2, T2, AW, AH, AW, AH)
connect(ax, E2, T2, AW, AH, AW, AH)

save(fig, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
                       'assets', '图5-13-资源平滑.png'))
