low_implications = {1:[], 2:[1, 3, 4, 5, 8, 9, 10, 11, 13, 14, 16, 23, 24, 25, 26, 28, 31, 38, 39, 40, 41, 43], 3:[1, 8, 23], 4:[1, 3, 8, 9, 10, 11, 23, 24, 25, 26, 38], 5:[1, 3, 8, 10, 13, 16, 23, 25, 28, 31, 39], 8:[1], 9:[1, 3, 8, 23], 10:[1, 8], 11:[1, 8], 13:[1, 8, 10, 16, 39], 14:[1, 8, 23], 16:[1, 8], 23:[1], 24:[1, 23, 25, 26, 38], 25:[1, 23], 26:[1, 23], 28:[1, 3, 8, 23], 31:[1, 23], 38:[1], 39:[1], 40:[1], 41:[1, 38, 39, 40, 43], 43:[1]}

from find_equation_id import Equation
import networkx as nx
DG = nx.DiGraph()
DG.add_edges_from(set([(i, j) for i, l in low_implications.items() for j in l]))
DGred = nx.transitive_reduction(DG)

pos = {
    1: (-4,18),
    2: (0, 2),
    3: (0,15),
    4: (-4, 3),
    5: (4, 3),
    8: (-2,16),
    9: (-3.5,12),
    10: (-7, 12),
    11: (-8.7,14),
    13: (8, 4),
    14: (0, 8),
    16: (2, 9),
    23: (2,16),
    24: (-8, 4),
    25: (7, 12),
    26: (-2, 9),
    28: (3.5,12),
    31: (8.7,14),
    38: (-17,5.5),
    39: (-13,5.5),
    40: (-16.1,15),
    41: (-15, 3),
    43: (-14, 9),
}
edge_command = {
(14, 8): "to [bend right=10]",
(14, 23): "to [bend left=10]",
(4, 11): "to [bend left=25]",
(5, 31): "to [bend right=25]",
(4, 9): "to [bend left=5]",
(5, 28): "to [bend right=5]",
(13, 10): "to [bend left=20]",
(24, 25): "to [bend right=20]",
(10, 8): "to [bend left=30]",
(25, 23): "to [bend right=32]",
(11, 8): "to [bend left=15]",
(31, 23): "to [bend right=15]",
(38, 1): ".. controls (-19,18.5) and (-19,16) ..",
(43, 1): ".. controls (-12,15) and (-12,15) ..",
(39, 1): ".. controls (-11.5,14) and (-12,14) ..",
}

for i in DGred:
    eqi = str(Equation.from_id(i)).replace('◇', '\\op')
    if Equation.from_id(i).dual().id == i:
        label = f"\\underline{{{i}}}"
    else:
        label = str(i)
    print(f'\\node(eq{i})[draw, rounded corners] at {pos[i][:2]} {{${eqi}$ ({label})}};')

for k in DGred:
    for j in DGred[k]:
        ec = edge_command.get((k, j), "--")
        print(f'\\draw[-{{stealth}}](eq{k}){ec}(eq{j});')


'''
{1, 0, "Vacuum"},
{3, 14, "Vacuum"},
{8, 1, "Vacuum"},
{11, 2, "Vacuum"},
{23, 1, "Vacuum"},
{31, 2, "Vacuum"},
{40, 3, "Vacuum"},
{9, 15, 0},
{10, 5, 0},
{25, 5, 0},
{28, 15, 0},
{26, 9, 1/2},
{16, 9, 1/2},
{43, 9, 1/2},
{14, 29, 2/3},
{38, 54, "Solid"},
{39, 54, "Solid"},
{2, 66, "Crystal"},
{4, 64, "Crystal"},
{5, 64, "Crystal"},
{13, 62, "Crystal"},
{24, 62, "Crystal"},
{41, 62, "Crystal"}
'''

