use kalast::{
    EnvironmentSystem, Properties, SingleBody, Time, World, ASTRONAUMICAL_UNIT, HOUR, M3, MINUTE,
    V3,
};

fn main() {
    // [OPTIONAL] To enable log information.
    kalast::log_init!();

    // Define an empty object.
    let mut asteroid = SingleBody::empty_object(
        "Dimorphos",                                          // name
        -V3::new(1.0, 0.0, 0.0) * ASTRONAUMICAL_UNIT * 1.074, // position ICRS Sun-centered
        Properties::new(
            11.92 * HOUR, // rotation period
            11.92 * HOUR, // revolution period
            0.,           // obliquity
            500.0,        // thermal inertia
            2146.0,       // density
            600.0,        // heat capacity
            0.07,         // albedo
            0.9,          // emissivity
        ),
    );

    // Add a single facet at longitude = latitude = 0° of 2m² surface area.
    asteroid.body_mut().new_face(&M3::from_column_slice(&[
        1.0,       // vertex 1 x
        -2. / 3.,  //          y
        2. / 3.,   //          z
        1.0,       // vertex 2 x
        -2.0 / 3., //          y
        -4. / 3.,  //          z
        1.0,       // vertex 3 x
        4. / 3.,   //          y
        2. / 3.,   //          z
    ]));
    // Complete model = false ==>> no mutual nor self heating.
    asteroid.complete_model(false);

    // Time
    let time = Time::new(
        10.0 * asteroid.body().properties.revolution_period(), // duration
        10.0 * MINUTE,                                         // time step
    );

    // Initialize the World.
    let mut world = World::new(time, asteroid);
    // Run the simulation (time loop here).
    world.start();
    // To save the daily temperature in text file.
    world.save_daily("rsc/data/tmp.txt");

    // Some prints to know the temperature at the surface and deepest temperature.
    // Taking at index=0 because there is only one face.
    println!(
        "min: {:.2}, max: {:.2}",
        world.environment_system.body().surface_temperatures()[0],
        world.environment_system.body().deepest_temperatures()[0]
    );

    // Convert ground vector and ground temperatures as slice for plotting.
    let ground = world
        .environment_system
        .body()
        .properties
        .ground_vector()
        .as_slice();
    let temperatures = world
        .environment_system
        .body()
        .ground_temperatures()
        .as_slice();

    // Initialize the graph.
    let graph = kalast::Graph::new(1, 1);
    // Tweaking the graph.
    graph.set_title("Daily ground temperatures");
    graph.set_xlabel(0, "Depth [m]");
    graph.set_ylabel(0, "Temperature [K]");
    graph.set_axis_limits(0, Some(0.), Some(1.1), Some(260.), Some(360.));
    graph.set_legend_title(0, "Hour angle [deg]");
    // Plot ground temperature VS ground depth.
    graph.plot(
        0,
        kalast::args!(graph.py(), ground, temperatures),
        kalast::kwargs!(graph.py(), ("label", "0")),
    );
    // Finalize the graph.
    graph.save("rsc/img/tmp.svg", None);
    graph.show(true);
}
