using Serilog;
using VisionBuilder.UI.Common;
using VisionBuilder.UI.Statistics;

namespace B24SiemensPlugin;

public class SiemensCameraModule: IVisionBuilderModule
{
    private readonly SingleCameraVM _singleCameraVm;
    private readonly B24SiemensSettings _settings;
    private readonly VisionBuilderStatistics _statistics;

    private SiemensServer _server;

    public SiemensCameraModule(SingleCameraVM singleCameraVm,B24SiemensSettings settings, VisionBuilderStatistics statistics)
    {
        _singleCameraVm = singleCameraVm;
        _settings = settings;
        _statistics = statistics;
    }

    public void InitializeModule()
    {
        _server = new SiemensServer();
        _server.OnPLCPacket += _server_OnPLCPacket;
        _server.Start();
    }
    string? GetMapping(int material)
    {
        var mapping = _settings.RecipeMappings.FirstOrDefault(x => x.MaterialNumber == material);
        if (mapping == null)
        {
            return null;
        }
        return mapping.RecipeName;
    }
    private PLCPacket _server_OnPLCPacket(PLCPacket packet)
    {
        if (_singleCameraVm.SynchronizationContext == null)
        {
            Log.Warning("Synchronization context is null, cannot update UI from PLC packet.");
            return packet;
        }
        packet.KameraStart= _singleCameraVm.IsRunning;
        var recipe = GetMapping(packet.Materialnummer);
        var selectedRecipe = _singleCameraVm.SelectedRecipe;
        if (recipe != null && recipe != selectedRecipe?.RecipeName)
        {
            var vm = _singleCameraVm.GetRecipeSelectionVm();
            vm.SelectedRecipe = vm.Recipes.FirstOrDefault(r => r.RecipeName == recipe);
            if (vm.SelectedRecipe != null)
            {
                _singleCameraVm.SynchronizationContext!.Post((_) =>
                {
                    _singleCameraVm.ProcessRecipeSelectionVm(vm);
                }, null);
                _statistics.Metadata = packet.Materialnummer.ToString();
            }
            Log.Information("Selected recipe {Recipe} for material number {MaterialNumber}", recipe, packet.Materialnummer);
            
        }
        //Log.Information("Lifebit: {Lifebit}, KameraStart: {KameraStart}", packet.Lifebit, packet.KameraStart);
        //Log.Information("Camera running: {IsRunning}", _singleCameraVm.IsRunning);
        //if (recipe != selectedRecipe?.RecipeName)
        //{
        //    if (packet.KameraStart && !_singleCameraVm.IsRunning)
        //    {
        //        Log.Information("Starting camera based on PLC packet.");
        //        _singleCameraVm.SynchronizationContext!.Post((o) =>
        //        {
        //            _ = _singleCameraVm.Start();
        //        }, null);
        //    }
        //    if (!packet.KameraStart && _singleCameraVm.IsRunning)
        //    {
        //        Log.Information("Stopping camera based on PLC packet.");
        //        _singleCameraVm.SynchronizationContext!.Post((o) =>
        //        {
        //            _ = _singleCameraVm.Stop();
        //        }, null);
        //    }
        //}
        

        return packet;

    }
}