using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

namespace DuckHunter
{
    public class CrossHair : MonoBehaviour
    {
        public static CrossHair Instance;

        void Start()
        {
            Instance = this;
            InitRotateRangeVH();
            UpdateHideShow(SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked);
            GlobalEventCenter.ins.onSimulateMouseAwakeChanged += UpdateHideShow;

            CrossHairOutBoundChecker1 outBoundChecker = Instantiate(Resources.Load<GameObject>("Prefabs/CrossHairOutBoundChecker1")).GetComponent<CrossHairOutBoundChecker1>();
            outBoundChecker.crossHair = transform as RectTransform;

            if (InfraredManager.ConnetDevicesSingle.ins)
                InfraredManager.ConnetDevicesSingle.ins.posAction += UpdatePosition;

        }

        void OnDestroy()
        {
            if (Instance == this) Instance = null;
            GlobalEventCenter.ins.onSimulateMouseAwakeChanged -= UpdateHideShow;

            if (InfraredManager.ConnetDevicesSingle.ins)
                InfraredManager.ConnetDevicesSingle.ins.posAction -= UpdatePosition;

        
        }

        void UpdatePosition(Vector3 pos) {
            transform.position = pos;
        }
        void UpdateShow(bool show)
        {
            transform.Find("Image").GetComponent<Image>().enabled = show;
        }
        void UpdateHideShow(bool mouseAwaked)
        {
            transform.Find("Image").GetComponent<Image>().enabled = !mouseAwaked;
        }

        Vector2 mapPosition;
        float rotateRangeV;
        float rotateRangeH;
        void InitRotateRangeVH()
        {
            rotateRangeV = 25f;
            float halfScreenHeight = Screen.height * 0.5f;
            float halfScreenWidth = Screen.width * 0.5f;
            float halfRangeV = rotateRangeV * 0.5f;
            float normalLine = halfScreenHeight / Mathf.Tan(halfRangeV / 180f * Mathf.PI);
            float halfRangeH = Mathf.Atan(halfScreenWidth / normalLine) / Mathf.PI * 180f;
            rotateRangeH = halfRangeH * 2f;
        }
        public void UpdatePositionByModuleRotation(Quaternion rotation)
        {
            Vector3 resEulerAngles = rotation.eulerAngles;
            resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -rotateRangeV / 2, rotateRangeV / 2);
            resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -rotateRangeH / 2, rotateRangeH / 2);
            mapPosition.x = (resEulerAngles.y + rotateRangeH / 2) / rotateRangeH * Screen.width;
            mapPosition.y = (rotateRangeV / 2 - resEulerAngles.x) / rotateRangeV * Screen.height;
            transform.position = mapPosition;
        }

        public void Shoot()
        {
            AudioManager.Instance.PlayShoot(null);
            _PlayShootAnimation();
        }

        Sequence _shootSequence;
        void _PlayShootAnimation()
        {
            if (_shootSequence != null)
            {
                _shootSequence.Kill();
                _shootSequence = null;
            }
            _shootSequence = DOTween.Sequence();
            _shootSequence.Append(transform.DOScale(new Vector3(0.8f, 0.8f, 1), 0.15f));
            _shootSequence.Append(transform.DOScale(Vector3.one, 0.15f));
        }
    }
}
