using System;

namespace EEMSMain.ViewModels;

public partial class MainWindowViewModel : ObservableObject
{
    public MainWindowViewModel(ICommonContainer commonContainer, ContainerManager containerManager, DeviceCollection deviceCollection)
    {
        this._commonContainer = commonContainer;
        this.ContainerManager = containerManager;
        this._deviceCollection = deviceCollection;
        containerManager.Containers.CollectionChanged += Containers_CollectionChanged;
        this.FakeData();
    }

    private void Containers_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (sender is not ObservableDictionary<int, ContainerInfo> t)
            return;

        ContainerInfo? containerInfo = t.Values.FirstOrDefault(x => x.IsDefault);
        if (containerInfo is null)
            return;

        this.Open(containerInfo);
    }

    private readonly ICommonContainer _commonContainer;
    public ContainerManager ContainerManager { get; }

    private readonly DeviceCollection _deviceCollection;

    void FakeData()
    {
        Random r = new();
        for (int i = 1; i <= 6; i++)
        {
            Guid guid = Guid.NewGuid();
            DeviceInfo_VM device = new()
            {
                DeviceModel = DeviceModel.JetKepler,
                DeviceSubModel = KeplerSubModel.JetKepler_2200A.ToString(),
                DeviceName = $"Device {i}",
                Position = $"position-{i}",
                SoftwareVersion = "1.0.0.0",
                IP = $"192.168.250.{i}",
                Port = 50002,
                UpdateTime = DateTime.Now,
                Guid = guid,
                IsConnected = Convert.ToBoolean(r.Next(0, 2))
            };

            RecipeInfo_VM recipe = new()
            {
                DeviceId = guid,
                CurrentStepName = "Step 1",
                NextStepName = "Step 2",
                CurrentStepTotalTime = r.Next(50, 100),
                CurrentStepRemainTime = r.Next(0, 50),
                TotalTime = r.Next(200, 400),
                TotalRemainTime = r.Next(100, 200),
            };

            //recipe.RecipeInfo["PJobID"] = "TestPJob";
            //recipe.RecipeInfo["CJboID"] = "TestCJob";
            //recipe.RecipeInfo["RecipeType"] = (RecipeType)r.Next(0, 8);

            DeviceData_VM deviceData = new()
            {
                DeviceId = guid,
                DeviceModel = DeviceModel.JetKepler,
                DeviceStatus = ((DeviceStatus)r.Next(0, 12)).ToString(),
                RecipeInfo = recipe,
                Alarms = []
            };
            deviceData.OtherInfo.Add("子型号", KeplerSubModel.JetKepler_2200A);

            //deviceData.OtherInfo["PMC Mode"] = PMCMode.Running.ToString();

            if (r.Next(1, 4) > 1)
            {
                for (int j = 0; j < r.Next(1, 5); j++)
                {
                    Alarm_VM alarm = new()
                    {
                        DeviceId = guid,
                        AlarmCode = r.Next(10000, 90000).ToString(),
                        AlarmTime = DateTime.Now,
                        AlarmName = $"Test Alarm {j}"
                    };
                    deviceData.Alarms.Add(alarm);
                }
            }

            _deviceCollection.DeviceDataList.Add(guid, deviceData);
            _deviceCollection.DeviceList.Add(guid, device);
        }


        for (int i = 1; i <= 3; i++)
        {
            Guid guid = Guid.NewGuid();

            DeviceInfo_VM device = new()
            {
                DeviceModel = DeviceModel.Proxima,
                DeviceSubModel = ProximaSubModel.Proxima_ELK.ToString(),
                DeviceName = $"Device {i}",
                Position = $"position-{i}",
                SoftwareVersion = "1.0.0.0",
                IP = $"192.168.250.{i}",
                Port = 50002,
                UpdateTime = DateTime.Now,
                Guid = guid,
                IsConnected = Convert.ToBoolean(r.Next(0, 2))
            };

            RecipeInfo_VM recipe = new()
            {
                DeviceId = guid,
                CurrentStepName = "Step 1",
                NextStepName = "Step 2",
                CurrentStepTotalTime = r.Next(50, 100),
                CurrentStepRemainTime = r.Next(0, 50),
                TotalTime = r.Next(200, 400),
                TotalRemainTime = r.Next(100, 200),
            };
            recipe.RecipeInfo.Add("PJobID", "TestPJob");
            recipe.RecipeInfo.Add("CJboID", "TestCJob");
            recipe.RecipeInfo.Add("RecipeType", (RecipeType)r.Next(0, 8));
            DeviceData_VM deviceData = new()
            {
                DeviceId = guid,
                DeviceModel = DeviceModel.Proxima,
                DeviceStatus = ((DeviceStatus)r.Next(0, 12)).ToString(),
                //PMCMode = PMCMode.Running,
                RecipeInfo = recipe,
                Alarms = []
            };
            deviceData.OtherInfo.Add("子型号", ProximaSubModel.Proxima_ELK);
            deviceData.OtherInfo.Add(nameof(PMCMode), PMCMode.Running);

            _deviceCollection.DeviceDataList.Add(guid, deviceData);
            _deviceCollection.DeviceList.Add(guid, device);
        }

    }


    [ObservableProperty]
    private string? _CurrentModule;

    [RelayCommand]
    private void Open(ContainerInfo para)
    {
        try
        {
            _commonContainer.RequestNavigation(para.ModuleName);
            this.CurrentModule = para.Name;
        }
        catch
        {
        }
    }
}