using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aliyun.Api.LogService.Domain.Log;
using GameMessage;
using Server.Core;

namespace Server.AliyunSDK.SLSLog
{
    public class SLSLogManager : SingleInstance<SLSLogManager>
    {
        private AliyunSLSService mService = null;

        public SLSLogManager()
        {
            mService = AliyunServiceFactory.Instance.GetService<AliyunSLSService>();
        }

        public async Task ReportLog(ISLSBasicInfo info,IEnumerable<ISLSLogData> datas)
        {
            if (datas == null)
            {
                return;
            }
            
            IList<LogInfo> logInfos = Convert(datas);
            if (logInfos.Count <= 0)
            {
                return;
            }
            
            string logStore = ServerConst.IsTest ? info.LogStoreTest : info.LogStore;
            await mService.PutLogsAsync(logStore,logInfos,info.Topic,info.Source);
        }
        
        private IList<LogInfo> Convert(IEnumerable<ISLSLogData> datas)
        {
            List<LogInfo> logInfos = new List<LogInfo>();
            foreach (var data in datas)
            {
                logInfos.Add(data.Convert());
            }

            return logInfos;
        }
    }
}