using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

namespace StudyCase3
{
    public class ThirdCharacterController : MonoBehaviour
    {
        public CharacterController characterController;
        public Animator animator;
        public Transform forward;
        public Transform model;
        public Transform items;
        public Cinemachine.CinemachineVirtualCamera vCam;

        public float moveSpeed = 5f;
        public float jumpSpeed = 2f;
        public float turnSpeed = 10f;
        public float gravity = 10f;
        public bool pickUpByCollider;
        public float maxDistance = 5f;
        public LayerMask layer;
        public float itemsRotateSpeed = 100f;

        Vector3 moveDir;
        Vector2 moveInput;
        Item selectItem;
        private void Start()
        {
            Cursor.lockState = CursorLockMode.Locked;
        }
        private void Update()
        {
            Move();
            RotationItems();
            CheckRayHit();
        }
        public void OnMove(InputAction.CallbackContext context)
        {
            if (characterController.isGrounded)
                moveInput = context.ReadValue<Vector2>();
            else moveInput = Vector2.zero;
        }
        public void OnJump(InputAction.CallbackContext context)
        {
            if (context.performed && characterController.isGrounded)
            {
                moveDir.y = jumpSpeed;
            }
        }
        public void OnPickUp(InputAction.CallbackContext context)
        {
            if (context.performed && selectItem!=null)
            {
                selectItem.PickUp(items);
                selectItem = null;
                RearrangeItems();
            }
        }
        void Move()
        {
            moveDir = new Vector3(moveInput.x, moveDir.y, moveInput.y);
            forward.eulerAngles = new Vector3(0, vCam.transform.eulerAngles.y, 0);
            moveDir = forward.TransformDirection(moveDir);
            if (moveInput != Vector2.zero)
            {
                animator?.SetBool("Move", true);
                Quaternion target = Quaternion.LookRotation(new Vector3(moveDir.x, 0, moveDir.z));
                model.rotation = Quaternion.Slerp(model.rotation, target, turnSpeed * Time.deltaTime);
            }
            else animator?.SetBool("Move", false);
            if (!characterController.isGrounded)
                moveDir.y -= gravity * Time.deltaTime;
            characterController.Move(moveDir * moveSpeed * Time.deltaTime);
        }
        void CheckRayHit()
        {
            Ray ray = new Ray(model.position, model.forward);
            RaycastHit hit;
            if(Physics.Raycast(ray,out hit, maxDistance, layer))
            {
                Item curItem = hit.collider.GetComponent<Item>();
                if (curItem && curItem != selectItem)
                {
                    if(selectItem!=null)
                        selectItem.UnSelect();
                    selectItem = curItem;
                    selectItem.Select();
                }
            }
            else
            {
                if (selectItem != null)
                    selectItem.UnSelect();
                selectItem = null;
            }
        }
        void RotationItems()
        {
            items.transform.Rotate(0, itemsRotateSpeed*Time.deltaTime, 0);
        }
        void RearrangeItems()
        {
            int childCount = items.childCount;
            if (childCount == 0) return;

            float radius = 1.5f;
            float angleStep = 360f / childCount;

            for (int i = 0; i < childCount; i++)
            {
                Transform item = items.GetChild(i);
                float angle = i * angleStep;
                Quaternion rotation = Quaternion.Euler(0, angle, 0);
                item.localPosition = rotation * Vector3.forward * radius;
                item.localScale = Vector3.one * 0.1f;
            }
        }
    }
}
