using System;
using System.Collections.Generic;
using System.Linq;
using GxPress.Request.App.WorkMeeting;
using GxPress.Service.Interface.Meeting;

namespace GxPress.Service.Implement.Meeting
{
    public class MeetingService : IMeetingService
    {
        /// <summary>
        /// 返回相邻的
        /// </summary>
        /// <param name="nums"></param>
        /// <returns></returns>
        public Dictionary<int, List<int>> GetDictionary(List<int> nums)
        {
            // var nums = new int[] { 1, 2, 3, 5, 6, 89, 100 };
            var keyValue = new Dictionary<int, List<int>>();
            var k = 1;
            for (int i = 0; i < nums.Count; i++)
            {
                List<int> ints = new List<int>();
                if (keyValue.Count == 0)
                {
                    ints.Add(nums[i]);
                    keyValue.Add(k, ints);
                }
                if (keyValue.TryGetValue(k, out ints))
                {
                    if (ints.Contains(nums[i]))
                        continue;
                    var jg = ints.Last();
                    if ((jg + 1) == nums[i])
                    {
                        keyValue.Remove(k);
                        ints.Add(nums[i]);
                        keyValue.Add(k, ints);
                    }
                    else
                    {
                        k++;
                        ints = new List<int>();
                        ints.Add(nums[i]);
                        keyValue.Add(k, ints);
                    }
                }
            }
            return keyValue;
        }
        /// <summary>
        /// 获取会议的订阅时间
        /// </summary>
        /// <param name="orderList"></param>
        /// <returns></returns>
        public Dictionary<List<TimeDictionary>, List<MeetingTime>> GetMeetingTimeDictionary(IEnumerable<Entity.WorkMeeting.MeetingOrder> orderList)
        {
            Dictionary<List<TimeDictionary>, List<MeetingTime>> result = new Dictionary<List<TimeDictionary>, List<MeetingTime>>();
            var orderLists = new List<TimeDictionary>();
            List<MeetingTime> meetingTimes = new List<MeetingTime>();
            //
            var dataList = orderList.OrderByDescending(n => n.Date).Select(n => n.Date).Distinct();
            //新版
            foreach (var item in dataList)
            {
                var timeDictionary = new TimeDictionary();
                timeDictionary.TimeKey = item.ToString();
                timeDictionary.TimeValues = orderList.Where(n => n.Date == item).OrderBy(n => n.Hour).Select(n => n.Hour).Distinct().ToList();
                orderLists.Add(timeDictionary);
                //获取相邻的
                var nearTimeDictionary = GetDictionary(timeDictionary.TimeValues);
                foreach (var ntd in nearTimeDictionary)
                {
                    var meetingTime = new MeetingTime();
                    var data = item.ToString().Insert(4, "-").Insert(7, "-");
                    var dataTime = Convert.ToDateTime(data);
                    var month = "M";
                    var day = "d";
                    if (dataTime.Month >= 10)
                        month = "MM";
                    if (dataTime.Day >= 10)
                        day = "dd";
                    meetingTime.DateValue = dataTime.ToString($"{month}月{day}日");
                    //获取星期几
                    string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
                    string week = Day[Convert.ToInt32(dataTime.DayOfWeek.ToString("d"))].ToString();
                    meetingTime.WeekValue = week;
                    //获取相邻的数据集
                    meetingTime.BeginHour = ntd.Value.First();
                    meetingTime.EndHour = ntd.Value.Last()+1;
                    meetingTimes.Add(meetingTime);
                }
            }
            result.Add(orderLists, meetingTimes);
            return result;
        }
    }
}