using System;

namespace StandardScene.Fass2Simulator
{
    public sealed class Fass2SimRuntime : IDisposable
    {
        private readonly Fass2SimUdpHost _host;

        public Fass2SimRuntime(Fass2SimConfig config, Fass2SimNodeProfileStore profiles)
        {
            Config = config;
            Vehicle = new Fass2SimVehicle(config);
            Actions = new Fass2SimActionEngine(Vehicle, config, profiles);
            Motion = new Fass2SimMotionEngine(Vehicle, config, Actions);
            Actions.StationActionsCompleted += Motion.OnStationActionsCompleted;
            var commandHandler = new Fass2SimCommandHandler(Vehicle, Motion, Actions, config.LogRawFrames);
            _host = new Fass2SimUdpHost(config, Vehicle, commandHandler, Motion, Actions);
            _host.StatusReported += (_, __) => StatusChanged?.Invoke();
        }

        public Fass2SimConfig Config { get; }

        public Fass2SimVehicle Vehicle { get; }

        public Fass2SimMotionEngine Motion { get; }

        public Fass2SimActionEngine Actions { get; }

        public bool IsRunning => _host.IsRunning;

        public event Action StatusChanged;

        public void Start()
        {
            _host.Start();
            StatusChanged?.Invoke();
        }

        public void Stop()
        {
            _host.Stop();
            StatusChanged?.Invoke();
        }

        public void Dispose()
        {
            _host.Dispose();
        }
    }
}
