using System;
using UnityEngine;

namespace XGame.Framework.UI
{
    public abstract class NestedView<TController, TViewModel> : INestedInternal, IDisposable
        where TController : class, IUIController
        where TViewModel : class, IUIViewModel
    {
        private UIContext _context;
        private IUINested _nested;
        private TViewModel _vm;
        private TController _controller;

        public TViewModel VM => _vm;
        public TController Ctrl => _controller;
        private Transform NestedTr => _nested.Tr;
        Vector3 INestedView.LocalPosition { get => NestedTr.localPosition; set => NestedTr.localPosition = value; }
        Vector3 INestedView.Position { get => NestedTr.position; set => NestedTr.position = value; }
        Vector3 INestedView.Rotation { get => NestedTr.rotation.eulerAngles; set => NestedTr.rotation = Quaternion.Euler(value); }
        IUINested INestedInternal.Nested => _nested;

        void INestedInternal.Init(UIContext context, IUINested nested, bool isStatic)
        {
            _context = context;
            _nested = nested;
            _vm = Activator.CreateInstance<TViewModel>();
            (_vm as IUIVMInitable).Init(nested, context);
            var controller =Activator.CreateInstance<TController>();
            if (controller is IUIContextSetter contextSetter)
            {
                contextSetter.Context = _context;
            }
            if (controller is IUIViewModelSetter vmSetter)
            {
                vmSetter.ViewModel = _vm;
            }
            _controller = controller;
            if (isStatic && nested.IsActiveSelf)
            {
                Active = true;
            }
        }
        private bool _isActive;
        public bool Active 
        { 
            get => _isActive; 
            private set 
            {
                _isActive = value;
                var ctrlGroup = (_context as IUIViewAdapter).CtrlGroup;
                if (value)
                    ctrlGroup?.Add(_controller, false);
                else
                    ctrlGroup?.Remove(_controller);
            } 
        }
        public void Enable(object intent)
        {
            if (Active) return;
            Active = true;
            _nested.SetActive(true);
            if (_nested.IsActiveInHierarchy)
            { // 已经在场景里显示，自己激活Controller，否则由UIView统一激活
                _controller.Enable(intent);
            }
        }
        public void Disable()
        {
            if (!Active) return;
            Active = false;
            _nested.SetActive(false);
            _controller.Disable();
        }

        void IDisposable.Dispose()
        {
            //if (_isActive)
            //{ // 预防没有执行Disable()
            //    Active = false;
            //    _controller.OnDisable();
            //}
            OnDispose();
            (_controller as IDisposable)?.Dispose();
            _nested = default;
            _context = null;
            _vm = null;
        }
        protected abstract void OnDispose();
    }
}
