using Infrastructure.Cache;
using Infrastructure.CloudSdk.mqttmessagecenter;
using OpenAuth.App.ServiceApp;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Model.mqtt
{
    public class ThingAiTaskHandler : IMqttMessageHandler
    {
        LasaPlatformPushApp _app;
        private readonly ICacheContext _cache;
        public ThingAiTaskHandler(LasaPlatformPushApp app, ICacheContext cache)
        {
            _app = app;
            _cache = cache;
        }
        public bool CanHandle(string topic)
        {
            return topic.Contains("ai/task");
        }
        public async Task HandleAsync(string topic, string payload)
        {
            var parts = topic.Split('/', StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length == 0)
                return;
            var action = parts[^1];
            switch (action)
            {
                case "tasklog":
                    var info = Newtonsoft.Json.JsonConvert.DeserializeObject<LasaTaskAiLog>(payload);
                    if (info != null)
                    {
                        await _app.AddTaskAiLog(info);
                    }
                    break;
                case "aiachievement":
                    await _app.AddImgTopic(payload);
                    break;
            }
        }
    }
}
