"""Draw theta(1, 3, 3) = hexagon with one chord, illustrating section 6
of the menagerie.  Shows a proper 3-edge-coloring with the two
trivalent vertices highlighted."""
import math
import os
import matplotlib.pyplot as plt


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

    # Hexagon: 6 cycle vertices in regular hexagon, with v_0 at top and v_3 at bottom
    # so the chord v_0-v_3 is the vertical diameter.
    n = 6
    pos = {}
    for i in range(n):
        angle = math.pi / 2 - 2 * math.pi * i / n
        pos[i] = (math.cos(angle), math.sin(angle))

    cycle_edges = [(i, (i + 1) % n) for i in range(n)]
    chord = (0, 3)

    # Proper 3-edge-coloring:
    # chord = color C, e_0 = A, e_1 = C, e_2 = B, e_3 = A, e_4 = C, e_5 = B
    A = '#1f77b4'   # blue
    B = '#d62728'   # red
    C = '#2ca02c'   # green

    cycle_colors = [A, C, B, A, C, B]
    chord_color = C

    fig, ax = plt.subplots(figsize=(4.5, 4.0))

    # Draw cycle edges
    for i, (u, v) in enumerate(cycle_edges):
        x1, y1 = pos[u]; x2, y2 = pos[v]
        ax.plot([x1, x2], [y1, y2], color=cycle_colors[i], linewidth=3.0,
                solid_capstyle='round', zorder=1)

    # Draw chord (slightly dashed to distinguish from polygon edges)
    x1, y1 = pos[chord[0]]; x2, y2 = pos[chord[1]]
    ax.plot([x1, x2], [y1, y2], color=chord_color, linewidth=3.0,
            linestyle=(0, (3, 2)), solid_capstyle='round', zorder=1)

    # Draw vertices, highlighting trivalent vertices in a darker shade
    trivalent = {0, 3}
    for v, (x, y) in pos.items():
        if v in trivalent:
            ax.plot(x, y, 'o', color='#333333', markersize=22, zorder=2)
            label_color = 'white'; fontsize = 11
        else:
            ax.plot(x, y, 'o', color='#888888', markersize=18, zorder=2)
            label_color = 'white'; fontsize = 10
        ax.annotate(f"$v_{v}$", (x, y), color=label_color, ha='center',
                    va='center', fontsize=fontsize, fontweight='bold', zorder=3)

    # Annotations: indicate the 3 paths and the chord
    ax.annotate('chord (path of length 1)',
                xy=(0, 0), xytext=(1.65, 0.05),
                fontsize=9, color=chord_color,
                arrowprops=dict(arrowstyle='->', color=chord_color, lw=1.2))
    ax.annotate('right path (length 3)',
                xy=(1.0, -0.05), xytext=(1.6, -0.75),
                fontsize=9, color='#444',
                arrowprops=dict(arrowstyle='->', color='#444', lw=1.0))
    ax.annotate('left path (length 3)',
                xy=(-1.0, -0.05), xytext=(-2.45, -0.75),
                fontsize=9, color='#444',
                arrowprops=dict(arrowstyle='->', color='#444', lw=1.0))

    # Legend for edge colors
    legend_items = [
        plt.Line2D([], [], color=A, linewidth=3, label='color $a$'),
        plt.Line2D([], [], color=B, linewidth=3, label='color $b$'),
        plt.Line2D([], [], color=C, linewidth=3, label='color $c$'),
        plt.Line2D([], [], marker='o', color='w', markerfacecolor='#333333',
                   markersize=12,
                   label='trivalent (degree-3) vertex'),
    ]
    ax.legend(handles=legend_items, loc='upper left',
              bbox_to_anchor=(1.02, 1.0), fontsize=9, frameon=False)

    ax.set_xlim(-2.6, 2.7)
    ax.set_ylim(-1.4, 1.4)
    ax.set_aspect('equal'); ax.axis('off')
    ax.set_title(r'$\theta(1, 3, 3)$: hexagon $C_6$ with one chord' + '\n' +
                 r'(2-connected outerplanar, $\Delta = 3$, $P_e(\theta(1,3,3),3)=30$)',
                 fontsize=11)

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


if __name__ == '__main__':
    main()
