# -*- coding: utf-8 -*-
"""图 7-11　基于配置库的变更控制 —— 教材第 168 页（7.5.2 配置项控制）

按「图内要素」还原：三枚“数据库”圆柱 + 四条单向弧形箭头构成的闭环。
位置：产品库（右上）、受控库（中央偏右）、开发库（左下）。
① 1.复制：产品库 → 受控库（弧线自产品库左上起笔，向左下弯出，箭头垂直落在受控库桶口右上）
② 2.检出 / check out：受控库 → 开发库（自受控库左侧起笔，向左下弯，落在开发库桶口）
③ 3.检入 / check in：开发库 → 受控库（自开发库右侧起笔，先右后上，落在受控库底部圆弧）
④ 4.更新：受控库 → 产品库（自受控库右侧起笔，向右上弯，落在产品库底部圆弧）
闭环：产品库 →复制→ 受控库 →检出→ 开发库 →检入→ 受控库 →更新→ 产品库。
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _draw import setup, canvas, caption, save, NAV, ACC, FILL

from matplotlib.patches import Rectangle, Ellipse, Arc, FancyArrowPatch
from matplotlib.path import Path

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'assets',
                   '图7-11-基于配置库的变更控制.png')

W, H = 26.0, 22.0        # 圆柱体宽、桶身（竖直段）高
RY = 3.5                 # 桶口椭圆的半高


def cylinder(ax, cx, top, label, fs=12):
    """圆柱体：桶身矩形 + 顶部完整椭圆（桶口）+ 底部下凸弧线。"""
    left, bottom = cx - W / 2, top - H
    # 底部下半椭圆填充（先铺底，避免圆柱底出现白色缺口）
    ax.add_patch(Ellipse((cx, bottom), W, 2 * RY, facecolor=FILL,
                         edgecolor='none', zorder=2))
    ax.add_patch(Rectangle((left, bottom), W, H, facecolor=FILL,
                           edgecolor='none', zorder=2))
    # 侧壁母线
    ax.plot([left, left], [bottom, top], color=ACC, lw=1.3, zorder=3)
    ax.plot([cx + W / 2, cx + W / 2], [bottom, top], color=ACC, lw=1.3, zorder=3)
    # 桶口（完整椭圆）
    ax.add_patch(Ellipse((cx, top), W, 2 * RY, facecolor='#d9edfb',
                         edgecolor=ACC, lw=1.3, zorder=4))
    # 桶底下凸弧
    ax.add_patch(Arc((cx, bottom), W, 2 * RY, theta1=180, theta2=360,
                     edgecolor=ACC, lw=1.3, zorder=3))
    ax.text(cx, top - 8.0, label, ha='center', va='center', color=NAV,
            fontsize=fs, fontweight='bold', zorder=5)


def bez_arrow(ax, p0, p1, p2, p3, color=ACC, lw=1.7):
    """三次贝塞尔弧形箭头，箭头位于 p3，方向由 p2→p3 决定。"""
    path = Path([p0, p1, p2, p3],
                [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4])
    ax.add_patch(FancyArrowPatch(path=path, arrowstyle='-|>', mutation_scale=16,
                                 linewidth=lw, color=color, shrinkA=0, shrinkB=0,
                                 zorder=6))


def main():
    setup()
    fig, ax = canvas(10.0, 8.0, xlim=(0, 100), ylim=(0, 100))

    # ---- 三枚库 ----
    cylinder(ax, 78.0, 89.0, '产品库')      # 右上
    cylinder(ax, 58.0, 61.0, '受控库')      # 中央略偏右
    cylinder(ax, 28.0, 33.0, '开发库')      # 左下

    # ---- ① 1.复制：产品库 → 受控库 ----
    bez_arrow(ax, (64.8, 86.5), (48.0, 84.0), (65.5, 74.0), (65.5, 64.2))
    ax.text(42.0, 91.0, '1.复制', ha='center', va='center', fontsize=11.5, color=NAV)

    # ---- ② 2.检出：受控库 → 开发库 ----
    bez_arrow(ax, (44.8, 58.0), (28.0, 58.0), (34.0, 46.0), (34.0, 36.5))
    ax.text(19.0, 72.0, '2.检出\ncheck out', ha='center', va='center',
            fontsize=11, color=NAV, linespacing=1.5)

    # ---- ③ 3.检入：开发库 → 受控库 ----
    bez_arrow(ax, (41.0, 26.0), (60.0, 21.0), (60.0, 28.0), (60.0, 35.9))
    ax.text(67.5, 23.0, '3.检入\ncheck in', ha='center', va='center',
            fontsize=11, color=NAV, linespacing=1.5)

    # ---- ④ 4.更新：受控库 → 产品库 ----
    bez_arrow(ax, (71.0, 51.0), (89.0, 52.0), (78.0, 58.0), (78.0, 64.0))
    ax.text(74.5, 57.0, '4.更新', ha='center', va='center', fontsize=11.5, color=NAV)

    caption(ax, '图 7-11　基于配置库的变更控制', fs=11.5, y=2.0)
    save(fig, OUT)


if __name__ == '__main__':
    main()
