﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace JLGames.RocketDriver.Games.PanelManager
{
    internal sealed class PanelList
    {
        internal readonly List<PanelInstance> m_Panels = new List<PanelInstance>();

        /// <summary>
        /// 列表数量
        /// </summary>
        public int Size => m_Panels.Count;

        public int GetSize(string panelId)
        {
            var rs = 0;
            m_Panels.ForEach(instance =>
            {
                if (instance.PanelId == panelId) rs += 1;
            });
            return rs;
        }

        public PanelInstance GetByInstanceId(string instanceId)
        {
            if (string.IsNullOrEmpty(instanceId)) return null;
            foreach (var instance in m_Panels)
            {
                if (instance.InstanceId == instanceId) return instance;
            }

            return null;
        }

        public PanelInstance GetByPanelId(string panelId)
        {
            if (string.IsNullOrEmpty(panelId)) return null;
            foreach (var instance in m_Panels)
            {
                if (instance.PanelId == panelId) return instance;
            }

            return null;
        }

        public PanelInstance GetLastByPanelId(string panelId)
        {
            if (string.IsNullOrEmpty(panelId)) return null;

            for (var index = m_Panels.Count - 1; index >= 0; index--)
            {
                if (m_Panels[index].PanelId == panelId) return m_Panels[index];
            }

            return null;
        }

        public PanelInstance GetByView(GameObject view)
        {
            if (null == view) return null;
            foreach (var instance in m_Panels)
            {
                if (instance.DisplayView == view) return instance;
            }

            return null;
        }

        public PanelInstance this[int index] => m_Panels[index];

        public PanelInstance GetAtIndex(int index)
        {
            return m_Panels[index];
        }

        public PanelInstance[] GetAll()
        {
            return m_Panels.ToArray();
        }

        public bool ContainsPanel(string panelId)
        {
            return null != GetByPanelId(panelId);
        }

        public bool ContainsInstance(string instanceId)
        {
            return null != GetByInstanceId(instanceId);
        }

        public bool ContainsInstance(PanelInstance instance)
        {
            return m_Panels.Contains(instance);
        }

        public void Add(PanelInstance panel)
        {
            if (null == panel) return;
            if (ContainsInstance(panel.InstanceId)) return;
            m_Panels.Add(panel);
        }

        public void RemoveAt(int index)
        {
            m_Panels.RemoveAt(index);
        }

        public void RemoveInstance(PanelInstance panel)
        {
            m_Panels.Remove(panel);
        }

        public void RemoveByInstanceId(string instanceId)
        {
            if (string.IsNullOrEmpty(instanceId)) return;
            for (var index = 0; index < m_Panels.Count; index++)
            {
                if (m_Panels[index].InstanceId == instanceId)
                {
                    m_Panels.RemoveAt(index);
                    return;
                }
            }
        }

        public void RemoveByPanelId(string panelId)
        {
            for (var index = m_Panels.Count - 1; index >= 0; index--)
            {
                if (m_Panels[index].PanelId == panelId)
                {
                    m_Panels.RemoveAt(index);
                    return;
                }
            }
        }

        public void RemoveByPanelId(string[] panelId)
        {
            if (null == panelId || panelId.Length == 0) return;
            for (var index = m_Panels.Count - 1; index >= 0; index--)
            {
                if (Array.IndexOf(panelId, m_Panels[index].PanelId) != -1)
                {
                    m_Panels.RemoveAt(index);
                }
            }
        }

        public void RemoveByInfo(IPanelInfo[] infos)
        {
            if (null == infos || infos.Length == 0) return;
            var ids = new string[infos.Length];
            for (var index = 0; index < ids.Length; index++)
            {
                ids[index] = infos[index].Id;
            }

            RemoveByPanelId(ids);
        }

        public void Clear()
        {
            m_Panels.Clear();
        }
    }
}