using System;
using System.Collections.Generic;
using Aliyun.Api.LogService;
using Cloud.Alibaba;
using Cloud.Alibaba.Sls;
using Server.Core;
using ClientManager = Cloud.Alibaba.Sls.ClientManager;

namespace Server.AliyunSDK
{
    public class AliyunServiceFactory : SingleInstance<AliyunServiceFactory>
    {
        Dictionary<string, IAliyunBaseService> mServices = new Dictionary<string, IAliyunBaseService>();
        
        public T GetService<T>() where T : IAliyunBaseService
        {
            var serviceName = typeof(T).Name;
            if(mServices.TryGetValue(serviceName, out var service))
                return (T)service;
            
            // 根据泛型类型返回对应服务实例
            switch (serviceName)
            {
                case nameof(AliyunSLSService):
                    service = CreateSLSService();
                    mServices.Add(serviceName, service);
                    return (T)service;
                default:
                    throw new NotSupportedException($"未支持的服务类型: {typeof(T)}");
            }
        }
        
        
        private AliyunSLSService CreateSLSService()
        {
            ILogServiceClient client = ClientManager.GetClient(Account.Ecs, SlsProject.ServerLog);
            return new AliyunSLSService(client);
        }
        
    }
}