"""For the bridge case where T'_ann = theta(1, p, q) has three faces,
illustrate how the tire annular face connector T'_{f'} (Definition 1.16)
depends on the choice of face f'.

We use theta(1, 3, 3) = C_6 + chord (v_0, v_3) as the smallest
example.  In the inherited planar embedding theta(1, 3, 3) has three
faces:

  Face A (outer/unbounded):  V(A) = {v_0, ..., v_5} (all 6 vertices).
  Face B (inner, right side):  V(B) = {v_0, v_1, v_2, v_3}.
  Face C (inner, left side):   V(C) = {v_0, v_3, v_4, v_5}.

In the surrounding maximal-planar G, each non-chord annular dual
vertex (v_1, v_2, v_4, v_5) has one external G'-neighbor (corresponding
to a non-annular face of G across a boundary edge of T).  The chord
endpoints v_0, v_3 have all three G'-edges inside T'_ann (= 2 cycle
edges + chord edge), so no external neighbors.

T'_{f'} is the closed G'-neighborhood of V(f') together with every
G'-edge incident to V(f').
"""
import math
import os
import matplotlib.pyplot as plt
import matplotlib.patches as patches


# Hexagon positions: v_0 at top, v_3 at bottom, so chord v_0-v_3 is vertical.
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


# External neighbors of v_1, v_2, v_4, v_5 (drawn radially outward).
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_panel(ax, vfp, title, show_external_for, face_shade=None,
               face_shade_color='#ffe9a1', external_labels=True):
    """Render one panel showing T'_ann (faint) with V(f') highlighted
    (dark) and T'_{f'} edges + external neighbors highlighted."""
    pos = hex_pos()
    ext = ext_pos()

    # Shade the chosen face (optional)
    if face_shade is not None:
        ax.add_patch(patches.Polygon(face_shade, closed=True,
                                     facecolor=face_shade_color,
                                     edgecolor='none', alpha=0.55, zorder=0))

    # Draw faint version of T'_ann (cycle + chord) underneath
    for u, v in CYCLE_EDGES:
        x1, y1 = pos[u]; x2, y2 = pos[v]
        ax.plot([x1, x2], [y1, y2], color='#cccccc', linewidth=2.0,
                zorder=1)
    x1, y1 = pos[CHORD[0]]; x2, y2 = pos[CHORD[1]]
    ax.plot([x1, x2], [y1, y2], color='#cccccc', linewidth=2.0,
            linestyle=(0, (3, 2)), zorder=1)

    # Highlight T'_{f'} edges: any G'-edge incident to V(f')
    highlight = '#1f77b4'
    for u, v in CYCLE_EDGES:
        if u in vfp or v in vfp:
            x1, y1 = pos[u]; x2, y2 = pos[v]
            ax.plot([x1, x2], [y1, y2], color=highlight, linewidth=3.2,
                    zorder=2)
    if CHORD[0] in vfp or CHORD[1] in vfp:
        x1, y1 = pos[CHORD[0]]; x2, y2 = pos[CHORD[1]]
        ax.plot([x1, x2], [y1, y2], color=highlight, linewidth=3.2,
                linestyle=(0, (3, 2)), zorder=2)
    # External edges from V(f') ∩ {v_1, v_2, v_4, v_5}
    for v in show_external_for:
        if v in vfp:
            x1, y1 = pos[v]; x2, y2 = ext[v]
            ax.plot([x1, x2], [y1, y2], color=highlight, linewidth=3.2,
                    zorder=2)

    # Draw vertices: V(f') dark, others light
    for v in range(6):
        x, y = pos[v]
        if v in vfp:
            ax.plot(x, y, 'o', color='#1f3f70', markersize=18, zorder=3)
            tcol = 'white'
        else:
            ax.plot(x, y, 'o', color='#bbbbbb', markersize=14, zorder=3)
            tcol = 'white'
        ax.annotate(f"$v_{v}$", (x, y), color=tcol, ha='center',
                    va='center', fontsize=9, fontweight='bold', zorder=4)

    # External neighbors (drawn only if they're added to T'_{f'})
    for v in show_external_for:
        if v in vfp:
            x, y = ext[v]
            ax.plot(x, y, 's', color='#d62728', markersize=12, zorder=3)
            if external_labels:
                ax.annotate(f"$u_{v}$", (x, y), color='white', ha='center',
                            va='center', fontsize=7, fontweight='bold',
                            zorder=4)

    ax.set_xlim(-2.2, 2.2)
    ax.set_ylim(-2.2, 2.2)
    ax.set_aspect('equal'); ax.axis('off')
    ax.set_title(title, fontsize=10)


def main():
    here = os.path.dirname(os.path.abspath(__file__))
    out = os.path.join(here, '..', 'notes', 'fig_facial_dual_choices.png')
    out = os.path.normpath(out)

    pos = hex_pos()

    fig, axes = plt.subplots(1, 3, figsize=(12.0, 4.6))

    # External neighbors only exist for non-chord vertices
    ext_candidates = {1, 2, 4, 5}

    # ---- Face A: outer (all 6 vertices) ----
    vA = {0, 1, 2, 3, 4, 5}
    # Outer face shading: a frame outside the hexagon.  Skip explicit shading.
    draw_panel(axes[0], vA,
               title=r"Face $A$ (outer): $V(A) = \{v_0,\dots,v_5\}$" + "\n" +
                     r"$T'_{f'=A}$: full $\theta(1,3,3)$ + 4 external neighbors",
               show_external_for=ext_candidates)

    # ---- Face B: inner right (v_0, v_1, v_2, v_3) ----
    vB = {0, 1, 2, 3}
    shadeB = [pos[i] for i in (0, 1, 2, 3)]
    draw_panel(axes[1], vB,
               title=r"Face $B$ (inner right): $V(B) = \{v_0,v_1,v_2,v_3\}$" + "\n"
                     + r"$T'_{f'=B}$: includes $v_4, v_5$ via $v_0v_5, v_3v_4$",
               show_external_for=ext_candidates,
               face_shade=shadeB)

    # ---- Face C: inner left (v_0, v_3, v_4, v_5) ----
    vC = {0, 3, 4, 5}
    shadeC = [pos[i] for i in (0, 5, 4, 3)]
    draw_panel(axes[2], vC,
               title=r"Face $C$ (inner left): $V(C) = \{v_0,v_3,v_4,v_5\}$" + "\n"
                     + r"$T'_{f'=C}$: mirror image of $B$",
               show_external_for=ext_candidates,
               face_shade=shadeC)

    fig.suptitle(r"Tire annular face connector $T'_{f'}$ for the bridge case " +
                 r"($T'_{\mathrm{ann}} = \theta(1,3,3)$, three faces $A,B,C$)" + "\n" +
                 r"Blue: edges of $T'_{f'}$.  Dark circles: $V(f')$.  " +
                 r"Red squares: external $G'$-neighbors $u_v$ included via $v \in V(f')$.",
                 fontsize=11, y=1.02)

    plt.tight_layout()
    plt.savefig(out, dpi=160, bbox_inches='tight')
    plt.close()
    print(f"wrote {out}")


if __name__ == '__main__':
    main()
