# -*- coding: utf-8 -*-
"""图 9-3　凸显模型 —— 教材第 209 页（9.2.1 凸显模型）

按「图内要素」还原：三集合韦恩图（Venn）——三个**等径圆**两两相交、呈品字形
（一上两下）排列，仅细线轮廓。
- 属性标注：上方圆正上方「权力」；左下圆左下方「紧迫性」；右下圆右下方「合法性」；
- 七个区域（按集合归属）：
    仅权力 → 潜伏型（上圆顶部）
    仅紧迫性 → 苛求型（左下圆左侧）
    仅合法性 → 自主型（右下圆右侧）
    权力∩紧迫性 → 危险型（左上交叉区）
    权力∩合法性 → 支配型（右上交叉区）
    紧迫性∩合法性 → 依赖型（底部交叉区）
    权力∩紧迫性∩合法性 → 确定型（三圆共同中心）
- 图题「图 9-3　凸显模型」居中排在图形正下方。

本套插图统一「浅蓝填充 + 细线」：三个半透明浅蓝圆叠加，交集越深颜色越深，
七个区域随明暗自然分层；文字放在各自区域内部，脚本末尾用坐标自检归属，防串区。
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import matplotlib.pyplot as plt                      # noqa: F401
from matplotlib.colors import to_rgba
from matplotlib.patches import Circle

from _draw import setup, canvas, caption, save, INK, NAV, ACC, LINE

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'assets',
                   '图9-3-凸显模型.png')

setup()

# ---------------------------------------------------------------- 几何参数
R = 23.5
A = (50.0, 64.0)          # 上方圆：权力
B = (36.0, 38.0)          # 左下圆：紧迫性
C = (64.0, 38.0)          # 右下圆：合法性

fig, ax = canvas(9.0, 9.0, (0, 100), (0, 100))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)

# ---------------------------------------------------------------- 三个等径圆
for c in (A, B, C):
    # 面用半透明浅蓝（叠加处自然加深），边线保持不透明细线
    ax.add_patch(Circle(c, R, facecolor=to_rgba('#7fbde8', 0.32),
                        edgecolor=ACC, linewidth=1.15, zorder=1))

# ---------------------------------------------------------------- 属性标注
ax.text(A[0], 92.0, '权力', ha='center', va='center', color=NAV,
        fontsize=14, fontweight='bold')
ax.text(13.5, 19.5, '紧迫性', ha='center', va='center', color=NAV,
        fontsize=13, fontweight='bold')
ax.text(86.5, 19.5, '合法性', ha='center', va='center', color=NAV,
        fontsize=13, fontweight='bold')


# ---------------------------------------------------------------- 区域归属自检
def region_of(x, y):
    """返回 (权力, 紧迫性, 合法性) 三布尔，表示该点落在哪些圆内。"""
    return tuple((x - cx) ** 2 + (y - cy) ** 2 < R ** 2 for cx, cy in (A, B, C))


# 名称、位置、期望集合归属（p=权力, u=紧迫性, l=合法性）
LABELS = [
    ('潜伏型', (50.0, 79.5), (1, 0, 0)),
    ('苛求型', (21.5, 33.5), (0, 1, 0)),
    ('自主型', (78.5, 33.5), (0, 0, 1)),
    ('危险型', (34.0, 56.5), (1, 1, 0)),
    ('支配型', (66.0, 56.5), (1, 0, 1)),
    ('依赖型', (50.0, 27.0), (0, 1, 1)),
    ('确定型', (50.0, 47.0), (1, 1, 1)),
]

for name, (x, y), want in LABELS:
    got = tuple(int(v) for v in region_of(x, y))
    assert got == want, f'{name} 位置串区：期望 {want}，实际 {got}'
    ax.text(x, y, name, ha='center', va='center', color=INK,
            fontsize=11.8, fontweight='bold', zorder=5)
    print(f'  {name:<4} @({x:4.1f},{y:4.1f}) → 权力{got[0]} 紧迫性{got[1]} 合法性{got[2]} ✓')

caption(ax, '图 9-3　凸显模型', fs=12, y=2.4)

print(f'  三圆等径 R={R}；圆心 权力{A} 紧迫性{B} 合法性{C}')
print('  ✅ 七个区域标签全部落在各自集合区域内')

save(fig, OUT)
