using OnlineUserPool.Model;
using OnlineUserPool.Unility;

using Serilog;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineUserPool.Hander
{
    public class MapRecordRankingHander
    {
        private static List<MapRecordRanking> mapRecordRankings;
        private static VirtualUserList virtualUserList;
        private static bool loading = false;//防止服务端响应慢，多次加载
        private int top = int.Parse(ConfigHelp.Top);

        public MapRecordRankingHander()
        {
            var randomUser = WebService.GetMapRouteRandomUser(new RandomRankingUserRequestVM() { top = top });
            mapRecordRankings = WebService.GetRecordFileFromServer(randomUser.Select(n => n.RankingId).ToList());
        }

        /// <summary>
        /// 获取虚拟人物数据
        /// </summary>
        /// <returns></returns>
        public List<MsgModel> GetVirtualUserData()
        {
            List<MsgModel> msgModels = new List<MsgModel>();
            if (mapRecordRankings != null && mapRecordRankings.Count > 0)
            {
                for (int i = 0; i < mapRecordRankings.Count; i++)
                {
                    try
                    {
                        var item = mapRecordRankings[i];
                        TargetData targetData = item.GetCurrentTargetData();
                        List<string> prop = new List<string>()
                        {
                            Math.Round(targetData._Speed, 2).ToString(),
                            targetData._Power.ToString(),
                            targetData._Cadence.ToString()
                        };
                        msgModels.Add(new MsgModel()
                        {
                            exit = item.End,
                            IsCompleted = item.End,
                            MemberId = item.UserId,
                            Point = new double[] { targetData._Lat, targetData._Lon },
                            Prop = string.Join(',', prop),
                            RouteId = item.RouteId,
                            EndDistance = targetData._Distance,
                            ShowVirtual = true,
                            CommandType = 1,
                            IsVirtual = true,
                            Speed = targetData._Speed
                        });
                    }
                    catch (Exception e)
                    {
                        Log.Error("加载虚拟人物错误:" + e.Message);
                    }
                }
            }
            return msgModels;
        }

        /// <summary>
        /// 删除已经骑行完的人，添加新的人物进去
        /// </summary>
        public void RemoveEndAndAddNewVirtualUser()
        {
            if (mapRecordRankings != null && mapRecordRankings.Count > 0 && !loading)
            {
                loading = true;
                var end = mapRecordRankings.FindAll(n => n.End);
                if (end.Count > 0)
                {
                    mapRecordRankings.RemoveAll(n => n.End);
                    var randomUser = WebService.GetMapRouteRandomUser(new RandomRankingUserRequestVM()
                    {
                        top = int.Parse(ConfigHelp.Top) - end.Count,
                        ids = mapRecordRankings.Select(n => n.UserId.ToString()).ToList()
                    });
                    var addRankings = WebService.GetRecordFileFromServer(randomUser.Select(n => n.RankingId).ToList());
                    mapRecordRankings.AddRange(addRankings);
                }
                loading = false;
            }
        }
    }
}
