using UnityEngine;

namespace StudyCase3
{
    public class Item : MonoBehaviour
    {
        Outline outline;
        bool pick;
        ThirdCharacterController player;
        private void Awake()
        {
            player = FindAnyObjectByType<ThirdCharacterController>();
            if (outline == null)
                outline = gameObject.AddComponent<Outline>();
            else
                outline = GetComponent<Outline>();
            outline.enabled = false;
            outline.OutlineMode = Outline.Mode.OutlineVisible;
            outline.OutlineColor = new Color(0, 1, 1);
            outline.OutlineWidth = 10f;
            gameObject.layer = LayerMask.NameToLayer("Item");
        }
        public void Select()
        {
            if(!pick)
                outline.enabled = true;
        }
        public void UnSelect()
        {
            if (!pick)
                outline.enabled = false;
        }
        public void PickUp(Transform root)
        {
            if (pick) return;
            pick = true;
            transform.SetParent(root);
            outline.OutlineWidth = 2f;
            gameObject.layer = LayerMask.NameToLayer("Default");
            player.SetItems();
        }
        private void OnMouseEnter()
        {
            if (player.pickMode == PickMode.Mouse)
                Select();
        }
        private void OnMouseExit()
        {
            if (player.pickMode == PickMode.Mouse)
                UnSelect();
        }
        private void OnMouseDown()
        {
            if (player.pickMode == PickMode.Mouse)
                PickUp(player.itemRoot);
        }
    }
}