using Inspectron.HawkEye.Protocol;
using Newtonsoft.Json;

namespace Hawkeye.VisionBuilder.UI.Sources.Hawkeye;

public class UICameraSettings
{
    public UICameraSettings()
    {
    }

    public UICameraSettings(CameraSettings cameraSettings)
    {
        Shutter = cameraSettings.ImageSettings.Shutter;
        Gain = cameraSettings.ImageSettings.Gain;
        OffsetX = cameraSettings.OffsetX;
        SensorWidth = cameraSettings.ImageSettings.SensorWidth;
        Width = cameraSettings.ImageWidth;
        Lines = cameraSettings.ImageSettings.Lines;
        Divider = cameraSettings.ImageSettings.Divider;
        Light1 = cameraSettings.LightPwm1;
        Light2 = cameraSettings.LightPwm2;
        TriggerEnabled = cameraSettings.ImageSettings.UseExternalTrigger == 1;
        Name = cameraSettings.Name;
        CaptureBuffer = cameraSettings.ImageSettings.CaptureBuffer;
        RescaleWidth = cameraSettings.RescaleWidth;
        MinorCutoff = cameraSettings.MinorCutoff;
        BayerFilter = cameraSettings.BayerFilter;
        LaserTrigger = cameraSettings.LaserTrigger;
        LaserTriggerDelay = cameraSettings.LaserTriggerDelay;
        FlipLines = cameraSettings.FlipLines;
        TriggerLights = cameraSettings.TriggerLights;
        MirrorX = cameraSettings.MirrorX;
    }



    public CameraSettings ToCameraSettings()
    {
        return new CameraSettings()
        {
            ImageSettings = new ImageSettings()
            {
                Lines = Lines,
                CaptureBuffer = CaptureBuffer,
                Divider = Divider,
                UseExternalTrigger = TriggerEnabled ? 1 : 0,
                Gain = Gain,
                Shutter = Shutter,
                SensorWidth = SensorWidth


            },
            LightPwm1 = Light1,
            LightPwm2 = Light2,
            OffsetX = OffsetX,
            ImageWidth = Width,
            RescaleWidth = RescaleWidth,
            MinorCutoff = MinorCutoff,
            BayerFilter = BayerFilter,
            LaserTrigger = LaserTrigger,
            LaserTriggerDelay = LaserTriggerDelay,
            FlipLines = FlipLines,
            TriggerLights = TriggerLights,
            MirrorX = MirrorX
        };
    }

    public static UICameraSettings LoadSettingsLocal(string path)
    {
        return JsonConvert.DeserializeObject<UICameraSettings>(File.ReadAllText(path));
    }


    public int LaserTriggerDelay { get; set; }
    
    public bool LaserTrigger { get; set; }
    
    public bool TriggerEnabled { get; set; }

    
    public int CaptureBuffer { get; set; }
    
    public bool FlipLines { get; set; }
    
    public int Shutter { get; set; }
    
    public int Gain { get; set; }
    
    public int OffsetX { get; set; }
    
    public int SensorWidth { get; set; }
    
    public int Width { get; set; }
    
    public int RescaleWidth { get; set; }
    
    public int Lines { get; set; }
    
    public int Divider { get; set; }
    
    public int Light1 { get; set; }
    
    public int Light2 { get; set; }
    
    public bool TriggerLights { get; set; }

    public string Name { get; set; }

    
    public int MinorCutoff { get; set; }


    
    public bool BayerFilter { get; set; }

    
    public bool MirrorX { get; set; }
}