"""FIRE result adapter: Flame/Smoke current objects on the existing Frame wire."""
from dataclasses import replace
from app.contracts.visualization import FrameResult, VisualizationCommand
from app.fire_config import FIRE_MODEL, FIRE_NAMES

FIRE_COLORS = ('#A855F7', '#FF3030')  # smoke violet, flame red


def evaluate_result(engine, frame, observations, *, camera_id=None, scenario_revision=None, gap_before=False):
    evaluation = engine.evaluate(frame, observations, gap_before=gap_before)
    if not evaluation.accepted:
        return None
    refs = {old: new for new, old in enumerate(evaluation.indices)}
    commands = tuple(
        VisualizationCommand(
            observations[i], FIRE_NAMES[observations[i].class_id],
            color=FIRE_COLORS[observations[i].class_id], visible=True,
            track_id=None, event_ids=(d.event_id,) if d.event_id else (),
            scenario=d)
        for i, d in zip(evaluation.indices, evaluation.decisions))
    events = tuple(replace(e, object_ref=str(refs[int(e.object_ref)]))
                   for e in evaluation.events)
    return FrameResult(
        frame, commands, events, engine.config, scenario_revision,
        provider_model=FIRE_MODEL, camera_id=camera_id)
