using Device;
using Mapster;
using ServiceBase;
namespace EEMSMain.Service;

internal class UIProvider(DeviceCollection deviceCollection) : IUIProvider
{
    Task<bool> IUIProvider.InsertNewDevice(DeviceInfo deviceInfo)
    {
        DeviceInfo_VM device = deviceInfo.Adapt<DeviceInfo_VM>();
        device.IsConnected = true;
        if (device.Guid is null)
            return Task.FromResult(true);


        App.Current.Dispatcher.Invoke(() =>
        {
            deviceCollection.NewDeviceList[device.Guid.Value] = device;
        });

        return Task.FromResult(true);
    }


    Task<bool> IUIProvider.UpdateDevice(DeviceInfo deviceInfo)
    {
        if (deviceInfo.Guid is null)
            return Task.FromResult(true);

        App.Current.Dispatcher.Invoke(() =>
        {
            if (!deviceCollection.DeviceList.TryGetValue(deviceInfo.Guid.Value, out DeviceInfo_VM? device) || device is null)
            {
                device = deviceInfo.Adapt<DeviceInfo_VM>();
                deviceCollection.DeviceList[deviceInfo.Guid.Value] = device;
            }
            else
            {
                deviceInfo.Adapt(device);
            }

            if (!deviceCollection.DeviceDataList.TryGetValue(deviceInfo.Guid.Value, out DeviceData_VM? deviceData) || deviceData is null)
            {
                deviceData = new()
                {
                    DeviceId = deviceInfo.Guid.Value,
                    DeviceModel = device.DeviceModel,
                    OtherInfo = [],
                    Alarms = []
                };
                deviceCollection.DeviceDataList[deviceInfo.Guid.Value] = deviceData;
            }
            else
            {
                deviceData.DeviceModel = device.DeviceModel;
            }
        });

        return Task.FromResult(true);
    }

    Task<bool> IUIProvider.UpdateDeviceList(IEnumerable<DeviceInfo> device)
    {
        return Task.FromResult(true);
    }

    Task<bool> IUIProvider.UpdateRealtimeData(Guid guid, Dictionary<string, object> data)
    {
        if (!deviceCollection.DeviceDataList.TryGetValue(guid, out DeviceData_VM? deviceData) || deviceData is null)
            return Task.FromResult(false);

        App.Current.Dispatcher?.Invoke(() =>
        {
            deviceData.OtherInfo.Clear();
            foreach (var item in data)
            {
                switch (item.Key)
                {
                    case "Status":
                        deviceData.DeviceStatus = item.Value.ToString();
                        break;
                    default:
                        deviceData.OtherInfo[item.Key] = item.Value;
                        break;
                }
            }
        });

        return Task.FromResult(true);
    }

    Task<bool> IUIProvider.UpdateRecipeInfo(Recipe recipeInfo)
    {
        if (!deviceCollection.DeviceDataList.TryGetValue(recipeInfo.DeviceId, out DeviceData_VM? deviceData) || deviceData is null)
            return Task.FromResult(false);
        App.Current.Dispatcher?.Invoke(() =>
        {
            deviceData.RecipeInfo = recipeInfo.Adapt<RecipeInfo_VM>();
        });

        return Task.FromResult(true);
    }

    Task<bool> IUIProvider.UpdateAlarm(IEnumerable<Alarm> alarm)
    {
        if (alarm is null || alarm.FirstOrDefault() is not Alarm first)
            return Task.FromResult(true);

        if (!deviceCollection.DeviceDataList.TryGetValue(first.DeviceID, out DeviceData_VM? deviceData) || deviceData is null)
            return Task.FromResult(false);

        App.Current.Dispatcher?.Invoke(() =>
        {
            deviceData.Alarms ??= [];
            deviceData.Alarms.Clear();
            foreach (var item in alarm)
                deviceData.Alarms.Add(item.Adapt<Alarm_VM>());
        });

        return Task.FromResult(true);
    }
}
