#!/usr/bin/env python3

import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch, Rectangle

plt.rcParams.update({
    "font.family": "serif",
    "font.size": 12
})


def draw_box(ax, text, x, y, w=0.26, h=0.14):

    rect = Rectangle((x,y), w, h, fill=False, linewidth=1.8)
    ax.add_patch(rect)

    ax.text(
        x + w/2,
        y + h/2,
        text,
        ha="center",
        va="center"
    )


def arrow(ax, p1, p2):

    arr = FancyArrowPatch(
        p1, p2,
        arrowstyle='->',
        mutation_scale=18,
        linewidth=1.6
    )

    ax.add_patch(arr)


def generate_mtde_coupling():

    fig, ax = plt.subplots(figsize=(8,6))
    ax.axis("off")

    # core chain
    draw_box(ax,"Mantle Convection\n(Long-Wavelength Flow)",0.37,0.80)

    draw_box(ax,"Mass Redistribution\nin Mantle",0.37,0.62)

    draw_box(ax,"Inertia Tensor\nEvolution",0.37,0.44)

    draw_box(ax,"True Polar Wander\nSensitivity",0.37,0.26)

    draw_box(ax,"MTDE Threshold\nCrossing",0.37,0.08)


    # arrows
    arrow(ax,(0.50,0.80),(0.50,0.70))
    arrow(ax,(0.50,0.62),(0.50,0.52))
    arrow(ax,(0.50,0.44),(0.50,0.34))
    arrow(ax,(0.50,0.26),(0.50,0.16))


    # side process: magnetic variability

    draw_box(ax,"Geomagnetic\nInstability",0.08,0.26)

    arrow(ax,(0.34,0.33),(0.37,0.33))


    # final amplification

    draw_box(ax,"Climate / Carbon\nAmplification",0.74,0.08)

    arrow(ax,(0.63,0.15),(0.74,0.15))


    ax.set_title(
        "Global Dynamical Coupling in the MTDE Framework",
        pad=20
    )

    plt.tight_layout()

    plt.savefig("mtde_global_coupling.pdf")
    plt.close()


if __name__ == "__main__":

    generate_mtde_coupling()

    print("Generated mtde_global_coupling.pdf")