# -*- coding: utf-8 -*-
"""图 9-5　基本沟通模型 —— 教材第 218 页

逐项对照图内要素：
· 左右两个大矩形主体框：左框靠左居中标「发送方」，右框靠右居中标「接收方」；
· 左框内上下两个小矩形：上「编码」、下「解码」，两框之间一条竖直箭头（解码→编码）；
· 右框内上下两个小矩形：上「解码」、下「编码」；
· 三条水平带箭头实线构成闭合回路：
  ① 左编码 → 右解码（自左向右），线上方偏左标「传递信息」，中部偏右有闪电噪声符号，
     符号上方标「噪声」；
  ② 右解码 → 左编码（自右向左），线下方偏右标「确认信息」，中部偏左有噪声符号，
     符号左下标「噪声」；
  ③ 右编码 → 左解码（自右向左），线下方偏右标「反馈信息」，中部偏左有噪声符号，
     符号下标「噪声」；
· 两框间隙正中偏下画横向椭圆，内标「媒介」，不与任何箭头相连；
· 图题居中于框图正下方。
"""
import os
import sys

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

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

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'assets',
                   '图9-5-基本沟通模型.png')


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


def arw(ax, p1, p2, color=ACC, lw=1.8, z=6):
    """带箭头的实线（zorder 高于白底框，保证箭头不被框填充盖住）。"""
    ax.add_patch(FancyArrowPatch(p1, p2, arrowstyle='-|>', mutation_scale=16,
                                 linewidth=lw, color=color, shrinkA=0, shrinkB=0,
                                 zorder=z))


def noise(ax, x, y, h=6.2, w=2.5):
    """闪电状噪声符号：白填充＋蓝描边的闪电多边形，压在箭线上，避开文字。"""
    pts = [(0.55 * w, h / 2), (-0.62 * w, 0.12 * h), (-0.06 * w, 0.12 * h),
           (-0.55 * w, -h / 2), (0.62 * w, -0.10 * h), (0.06 * w, -0.10 * h)]
    ax.add_patch(Polygon([(x + px, y + py) for px, py in pts], closed=True,
                         facecolor='white', edgecolor=ACC, linewidth=1.6,
                         joinstyle='miter', zorder=7))


def main():
    setup()
    fig, ax = canvas(12.0, 8.7, xlim=(0, 100), ylim=(0, 72))

    # ════ ① 两个主体框 ════
    rect(ax, 3, 8, 37, 66, fc='white', ec=NAV, lw=1.3)
    rect(ax, 63, 8, 97, 66, fc='white', ec=NAV, lw=1.3)
    ax.text(10.5, 37, '发送方', fontsize=13, ha='center', va='center',
            color=NAV, fontweight='bold')
    ax.text(89.5, 37, '接收方', fontsize=13, ha='center', va='center',
            color=NAV, fontweight='bold')

    # ════ ② 四个小框 ════
    rect(ax, 20, 44, 34, 60, fc=FILL, ec=NAV, lw=1.0)      # 左·编码
    rect(ax, 20, 18, 34, 34, fc=FILL, ec=NAV, lw=1.0)      # 左·解码
    rect(ax, 66, 44, 80, 60, fc=FILL, ec=NAV, lw=1.0)      # 右·解码
    rect(ax, 66, 18, 80, 34, fc=FILL, ec=NAV, lw=1.0)      # 右·编码
    ax.text(27, 52, '编码', fontsize=11.5, ha='center', va='center', color=INK)
    ax.text(27, 26, '解码', fontsize=11.5, ha='center', va='center', color=INK)
    ax.text(73, 52, '解码', fontsize=11.5, ha='center', va='center', color=INK)
    ax.text(73, 26, '编码', fontsize=11.5, ha='center', va='center', color=INK)

    # ════ ③ 左框内的竖直箭头：解码 → 编码 ════
    arw(ax, (27, 34.3), (27, 43.7), lw=1.9)

    # ════ ④ 三条水平箭头 ════
    arw(ax, (34.3, 56.0), (65.7, 56.0))     # 传递信息  →
    arw(ax, (65.7, 47.5), (34.3, 47.5))     # 确认信息  ←
    arw(ax, (65.7, 25.0), (34.3, 25.0))     # 反馈信息  ←

    ax.text(39.5, 58.6, '传递信息', fontsize=10.5, ha='left', va='center', color=INK)
    ax.text(52.0, 44.7, '确认信息', fontsize=10.5, ha='left', va='center', color=INK)
    ax.text(52.0, 22.2, '反馈信息', fontsize=10.5, ha='left', va='center', color=INK)

    # ════ ⑤ 三处噪声符号 ════
    noise(ax, 57.5, 56.0)
    ax.text(57.5, 62.0, '噪声', fontsize=9.5, ha='center', va='center', color=INK)

    noise(ax, 42.5, 47.5)
    ax.text(42.5, 41.4, '噪声', fontsize=9.5, ha='center', va='center', color=INK)

    noise(ax, 42.5, 25.0)
    ax.text(42.5, 18.9, '噪声', fontsize=9.5, ha='center', va='center', color=INK)

    # ════ ⑥ 媒介椭圆（间隙正中偏下，不与箭头相连）════
    ax.add_patch(Ellipse((50, 11.5), 16, 7.6, facecolor=FILL, edgecolor=LINE,
                         linewidth=1.2, zorder=3))
    ax.text(50, 11.5, '媒介', fontsize=11, ha='center', va='center', color=NAV, zorder=4)

    # ════ 图题 ════
    caption(ax, '图 9-5　基本沟通模型', fs=10.5, y=1.2)

    save(fig, OUT)


if __name__ == '__main__':
    main()
