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

public class Player : MonoBehaviour {

    private GameManager gameManager;

    public enum Player_number {
        one, two 
    }

    public float speed = 6.0f;
    public int lives = 1;

    public float reloadSpeed = 1.5f;
    private bool canShoot = true;

    public Transform shootingPoint;
    public GameObject projectile;
    public Transform projectileHolder;

    public Player_number playerNumber;

    private TargetFinder targetFinder;
    private CharacterController characterController;
    private Vector3 moveDirection = Vector3.zero;

    public Animator animator;

    public Text text;

    public AudioSource audioSource;
    public AudioClip fireSound;
    public AudioClip dieSound;

    void Start() {
        characterController = GetComponent<CharacterController>();
        targetFinder = GetComponent<TargetFinder>();
        gameManager = FindObjectOfType<GameManager>();

        UpdateText();
    }

    void Update() {
        if (gameManager.IsGamePaused())
            return;

        if (IsAlive()) {
            Move();
            
            if (canShoot && IsShootDown())
                Shoot();
        }
    }

    void Move() {
        if (playerNumber == Player_number.one) {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
        }
        else if (playerNumber == Player_number.two) {
            moveDirection = new Vector3(Input.GetAxis("Horizontal_2"), 0.0f, Input.GetAxis("Vertical_2"));
        }

        transform.LookAt(transform.position + moveDirection);

        moveDirection *= speed;

        animator.SetBool("isRunning", moveDirection.magnitude > 0);

        characterController.Move(moveDirection * Time.deltaTime);
    }

    void Shoot() {
        Debug.Log("Bang!");

        GameObject target = targetFinder.GetClosest();

        if (target != null) {
            audioSource.PlayOneShot(fireSound);
            UpdateText();
            GameObject newProjectile = Instantiate(
                projectile, 
                new Vector3(shootingPoint.transform.position.x, 2, shootingPoint.transform.position.z),
                transform.rotation, 
                projectileHolder
            );
            newProjectile.GetComponent<Projectile>().SetTarget(target);
            canShoot = false;
            StartCoroutine("Reload");
        }
    }

    IEnumerator Reload() {
        UpdateText();
        yield return new WaitForSeconds(reloadSpeed);
        canShoot = true;
        Debug.Log("Player " + playerNumber + " can shoot");
        UpdateText();
    }

    bool IsShootDown() {
        return 
            (playerNumber == Player_number.one && Input.GetAxis("Fire1") == 1) || 
            (playerNumber == Player_number.two && Input.GetAxis("Fire2") == 1);
    }

    public void Hit() {
        if (IsAlive()) {
            Debug.Log("Hit");

            lives -= 1;

            if (!IsAlive())
                Death();
        }
    }

    void Death() {
        audioSource.PlayOneShot(dieSound);
        Debug.Log("Kill " + playerNumber);
        animator.SetBool("isDead", true);
        FindObjectOfType<GameManager>().KillPlayer();
        transform.tag = "Untagged";
    }

    public bool IsAlive() {
        return lives > 0;
    }

    void UpdateText() {
        text.text = (canShoot ? "1" : "0") + "/1";
    }
}


