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

public class PlayerController : MonoBehaviour{

    [Header("Statistics")]
    public float speed;
    public float shootingSpeed;
    public float invulnerableTime = 2f;
    [HideInInspector]
    public float defaultSpeed;
    [HideInInspector]
    public int gunSelected;
    [Header("Guns")]
    public GameObject[] gun;

    GameController gameController;
    Animator anim;
    PlayerHealth health;
    bool dead;

    Rigidbody2D rb2d;

    PlayerInput playerInput;
    string currentControlScheme;
    Vector2 aim;
    Vector2 mov;

    #region Input
    public void PlayerMove(InputAction.CallbackContext state){
        mov = state.ReadValue<Vector2>();
    }
    public void PlayerAim(InputAction.CallbackContext state){
        aim = state.ReadValue<Vector2>();
    }
    #endregion

    // Start is called before the first frame update
    void Awake(){
        playerInput = GetComponent<PlayerInput>();
        gameController = FindObjectOfType<GameController>();
        anim = GetComponent<Animator>();
        health = GetComponent<PlayerHealth>();
        defaultSpeed = speed;
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update(){
        RotateGun();

        if(health.hp <= 0 && !dead){
            Time.timeScale = 0;
            ObjectPooler.Instance.SpawnFromPool("PlayerDeath", transform.position, Quaternion.identity);
            Camera.main.GetComponent<Animator>().SetBool("GameOver", true);
            dead = true;
            AudioManager.instance.Stop("MainTheme");
            gameObject.SetActive(false);
        }

        if(playerInput.currentControlScheme != currentControlScheme){
            currentControlScheme = playerInput.currentControlScheme;
        }
    }

    void RotateGun(){
        if(currentControlScheme == "Gamepad"){
            if(aim.magnitude > .1f){
                Quaternion newRot = Quaternion.Euler(0f, 0f, Mathf.Atan2(aim.y, aim.x) * Mathf.Rad2Deg);
                gun[gunSelected].transform.rotation = newRot;
            }
        }else{
            Vector2 mousePos = Camera.main.ScreenToWorldPoint(aim) - transform.position;
            float rotZ = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
            gun[gunSelected].transform.rotation = Quaternion.Euler(0f, 0f, rotZ);
        }
    }

    void FixedUpdate(){
        rb2d.MovePosition(rb2d.position + (Vector2.ClampMagnitude(mov, 1f) * speed * Time.deltaTime));
    }

    void OnTriggerEnter2D(Collider2D col){
        if(col.CompareTag("Gun1")){
            gunSelected = 0;
            for (int i = 0; i < gun.Length; i++){
                if (i == gunSelected){
                    gun[i].SetActive(true);
                }else{
                    gun[i].SetActive(false);
                }
            }
        }
        if(col.CompareTag("Gun2")){
            gunSelected = 1;
            for (int i = 0; i < gun.Length; i++){
                if (i == gunSelected){
                    gun[i].SetActive(true);
                }else{
                    gun[i].SetActive(false);
                }
            }
        }
        if(col.CompareTag("Gun3")){
            gunSelected = 2;
            for (int i = 0; i < gun.Length; i++){
                if (i == gunSelected){
                    gun[i].SetActive(true);
                }else{
                    gun[i].SetActive(false);
                }
            }
        }
    }
    bool damaged;
    void OnCollisionEnter2D(Collision2D col){
        if(col.gameObject.CompareTag("Enemy") && !damaged || col.gameObject.CompareTag("BigEnemy") && !damaged){
            anim.SetBool("Damaged", true);
            if(gameController.level == 2){
                health.TakeDamage(6);
            }else if(gameController.level == 3){
                health.TakeDamage(7);
            }else{
                health.TakeDamage(5);
            }
            AudioManager.instance.Play("Hit");
            Invoke(nameof(Damaged), invulnerableTime);
            damaged = true;
        }
        if (col.gameObject.CompareTag("RocketEnemy")){
            anim.SetBool("Damaged", true);
            if (gameController.level == 2){
                health.TakeDamage(10);
            }else if (gameController.level == 3){
                health.TakeDamage(13);
            }else{
                health.TakeDamage(7);
            }
            AudioManager.instance.Play("Hit");
            Invoke(nameof(Damaged), invulnerableTime);
            damaged = true;
        }
    }

    void Damaged(){
        anim.SetBool("Damaged", false);
        damaged = false;
    }

    void OnCollisionStay2D(Collision2D col){
        if(col.gameObject.CompareTag("CircleEnemy") && !damaged){
            if(col.gameObject.GetComponent<CircleEnemyController>().hp == 2){
                health.TakeDamage(0.1f);
            }else if(col.gameObject.GetComponent<CircleEnemyController>().hp == 1){
                health.TakeDamage(0.05f);
            }
        }
    }
}
