namespace HistoryView.Helper;
public class MonitorInfo
{
    public int Index { get; set; }

    public int StartPixVertical { get; set; }
    public int StartPixHorizontal { get; set; }

    public int ResolutionVertical { get; set; }
    public int ResolutionHorizontal { get; set; }
}

public partial class MonitorHelper : ObservableObject
{
    public MonitorHelper()
    {
        this.LoadScreensInfo();
    }

    [ObservableProperty]
    private int _SelectedDisplay = 1;

    [ObservableProperty]
    private ObservableDictionary<int, MonitorInfo> _Monitors = [];

    public void LoadScreensInfo()
    {
        List<MonitorInfo> list = [];
        for (int i = 1; i <= System.Windows.Forms.Screen.AllScreens.Length; i++)
        {
            System.Drawing.Rectangle rec = System.Windows.Forms.Screen.AllScreens[i - 1].Bounds;
            MonitorInfo mi = new()
            {
                Index = i,
                ResolutionHorizontal = rec.Width,
                ResolutionVertical = rec.Height,
                StartPixHorizontal = rec.X,
                StartPixVertical = rec.Y,
            };
            list.Add(mi);
        }


        foreach (var item in list.OrderBy(m => m.StartPixHorizontal))
            this.Monitors.Add(item.Index, item);

        if (!BaseConfigFileLoader.Load(out BasicInfo? basicInfo) ||
         basicInfo is null || !this.Monitors.ContainsKey(basicInfo.SelectedDisplay))
        {
            this.SelectedDisplay = this.Monitors.FirstOrDefault().Key;
            return;
        }

        this.SelectedDisplay = basicInfo.SelectedDisplay;
    }
}

