using Crestron.SimplSharp;
using Crestron.SimplSharpPro;
using CrestronOpenCvSharp.Capture;
using Directory = Crestron.SimplSharp.CrestronIO.Directory;
using Path = Crestron.SimplSharp.CrestronIO.Path;

namespace CrestronOpenCvSharp;

public class ControlSystem : CrestronControlSystem
{
    private FacialRecognition? _facialRecognition;
    private MjpegCapture? _capture;
    private UiHandler? _uiHandler;
    
    // Default snapshot URL of a Bosch camera
    private const string Url = "http://192.168.1.221/snap.jpg";
    private readonly string? _directory;
    
    public ControlSystem() : base()
    {
        try
        {
            Crestron.SimplSharpPro.CrestronThread.Thread.MaxNumberOfUserThreads = 20;
            _directory = Path.Combine(Directory.GetApplicationRootDirectory(), $"html");
            Console.WriteLine($"Home directory = {_directory}");
        }
        catch (Exception e)
        {
            ErrorLog.Error("Error in the constructor: {0}", e.Message);
        }
    }
    
    public override void InitializeSystem()
    {
        try
        {
            _facialRecognition = new FacialRecognition(_directory);
            _capture = new MjpegCapture(Url, _directory, _facialRecognition);
            _uiHandler = new UiHandler(_capture);
            
            _capture.StartCapture(5000);
        }
        catch (Exception e)
        {
            ErrorLog.Error("Error in InitializeSystem: {0}", e.Message);
        }
    }
}