from __future__ import annotations

from typing import Literal

import numpy as np

from chemex.configuration.base import ExperimentConfiguration, ToBeFitted
from chemex.configuration.conditions import ConditionsWithValidations
from chemex.configuration.data import RelaxationDataSettings
from chemex.configuration.experiment import ExperimentSettings
from chemex.containers.data import Data
from chemex.containers.dataset import load_exsy_dataset
from chemex.experiments.factories import Creators, factories
from chemex.filterers import PlanesFilterer
from chemex.nmr.basis import Basis
from chemex.nmr.spectrometer import Spectrometer
from chemex.parameters.spin_system import SpinSystem
from chemex.plotters.exsy import EXSYPlotter
from chemex.printers.data import EXSYPrinter
from chemex.typing import Array

EXPERIMENT_NAME = "noesyfpgpph19"


class Noesyfpgpph19Settings(ExperimentSettings):
    name: Literal["noesyfpgpph19"]


class Noesyfpgpph19Config(
    ExperimentConfiguration[
        Noesyfpgpph19Settings,
        ConditionsWithValidations,
        RelaxationDataSettings,
    ],
):
    @property
    def to_be_fitted(self) -> ToBeFitted:
        return ToBeFitted(rates=["r1_i"], model_free=["tauc", "s2"])


def build_spectrometer(
    config: Noesyfpgpph19Config,
    spin_system: SpinSystem,
) -> Spectrometer:
    conditions = config.conditions

    basis = Basis(type="iz", spin_system="hn", model=config.model)
    return Spectrometer.from_spin_system(spin_system, basis, conditions)


class Noesyfpgpph19Sequence:
    """Sequence for NOESY-FPGP-PH19 experiment."""

    def __init__(self, settings: Noesyfpgpph19Settings) -> None:
        self.settings = settings

    def calculate(self, spectrometer: Spectrometer, data: Data) -> Array:
        times = data.metadata["times"]
        states1 = data.metadata["states1"]
        states2 = data.metadata["states2"]

        # Getting the starting magnetization
        equilibrium = spectrometer.get_equilibrium()

        # Calculate delay propagators
        delays = spectrometer.delays(times)

        intensities: list[float] = []
        for state1, state2, delay in zip(states1, states2, delays, strict=False):
            start = spectrometer.keep(equilibrium, [f"iz_{state1}"])
            spectrometer.detection = f"[iz_{state2}]"
            intensities.append(spectrometer.detect(delay @ start))

        return np.array(intensities)

    def is_reference(self, metadata: Array) -> Array:
        return np.full_like(metadata, fill_value=False, dtype=np.bool_)


def register() -> None:
    creators = Creators(
        config_creator=Noesyfpgpph19Config,
        spectrometer_creator=build_spectrometer,
        sequence_creator=Noesyfpgpph19Sequence,
        dataset_creator=load_exsy_dataset,
        filterer_creator=PlanesFilterer,
        printer_creator=EXSYPrinter,
        plotter_creator=EXSYPlotter,
    )
    factories.register(name=EXPERIMENT_NAME, creators=creators)
