"""Tutorial 08b: Mesh and TurbidBoundary blending scene."""

from pathlib import Path

import less


output_dir = Path("tutorial_output")
output_dir.mkdir(exist_ok=True)

geometry = less.examples.asset_path("mixed_canopy.obj")
leaf_optics = less.Fluspect(
    cab=40.0, car=10.0, cw=0.012, cm=0.009, fqe=0.012)

plant = less.Object("mixed_plant")
plant.add_component(
    "crown",
    less.TurbidBoundary(
        geometry,
        group="crown",
        sampling=less.TurbidSampling(quality="standard", seed=7),
    ),
)
plant.add_component(
    "explicit_leaf", less.Mesh(geometry, group="explicit_leaf"))

plant.set_property(
    "crown",
    less.OpticalVegetation(
        leaf=leaf_optics,
        leaf_area_density=1.25,
        leaf_angle_distribution="spherical",
        leaf_size=(0.08, 0.04),
        leaf_shape="ellipse",
    ),
)
plant.set_property("explicit_leaf", leaf_optics)

for component in ("crown", "explicit_leaf"):
    plant.set_property(
        component,
        less.ThermalProperty(emissivity=0.98, temperature=300.0),
    )
    plant.set_property(
        component, less.Farquhar(Vcmax25=60.0, Jmax25=120.0))
    plant.set_property(
        component,
        less.BiophysicalProperty(
            leaf_width=0.04,
            stomatal_resistance=80.0,
            stomata_side="bottom",
        ),
    )

scene = less.Scene()
scene.size = (8.0, 4.0)
scene.terrain = less.Terrain(
    property=less.Lambertian(reflectance=0.12))
scene.terrain.set_property(
    less.ThermalProperty(temperature=300.0, emissivity=0.96))
scene.illumination = less.Illumination(source=less.Sun(zenith=25.0, azimuth=180.0), atmosphere=less.SimpleSpectralAtmosphere(turbidity=2.0))
scene.add(plant, positions=[(2.0, 2.0, 0.0), (6.0, 2.0, 0.0)])
projection = less.Orthographic(
    view_zenith=25.0,
    view_azimuth=180.0,
    image_size=128,
)

optical = scene.simulate(less.OpticalImager(
    projection, bands=[550, 680, 850], quality=64))
optical.to_brf().save(output_dir / "08b_turbid_brf.png")

microclimate = less.Microclimate(
    air_temperature=27.0,
    humidity=60.0,
    wind_speed=2.0,
)
state = scene.solve(
    microclimate,
    modules="all",
    photosynthesis_kwargs={"quality": "standard"},
    sif_kwargs={"quality": "standard"},
    seed=7,
)

photo = state.photosynthesis
for key, area, apar, assimilation in zip(
        photo.component_keys, photo.leaf_area, photo.APAR, photo.An):
    print(
        f"{key}: leaf_area={area:.3f} m2, "
        f"APAR={apar:.1f} umol/m2/s, "
        f"An={assimilation:.2f} umol/m2/s")

photo.save(output_dir / "08b_turbid_photosynthesis.csv")

sif_image = scene.simulate(less.SIFImager(
    projection,
    bands=[687, 740, 760],
    quality=256,
    sif_result=state.sif,
))
sif_image.save(output_dir / "08b_turbid_sif.tif")

thermal_image = scene.simulate(less.ThermalImager(
    projection,
    bands=[10000],
    quality=64,
    mode="precomputed",
    eb_result=state.energy_balance,
))
thermal_image.save(output_dir / "08b_turbid_thermal.tif")
