"""Extract sequence data for diagram."""
import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))  # sibling experiment modules
from sage.all import graphs  # type: ignore
from sage.misc.randstate import set_random_seed  # type: ignore
from plane_depth_sequencing import (
    quadrilateral_sequencing,
    _quad_vertices,
    _level_edge_of_face,
    _quad_type
)

set_random_seed(42)
g = graphs.RandomTriangulation(7)
g.is_planar(set_embedding=True)
embedding = g.get_embedding()
faces = g.faces(embedding)
outer_cycle = [u for u, _ in faces[0]]

result = quadrilateral_sequencing(g, outer_cycle)
sequence = result['sequence']
move_codes = result['move_codes']
depth_labelling = result['depth_labelling']

move_names = {0: "AD", 1: "LA", 2: "J", 3: "RC"}

print("sequence_data = [")
for i, quad in enumerate(sequence[:4]):
    quad_type = _quad_type(quad, depth_labelling)
    f1, f2 = list(quad)
    level_edge = _level_edge_of_face(f1, depth_labelling)
    e1, e2 = list(level_edge)
    a = next(v for v in f1 if v not in level_edge)
    b = next(v for v in f2 if v not in level_edge)
    ordered_verts = [e1, a, e2, b]
    move = move_names[move_codes[i-1]] if i > 0 else ""
    depths_str = [depth_labelling[v] for v in ordered_verts]
    print(f'    {{"vertices": {sorted(_quad_vertices(quad))}, "ordered": {ordered_verts}, "depths": {depths_str}, "move": "Q_{i+1}^{{{move}}}", "type": "{quad_type}"}},')
print("]")
