// Benjamin Gordon, 2016
 
// This script goes on every single object that can be destroyed with guns.
// It allows that object to take damage and create a particle effect with a properly sized explosion and correct material color.

using UnityEngine;
 
public class ParticleDestructibleObject : MonoBehaviour
{
    public GameObject shrapnelPrefab;
    
    private Color ExplodyColor;
    private Color normalColor;
    private Renderer myRenderer;

    [SerializeField] private int maxHealth = 50;
    [SerializeField] private bool autoGenExplodeRadius = false;
    [SerializeField] private float explodeRadius = 50;

    private int curHealth = 50;

    protected bool destroyed = false;

    void Start () {

        // Most chill warning message ever. Happens when you attach this script but dont set the tag to "Destructible".
        if(gameObject.tag != "Destructible") {
            Debug.LogWarning("Hey, your '" + gameObject.name + 
                "' doesn't have the tag 'Destructible', yet has the 'ParticleDestructibleObject' script attached." +
                "\n" + "We're gonna change the tag for you this time, but you should really check it out next time.");

            gameObject.tag = "Destructible";
        }

        curHealth = maxHealth;
        myRenderer = GetComponent<Renderer>();
        normalColor = myRenderer.material.color;
        ExplodyColor = Color.red;
    }

    public void TakeExplodyDamage(int value) {

        curHealth -= value;

        if (curHealth <= 0)
            Explode();
        else
            AlterColor();
    }

    public void AlterColor() {
        float ratio = maxHealth / curHealth;

        Color newColor = (normalColor + ExplodyColor * ratio);

        //myRenderer.material.color = Color.Lerp(normalColor, newColor, 4 * Time.deltaTime);
        myRenderer.material.color = newColor;
    }

    public void Explode()
    {
        // Set color back to original
        myRenderer.material.color = normalColor;

        // Create particle effect
        GameObject shrapnelGO = (GameObject)Instantiate(shrapnelPrefab, transform.position, transform.rotation);

        // Subtract half the height of the object from the shrapnel's spawn point. Makes explosion appear on ground.
        shrapnelGO.transform.position = new Vector3(
            shrapnelGO.transform.position.x,
            transform.position.y - gameObject.GetComponent<Collider>().bounds.extents.y,
            shrapnelGO.transform.position.z
        );

        // Set up the particle's explosion size automatically, giving it twice the size of this object's collider.
        ExplodeParticleManager shrapnelPart = shrapnelGO.GetComponent<ExplodeParticleManager>();
        //if(autoGenExplodeRadius) {
        if(shrapnelPart.autoGenerateExplosionSize) {
            shrapnelPart.explodeRadius = gameObject.GetComponent<Collider>().bounds.extents.magnitude * 4;
            Debug.Log("Value: " + shrapnelPart.explodeRadius);
        }
        else
            shrapnelPart.explodeRadius = explodeRadius;

        // Give the particle effect the same material as this object.
        shrapnelPart.partMat = gameObject.GetComponent<Renderer>().material;

        // Destroy this gameobject.
        Destroy(gameObject);
    }
}
