using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Assets.Zion.Scripts.Adam
{
    public class WRJManager : MonoBehaviour
    {
        public RectTransform wrjMenuSetPanel;
        public RectTransform canvasRect;

        private void Start()
        {
            wrjMenuSetPanel.gameObject.SetActive(false);
        }

        private void Update()
        {
            if (UIBootstrap.Instance.GetRoleByIDPracticeId(GlobalFlag.practiceSeatId) == "0" && Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitInfo;
                if (Physics.Raycast(ray, out hitInfo, 1000))
                {
                    EquipmentCommon ec = hitInfo.collider.GetComponent<EquipmentCommon>();
                    if (ec != null && ec.isPlayer)
                    {
                        if (hitInfo.collider.tag == "WRJ")
                        {
                            wrjMenuSetPanel.localScale = Vector3.one;
                            Vector3 posItem = GetScreenPosition(hitInfo.collider.gameObject);
                            wrjMenuSetPanel.gameObject.SetActive(true);
                            wrjMenuSetPanel.anchoredPosition = new Vector3(posItem.x + 100f, posItem.y, posItem.z);
                        }
                    }
                }
                else
                {
                    wrjMenuSetPanel.localScale = Vector3.zero;
                }
            }

        }

        public Vector3 GetScreenPosition(GameObject target)
        {
            float width = canvasRect.sizeDelta.x;
            float height = canvasRect.sizeDelta.y;
            Vector3 pos = Camera.main.WorldToScreenPoint(target.transform.position);
            pos.x *= width / Screen.width;
            pos.y *= height / Screen.height;
            pos.x -= width * 0.5f;
            pos.y -= height * 0.5f;
            return pos;
        }
    }
}