using ESBasic.ObjectManagement.Managers;
using ESBasic;
using OMCS.Passive;
using System;
using System.Collections.Generic;
using System.Text;

namespace OVCS.ClientLinux.Tools
{
    /// <summary>
    /// 实时检测最大发言者的id
    /// </summary>
    internal class MaxVioceSpeaker
    {
        private IMultimediaManager MultimediaManager;
        //存入出现最大的声音的次数
        private ObjectManager<string, int> countManager = new ObjectManager<string, int>();
        //多次内出现声音最大者的出现最多次数
        private int maxCount = 0;
        //记录5次
        private int speakerCount = 0;
        //记录五次id为空的情况
        private int nullCount = 0;
        private string lastMaxVoiceUserID;

        /// <summary>
        /// 当说话声音最大者改变时，触发此事件。参数：说话声音最大者的UserID
        /// </summary>
        public event CbGeneric<string> MaxVoiceUserChanged;

        public MaxVioceSpeaker(IMultimediaManager mgr)
        {
            this.MultimediaManager = mgr;
            this.MultimediaManager.MaxVoiceSpeakerNotified += MultimediaManager_MaxVoiceSpeakerNotified;
        }

        private void MultimediaManager_MaxVoiceSpeakerNotified(string speakerId, int decibel)
        {
            string realMaxUserID = null;
            bool isOk = this.GetMaxVioceSpeaker(speakerId, out realMaxUserID);
            if (isOk)
            {
                if (realMaxUserID != this.lastMaxVoiceUserID)
                {
                    this.lastMaxVoiceUserID = realMaxUserID;
                    this.MaxVoiceUserChanged?.Invoke(this.lastMaxVoiceUserID);
                }
            }
        }
        public void ResetMaxVoiceSpeaker()
        {
            this.lastMaxVoiceUserID = null;
        }
        private bool GetMaxVioceSpeaker(string _speakerId, out string userID)
        {
            nullCount++;
            userID = null;
            if (_speakerId == null)
            {
                if (nullCount == 5)
                {

                    return true;
                }
                else
                {
                    return false;
                }
            }
            nullCount = 0;
            speakerCount++;
            countManager.Add(_speakerId, countManager.Get(_speakerId) + 1);
            if (speakerCount != 5)
            {
                return false;
            }
            speakerCount = 0;
            //记录多次内声音最大发言人出现次数
            List<string> ids = new List<string>();
            //循环判断是否为声音最大且出现次数最多的发言人
            foreach (string item in countManager.GetKeyList())
            {
                int currentCount = countManager.Get(item);
                if (currentCount == maxCount)
                {
                    ids.Add(item);
                }
                if (currentCount > maxCount)
                {
                    ids.Clear();
                    maxCount = currentCount;
                    ids.Add(item);
                }
            }
            if (ids.Count > 1)
            {
                return false;
            }
            maxCount = 0;
            countManager.Clear();

            userID = ids[0];
            return true;
        }
    }
}
