using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Passer.Humanoid;

namespace Passer {

    public class PossessionDetailsUI : MonoBehaviour {

        public PossessionsUI pawnPossessionsUI;

        public Text nameUI;
        public Text countUI;
        public Socket socket;

        public HumanoidControl humanoid;
        public VisitorPossessions humanoidPossessions;

        protected VisitorPossessions.Possession possession;

        public void SetPossesion(VisitorPossessions.Possession possession) {
            this.possession = possession;
        }

        void OnEnable() {
            if (possession == null)
                return;

            if (possession.type == Possessable.Type.Avatar) {
                // make the humanoid change to the avatar directly?
                StartCoroutine(VisitorPossessions.RetrievePossessionAsync(possession, ChangeAvatar));
            }
            else {

                nameUI.text = possession.name;
                countUI.text = "";

                StartCoroutine(VisitorPossessions.RetrievePossessionAsync(possession, AttachToSocket));
            }
        }

        private void OnDestroy() {
            VisitorPossessions.UnloadPossession();
        }

        private void AttachToSocket(GameObject prefab) {
            humanoidPossessions = humanoid.GetComponentInChildren<VisitorPossessions>();

            humanoidPossessions.DeletePossession(possession);
            GameObject possessionObj = Object.Instantiate(prefab);
            possessionObj.name = prefab.name;

            socket.Attach(possessionObj);
        }

        private void ChangeAvatar(GameObject avatarObj) {
            humanoid.ChangeAvatar(avatarObj);
            this.gameObject.SetActive(false);
            pawnPossessionsUI.gameObject.SetActive(true);
        }
    }
}