"""Two-panel figure for fiber_decomposition.tex:
LEFT  -- the core T'_ann = theta(1,3,3) alone (what menagerie sec.6 counts).
RIGHT -- the face connector T'_{f'} = core + 4 spoke pendants (what we want)."""
import math
import os
import matplotlib.pyplot as plt


def hex_pos(scale=1.0):
    pos = {}
    for i in range(6):
        angle = math.pi / 2 - 2 * math.pi * i / 6
        pos[i] = (scale * math.cos(angle), scale * math.sin(angle))
    return pos


def ext_pos(scale=1.0, radial=1.8):
    pos = hex_pos(scale)
    ext = {}
    for v in (1, 2, 4, 5):
        x, y = pos[v]
        nx, ny = x / math.hypot(x, y), y / math.hypot(x, y)
        ext[v] = (radial * nx, radial * ny)
    return ext


CYCLE_EDGES = [(i, (i + 1) % 6) for i in range(6)]
CHORD = (0, 3)


def draw_core(ax):
    pos = hex_pos()
    # cycle
    for u, v in CYCLE_EDGES:
        x1, y1 = pos[u]; x2, y2 = pos[v]
        ax.plot([x1, x2], [y1, y2], color='#1f3f70', linewidth=3.0, zorder=1)
    # chord
    x1, y1 = pos[CHORD[0]]; x2, y2 = pos[CHORD[1]]
    ax.plot([x1, x2], [y1, y2], color='#1f3f70', linewidth=3.0,
            linestyle=(0, (3, 2)), zorder=1)
    # vertices
    for v, (x, y) in pos.items():
        ax.plot(x, y, 'o', color='#1f3f70', markersize=16, zorder=2)
        ax.annotate(f"$v_{v}$", (x, y), color='white', ha='center',
                    va='center', fontsize=9, fontweight='bold', zorder=3)
    ax.set_xlim(-2.2, 2.2)
    ax.set_ylim(-2.2, 2.2)
    ax.set_aspect('equal'); ax.axis('off')
    ax.set_title(r"Core $T'_{\mathrm{ann}} = \theta(1,3,3)$" + "\n" +
                 r"(menagerie \S6 counts this)", fontsize=10)


def draw_connector(ax):
    pos = hex_pos()
    ext = ext_pos()
    # core (kept dark blue)
    for u, v in CYCLE_EDGES:
        x1, y1 = pos[u]; x2, y2 = pos[v]
        ax.plot([x1, x2], [y1, y2], color='#1f3f70', linewidth=3.0, zorder=1)
    x1, y1 = pos[CHORD[0]]; x2, y2 = pos[CHORD[1]]
    ax.plot([x1, x2], [y1, y2], color='#1f3f70', linewidth=3.0,
            linestyle=(0, (3, 2)), zorder=1)
    # spoke edges (orange to stand out)
    for v in (1, 2, 4, 5):
        x1, y1 = pos[v]; x2, y2 = ext[v]
        ax.plot([x1, x2], [y1, y2], color='#e08000', linewidth=3.0, zorder=1)
    # cycle vertices
    for v, (x, y) in pos.items():
        ax.plot(x, y, 'o', color='#1f3f70', markersize=16, zorder=2)
        ax.annotate(f"$v_{v}$", (x, y), color='white', ha='center',
                    va='center', fontsize=9, fontweight='bold', zorder=3)
    # spoke vertices
    for v in (1, 2, 4, 5):
        x, y = ext[v]
        ax.plot(x, y, 's', color='#e08000', markersize=14, zorder=2)
        ax.annotate(f"$u_{v}$", (x, y), color='white', ha='center',
                    va='center', fontsize=8, fontweight='bold', zorder=3)
    ax.set_xlim(-2.4, 2.4)
    ax.set_ylim(-2.4, 2.4)
    ax.set_aspect('equal'); ax.axis('off')
    ax.set_title(r"Connector $T'_{f'}$ = core + 4 spoke pendants" + "\n" +
                 r"(spoke edges in orange; this is what we actually want)",
                 fontsize=10)


def main():
    here = os.path.dirname(os.path.abspath(__file__))
    out = os.path.join(here, 'fig_fiber_setup.png')
    fig, axes = plt.subplots(1, 2, figsize=(11.0, 5.0))
    draw_core(axes[0])
    draw_connector(axes[1])
    plt.tight_layout()
    plt.savefig(out, dpi=160, bbox_inches='tight')
    plt.close()
    print(f"wrote {out}")


if __name__ == '__main__':
    main()
