using System;
using System.Collections.Generic;

using System.Text;
using UnityEngine;

using Thousandto.Core.RootSystem;
using Thousandto.Core.Base;

namespace Thousandto.Code.Logic
{
    //LocalPlayer的根--也用于保存LocalPlayer
    public static class LocalPlayerRoot
    {
        #region//常量
        //创建的实例名字
        private const String CN_NAME = "[LocalPlayer]";
        #endregion

        //主角的对象
        private static ulong _localPlayerID = 0;
        private static LocalPlayer _localPlayer = null;
        private static Transform _root = null;
        private static int _curMountId = 0;

        public static LocalPlayer LocalPlayer
        {
            get
            {
                return _localPlayer;
            }
        }

        public static ulong LocalPlayerID
        {
            get
            {
                return _localPlayerID;
            }
        }
        public static int CurMountId
        {
            get
            {
                return _curMountId;
            }
            set
            {
                _curMountId = value;
            }
        }

        //设置本地玩家ID
        public static void SetLocalPlayerID(ulong id)
        {
            _localPlayerID = id;
        }

        //设置LocalPlayer
        public static void SetLocalPlayer(LocalPlayer player)
        {
            if (_localPlayer != player)
            {
                if (_localPlayer != null)
                {
                    _localPlayer.Uninitialize();
                }

                _localPlayer = player;
                if (_localPlayer != null)
                {
                    _localPlayerID = _localPlayer.ID;
                    if (_localPlayer.Skin != null && _localPlayer.Skin.IsValid)
                    {
                        _localPlayer.Skin.SetParent(GetRoot());
                    }
                }
            }
        }

        public static void Uninitialize()
        {
            _localPlayerID = 0;
            if (_localPlayer != null)
            {
                _localPlayer.Uninitialize();
                _localPlayer = null;
            }
            if (_root != null)
            {
                GameObject.Destroy(_root.gameObject);
                _root = null;
            }
            
        }

        private static Transform GetRoot()
        {
            if (_root == null)
            {
                var go = new GameObject(CN_NAME);
                _root = go.transform;
                _root.parent = AppRoot.Transform;
            }
            return _root;
        }

    }
}