"""Draw C_8 with three non-crossing chords {(0,2), (3,7), (4,6)}, and
overlay the unique proper 3-edge-coloring (modulo permutation of the 3
colors)."""
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_3chord_example.png')

    n = 8
    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)]
    chords = [(0, 2), (3, 7), (4, 6)]

    # Coloring (derived in the menagerie note):
    # all 3 chords = color "a" (call it color 1).
    # cycle alternates "b", "c": e_0=b, e_1=c, e_2=b, e_3=c, e_4=b, e_5=c, e_6=b, e_7=c.
    A = '#1f77b4'   # color a (used by chords)
    B = '#d62728'   # color b
    C = '#2ca02c'   # color c

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

    fig, ax = plt.subplots(figsize=(5.4, 4.6))

    # 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)

    # Chords (dashed to distinguish)
    for u, v in chords:
        x1, y1 = pos[u]; x2, y2 = pos[v]
        ax.plot([x1, x2], [y1, y2], color=chord_color, linewidth=3.0,
                linestyle=(0, (3, 2)), solid_capstyle='round', zorder=1)

    # Vertices: chord endpoints (degree 3) darker; others (degree 2) lighter
    chord_verts = {v for c in chords for v in c}
    for v, (x, y) in pos.items():
        if v in chord_verts:
            ax.plot(x, y, 'o', color='#333333', markersize=22, zorder=2)
            ax.annotate(f"$v_{v}$", (x, y), color='white', ha='center',
                        va='center', fontsize=11, fontweight='bold', zorder=3)
        else:
            ax.plot(x, y, 'o', color='#888888', markersize=18, zorder=2)
            ax.annotate(f"$v_{v}$", (x, y), color='white', ha='center',
                        va='center', fontsize=10, fontweight='bold', zorder=3)

    legend_items = [
        plt.Line2D([], [], color=A, linewidth=3, linestyle='--',
                   label='chord, color $a$'),
        plt.Line2D([], [], color=B, linewidth=3, label='cycle, color $b$'),
        plt.Line2D([], [], color=C, linewidth=3, label='cycle, color $c$'),
        plt.Line2D([], [], marker='o', color='w', markerfacecolor='#333333',
                   markersize=12, label='chord-endpoint (degree 3)'),
        plt.Line2D([], [], marker='o', color='w', markerfacecolor='#888888',
                   markersize=11, label='polygon-only vertex (degree 2)'),
    ]
    ax.legend(handles=legend_items, loc='upper left',
              bbox_to_anchor=(1.02, 1.0), fontsize=9, frameon=False)

    ax.set_xlim(-1.4, 2.4)
    ax.set_ylim(-1.3, 1.3)
    ax.set_aspect('equal'); ax.axis('off')
    ax.set_title(r'$C_8$ with 3 non-crossing chords $\{02,\, 37,\, 46\}$'
                 + '\n'
                 + r'($P_e(G,3) = 6$; unique coloring up to permutation '
                   r'of $\{a,b,c\}$)',
                 fontsize=11)

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


if __name__ == '__main__':
    main()
