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

namespace Server.AliyunSDK.Logic
{
    /// <summary>
    /// 战绩上传管理
    /// </summary>
    public class WarRecordSLSManager : SingleInstance<WarRecordSLSManager>
    {
        AliyunSLSService mService = null;
        
        private const string GoldLogStore_Test = "war_record_test";
        private const string FriendLogStore_Test = "friendwarrecord_test";
        
        private const string GoldLogStore_Release = "war_record";
        private const string FriendLogStore_Release = "friendwarrecord";
        
        private string GoldLogStore{
            get
            {
                if (ServerConst.IsTest)
                {
                    return GoldLogStore_Test;
                }

                return GoldLogStore_Release;
            }
        }
        
        private string FriendLogStore{
            get{
                if (ServerConst.IsTest)
                {
                    return FriendLogStore_Test;
                }

                return FriendLogStore_Release;
            }
        }

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

        public async void ReportGold(WarGoldRecordItemData data, string source = "")
        {
            var log = ConvertLogItemData(data);
            await mService.PutLogsAsync(GoldLogStore, new List<LogInfo>(){log}, "", source);
        }
        
        public Task ReportGoldAsync(WarGoldRecordItemData data, string source = "")
        {
            return Task.Run(() =>
            {
                ReportGold(data, source);
            });
        }
        
        public Task ReportGoldAsync(IEnumerable<WarGoldRecordItemData> datas, string source = "")
        {
            return Task.Run(() =>
            {
                ReportGold(datas, source);
            });
        }
        
        public async void ReportGold(IEnumerable<WarGoldRecordItemData> datas, string source = "")
        {
            var logs = datas.Select(ConvertLogItemData).ToList();
            await mService.PutLogsAsync(GoldLogStore, logs, "", source);
        }

        public Task ReportFriendAsync(IEnumerable<WarFriendItemData> datas,string source = "")
        {
            return Task.Run(() =>
            {
                ReportFriend(datas, source);
            });
        }

        public async void ReportFriend(IEnumerable<WarFriendItemData> datas,string source = "")
        {
            var logs = datas.Select(ConvertLogItemData).ToList();
            Debug.Info("ReportFriendReportFriendReportFriend " + logs.Count);
            await mService.PutLogsAsync(FriendLogStore, logs, "", source);
        }
        
        private LogInfo ConvertLogItemData(WarGoldRecordItemData data)
        {
            LogInfo logItem = new LogInfo();
            logItem.Time = DateTimeOffset.Now;
            logItem.Contents = new Dictionary<string, string>()
            {
                {WarGoldRecordItemData.UserIdName, data.UserId.ToString() },
                {WarGoldRecordItemData.ScoreName, data.Score.ToString() },
                {WarGoldRecordItemData.ContentName, JsonPack.GetJson(data.Content)},
                {WarGoldRecordItemData.GameIdName, data.GameId.ToString()},
                {WarGoldRecordItemData.BeiLvName,data.BeiLv.ToString("f2")},
                {WarGoldRecordItemData.TaxName,data.Tax.ToString()},
                {WarGoldRecordItemData.RoomIdName,data.RoomId},
                {WarGoldRecordItemData.EndTimeName, data.EndTime},
                {WarGoldRecordItemData.DescriptionName,data.Description}
            };
            
            return logItem;
        }

        private LogInfo ConvertLogItemData(WarFriendItemData data)
        {
            LogInfo logItem = new LogInfo();
            logItem.Time = DateTimeOffset.Now;
            logItem.Contents = new Dictionary<string, string>()
            {
                {WarFriendItemData.UserIdName,data.UserId.ToString()},
                {WarFriendItemData.GameIdName,data.GameId.ToString()},
                {WarFriendItemData.WeiYiMaName,data.WeiYiMa.ToString()},
                {WarFriendItemData.ContentName,data.Content}
            };

            return logItem;
        }
    }
}