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

public class PlayerHealth : MonoBehaviour
{
    public RespawnManager respawnManager;

    public float startingHealth = 100f;
    public Slider healthSlider;
    public Text numberOfKillsText;

    public Color healthSliderColor;

    public Transform DeathPrefab;

    private float currentHealth;
    
    private bool dead;

    public int numberOfDeaths;
    public int numberOfKills;
    public int numberOfSuicides;
    public int respawnTimer = 3;

    private void Awake()
    {
        numberOfDeaths = 0;
    }

    private void Start()
    {
        numberOfDeaths = 0;
        numberOfKills = 0;
        //numberOfKillsText.text = "" + numberOfKills;

        respawnManager = GameObject.Find("RespawnManager").GetComponent<RespawnManager>();
    }

    private void OnEnable()
    {
        currentHealth = startingHealth;

        dead = false;

        UpdateUI();
    }

    // Returns true if damage kills
    public void TakeDamage(float amount, PlayerController sender)
    {
        currentHealth -= amount;

        if (currentHealth <= 0f && !dead)
        {
            OnDeath();

            if (sender)
            {
                sender.GetComponent<PlayerHealth>().AddKill();
                FindObjectOfType<StatsManager>().AddKill(sender.PlayerId);
            }
        }

        gameObject.GetComponent<PlayerController>().TakeDamage();

        UpdateUI();
    }

    public void AddKill()
    {
        numberOfKills++;
        UpdateUI();
    }

    private void UpdateUI()
    {
        if (healthSlider != null) 
        {
            float sliderValue = (currentHealth / startingHealth);
            healthSlider.value = sliderValue;
        }

        if (numberOfKillsText != null) 
        {
            numberOfKillsText.text = numberOfKills.ToString();
        }
    }

    public void Die()
    {
        currentHealth = 0;
        dead = true;
        numberOfDeaths++;
        numberOfSuicides++;

        gameObject.GetComponent<PlayerController>().Die();
        UpdateUI();
        
        SpriteRenderer[] sprites = gameObject.GetComponentsInChildren<SpriteRenderer>();
        foreach (var sprite in sprites)
        {
            sprite.enabled = false;
        }
        Collider2D[] colliders = gameObject.GetComponentsInChildren<Collider2D>();
        foreach (var collider in colliders)
        {
            collider.enabled = false;
        }
        Rigidbody2D[] rigidbodies = gameObject.GetComponents<Rigidbody2D>();
        foreach (var rigidbody in rigidbodies)
        {
            rigidbody.isKinematic = true;
        }
        StartCoroutine(Respawn());
    }

    private void OnDeath()
    {
        dead = true;
        numberOfDeaths++;

        Instantiate(DeathPrefab, transform.position, transform.rotation);
        SpriteRenderer[] sprites = gameObject.GetComponentsInChildren<SpriteRenderer>();
        foreach (var sprite in sprites)
        {
            sprite.enabled = false;
        }
        Collider2D[] colliders = gameObject.GetComponentsInChildren<Collider2D>();
        foreach (var collider in colliders)
        {
            collider.enabled = false;
        }
        Rigidbody2D[] rigidbodies = gameObject.GetComponents<Rigidbody2D>();
        foreach (var rigidbody in rigidbodies)
        {
            rigidbody.isKinematic = true;
        }

        StartCoroutine(Respawn());
    }

    IEnumerator Respawn()
    {
        yield return new WaitForSeconds(respawnTimer);

        int spawnIndex = Random.Range(0, respawnManager.spawnPositions.Length);
        gameObject.transform.position = respawnManager.spawnPositions[spawnIndex].transform.position;

        OnEnable();

        SpriteRenderer[] sprites = gameObject.GetComponentsInChildren<SpriteRenderer>();
        foreach (var sprite in sprites)
        {
            sprite.enabled = true;
        }
        Collider2D[] colliders = gameObject.GetComponentsInChildren<Collider2D>();
        foreach (var collider in colliders)
        {
            collider.enabled = true;
        }
        Rigidbody2D[] rigidbodies = gameObject.GetComponents<Rigidbody2D>();
        foreach (var rigidbody in rigidbodies)
        {
            rigidbody.isKinematic = false;
        }
    }
}
