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

public class MouseFol : MonoBehaviour
{
    private Inputs Input;
    private InputAction Shoot;
    private InputAction Aim;
    private InputAction Mouse;
    private SpriteRenderer Rend;
    private GameObject hand;
    public Animator PL_Ani;
    public BoxCollider2D PL_Col;
    public PlayerMovement PL_Script;
    public Transform PL_Trans;
    public Transform GR;
    public float rot;
    private void Awake()
    {
        Input = new Inputs();
        Rend = gameObject.GetComponent<SpriteRenderer>();
        hand = GameObject.Find("Hand");
    }
    private void OnEnable()
    {
        Shoot = Input.Movement.attack;
        Aim = Input.Movement.Aim;
        Mouse = Input.Movement.Mouse_Pos;
        Shoot.Enable();
        Aim.Enable();
        Mouse.Enable();
    }
    private void OnDisable()
    {
        Shoot.Disable();
        Aim.Disable();
        Mouse.Disable();
    }
    private void Update()
    {
        Aim.performed += _ => tpStand();
        Aim.canceled += _ => deTpStand();
        Vector2 campoint = Camera.main.ScreenToWorldPoint(Mouse.ReadValue<Vector2>());
        rot = (Mathf.Atan2((transform.position.y - campoint.y), (transform.position.x - campoint.x)) * Mathf.Rad2Deg) + 180;
        transform.rotation = Quaternion.AngleAxis(rot, Vector3.forward);
        if (Aim.IsPressed())
        {
            Rend.color = new Color(256, 256, 256, 256);
            PL_Col.offset = new Vector2(0f, 1.25f);
            PL_Col.size = new Vector2(3f, 9.5f);
            PL_Script.Overlap = new Vector2(3, 1);
            GR.localPosition = new Vector3(0f, -3f, 0);
            hand.SetActive(true);
            if (rot > 90 && rot < 270)
            {
                Rend.flipY = true;
                hand.transform.localPosition = new Vector3(hand.transform.localPosition.x, 0.25f, 0);
                if (hand.transform.childCount > 0)
                {
                    Transform gun = hand.transform.GetChild(0);
                    gun.GetComponent<SpriteRenderer>().flipY = true;
                }
            }
            else
            {
                Rend.flipY = false;
                hand.transform.localPosition = new Vector3(hand.transform.localPosition.x, -0.25f, 0);
                if (hand.transform.childCount > 0)
                {
                    Transform gun = hand.transform.GetChild(0);
                    gun.GetComponent<SpriteRenderer>().flipY = false;
                }
            }
        }
        else
        {
            Rend.color = new Color(256, 256, 256, 0);
            PL_Col.offset = new Vector2(-1.125f, 2.5f);
            PL_Col.size = new Vector2(13.75f, 5f);
            PL_Script.Overlap = new Vector2(6, 1);
            GR.localPosition = Vector3.zero;
            hand.SetActive(false);
        }
    }
    private void tpStand()
    {
        PL_Trans.position = PL_Trans.position + new Vector3(0, 3.5f, 0);
    }
    private void deTpStand()
    {
        PL_Trans.position = PL_Trans.position - new Vector3(0, 3.5f, 0);
    }
}
