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

public class PlayerController : MonoBehaviour
{
    [SerializeField] float moveSpeed;

    Rigidbody myRigidBody;

    [SerializeField] GameObject bombPrefab;

    private GameManager myGameManager;

    [SerializeField] private int maxBombs = 2;
    private int currentBombsPlaced = 0;

    private bool hasControl = true;
    [SerializeField] private float destroyTime = 2f;

    private bool isDefeated = false;

    [SerializeField] private LayerMask whatAreBombLayers;

    private Animator myAnimator;
    private AudioSource myAudioSource;

    [SerializeField] private AudioClip playerDefeatSound;

    // Start is called before the first frame update
    void Start()
    {
        myRigidBody = GetComponent<Rigidbody>();
        myAnimator = GetComponent<Animator>();
        myGameManager = FindObjectOfType<GameManager>();
        myAudioSource = GetComponent<AudioSource>();

    }

    // Update is called once per frame
    void Update()
    {
        if(hasControl)
        {
            Movement();
            Rotation();
            UpdateAnimator();
            PlaceBomb();
        }

    }

    private void Rotation()
    {
        if(myRigidBody.velocity != Vector3.zero)
        {
            transform.forward = myRigidBody.velocity;
        }
        
    }

    void Movement()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float z = Input.GetAxisRaw("Vertical");

        Vector3 newVelocity = new Vector3(x * moveSpeed, 0f, z * moveSpeed);
        myRigidBody.velocity = newVelocity;

    }

    private void PlaceBomb()
    {
        if(Input.GetButtonDown("Fire1") && currentBombsPlaced < maxBombs)
        {
            Vector3 center = new Vector3(Mathf.Round(transform.position.x), 0f, Mathf.Round(transform.position.z));

            Collider[] hitColliders = Physics.OverlapSphere(center, 0.5f, whatAreBombLayers);
            if(hitColliders.Length > 0)
            {
                return;
            }

            GameObject bomb = Instantiate(bombPrefab, transform.position, Quaternion.identity);
            bomb.transform.position = center;
            currentBombsPlaced++;
        }
    }

    public void Defeated()
    {
        if(!isDefeated)
        {
            isDefeated = true;

            myAudioSource.PlayOneShot(playerDefeatSound, 1f);

            hasControl = false;

            myRigidBody.velocity = Vector3.zero;

            myRigidBody.isKinematic = true;

            Destroy(gameObject, destroyTime);

            myGameManager.PlayerDefeated();
        }

    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Enemy")
        {
            Defeated();
        }
    }

    public void BombExploded()
    {
        currentBombsPlaced--;
    }

    public float GetDestroyDelayTime()
    {
        return destroyTime;
    }

    private void UpdateAnimator()
    {
        if(myRigidBody.velocity == Vector3.zero)
        {
            myAnimator.SetBool("isWalking", false);
        }
        else
        {
            myAnimator.SetBool("isWalking", true);
        }
    }

}
