using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace WS
{
    [ObjectSystem]
    public class ViewManagerAwake : AwakeSystem<ViewManager>
    {
        public override void Awake(ViewManager self)
        {
            self.Awake();
        }
    }

    ///<summary>UI 管理</summary>
    public partial class ViewManager : WSComponent
    {
        ///<summary>View配置</summary>
        private List<ResourcePath> Config;

        ///<summary>Canvas</summary>
        private Dictionary<ViewShowModeType, ViewCanvas> _Canvas =
            new Dictionary<ViewShowModeType, ViewCanvas>();

        ///<summary>标准分辨率值</summary>
        public float StandardScreen;

        ///<summary>当前分辨率值</summary>
        public float CurrStandardScreen;

        public void Awake()
        {
            StandardScreen = 1920.0f / 1080.0f;
            CurrStandardScreen = Screen.width / (float)Screen.height;
            GameObject init = GameObject.Find("Init");
            Transform scene = init.transform.Find("UI/SceneCanvas");
            ViewCanvas canvass = ComponentFactory.CreateWithParent<
                ViewCanvas,
                Transform,
                ViewShowModeType
            >(this, scene, ViewShowModeType.Screen);
            _Canvas[ViewShowModeType.Screen] = canvass;
            scene = init.transform.Find("UI/WorldCanvas");
            canvass = ComponentFactory.CreateWithParent<ViewCanvas, Transform, ViewShowModeType>(
                this,
                scene,
                ViewShowModeType.World
            );
            _Canvas[ViewShowModeType.World] = canvass;
            Config = ConfigHelper.LoadConfig<List<ResourcePath>>("Config/Framework/ViewConfig");
            StartType();
        }

        ///<summary>创建ViewCanvas</summary>
        public void CreateViewCanvas(ViewShowModeType viewshow, Transform root)
        {
            ViewCanvas canvas = ComponentFactory.CreateWithParent<
                ViewCanvas,
                Transform,
                ViewShowModeType
            >(this, root, viewshow);
            _Canvas[viewshow] = canvas;
        }

        ///<summary>获取ViewCanvas</summary>
        public ViewCanvas GetViewCanvas(ViewShowModeType viewshow)
        {
            if (_Canvas.ContainsKey(viewshow))
            {
                return _Canvas[viewshow];
            }
            return null;
        }

        ///<summary>移除ViewCanvas</summary>
        public void RemoveViewCanvas(ViewShowModeType viewshow)
        {
            ViewCanvas canvas = _Canvas[viewshow];
            _Canvas.Remove(viewshow);
            canvas.Dispose();
        }

        /// <summary>打开窗口</summary>
        public V OpenView<V>(UnityAction opencallback = null)
            where V : View
        {
            return CreateView<V>(ViewShowModeType.Default, null, opencallback);
        }

        /// <summary>打开窗口</summary>
        /// <param name="showmode">显示Canvas类型</param>
        /// <param name="clone">克隆体名称</param>
        public V OpenView<V>(string clone, UnityAction opencallback = null)
            where V : View
        {
            return CreateView<V>(ViewShowModeType.Default, clone, opencallback);
        }

        /// <summary>打开窗口</summary>
        public V OpenView<V>(ViewShowModeType showmode, UnityAction opencallback = null)
            where V : View
        {
            return CreateView<V>(showmode, null, opencallback);
        }

        /// <summary>打开窗口</summary>
        /// <param name="showmode">显示Canvas类型</param>
        /// <param name="clone">克隆体名称</param>
        public V OpenView<V>(
            ViewShowModeType showmode,
            string clone,
            UnityAction opencallback = null
        )
            where V : View
        {
            return CreateView<V>(showmode, clone, opencallback);
        }

        ///<summary>创建View</summary>
        private V CreateView<V>(
            ViewShowModeType showmode = ViewShowModeType.Default,
            string clone = null,
            UnityAction opencallback = null
        )
            where V : View
        {
            string uiprefab = GetToName(typeof(V));
            ResourcePath pathdata = null;
            if (string.IsNullOrEmpty(clone))
                pathdata = Config.Find(v => v.Name == uiprefab);
            else
                pathdata = Config.Find(v => v.Name == clone);
            GameObject obj = GameObject.Instantiate(Resources.Load<GameObject>(pathdata.Path));
            V view = ComponentFactory.Create<V>();
            view.UIGameObject = obj;
            view.LoadInit();
            if (showmode == ViewShowModeType.Default)
            {
                _Canvas[view.ShowModeType].ShowCanvas(view);
            }
            else
            {
                _Canvas[showmode].ShowCanvas(view);
                view.ShowModeType = showmode;
            }
            view.SetPosition();
            Animation component = view.UIGameObject.GetComponent<Animation>();
            if (component != null)
            {
                view.AnimComponent = view.AddComponent<AnimationComponent, Animation>(component);
                view.AnimComponent.PlayAnim(
                    "open",
                    () =>
                    {
                        view.OnOpen();
                        opencallback?.Invoke();
                        view.ShowType = ViewShowType.Open;
                    }
                );
            }
            else
            {
                view.OnOpen();
                opencallback?.Invoke();
                view.ShowType = ViewShowType.Open;
            }
            VoiceManager.Instance.Play(VoiceType.Effects, "ui-open-1");
            return view;
        }

        /// <summary>关闭窗口</summary>
        public void CloseView(View view, UnityAction closecallback)
        {
            if (view != null)
            {
                if (view.AnimComponent != null)
                {
                    view.AnimComponent.PlayAnim(
                        "close",
                        () =>
                        {
                            closecallback?.Invoke();
                            CloseSet(view);
                        }
                    );
                }
                else
                {
                    CloseSet(view);
                }
            }
        }

        ///<summary>关闭设置</summary>
        private static void CloseSet(View view)
        {
            if (view.ShowModeType != ViewShowModeType.Screen)
            {
#if PLATFORM_PICO
                GamePoolManager.Instance.Recovery(view.UIGameObject.transform.parent.gameObject);
#endif
            }
            UnityEngine.Object.Destroy(view.UIGameObject);
            view.Dispose();
        }

        public override void Dispose()
        {
            base.Dispose();
            foreach (var item in _Canvas)
            {
                item.Value.Dispose();
            }
            _Canvas.Clear();
        }
    }
}
