// Benjamin Gordon, 2016

// This script is placed on the particle effects spawned from objects that are destroyed.
// This handles the actual forces behind the explosion, as well as sound effects.

using UnityEngine;

[RequireComponent(typeof(ParticleSystem))]
[RequireComponent(typeof(AudioSource))]
//[RequireComponent(typeof(Collider))]
public class ExplodeParticleManager : MonoBehaviour {

    public float knockbackForce;
    public float explodeRadius;
    public bool autoGenerateExplosionSize = true;
    public bool autoGenerateParticleSpeed = true;
    public bool autoGenerateKnockBackForce = true;
    public float explodeSpeed;
    public float explodeTimer;
    public Material partMat;

    public AudioClip breakSound;
    private SphereCollider explodeCol; // Collider used for explosion.

	void Start () {
        //Time.timeScale = 0.1f;

        GetComponent<AudioSource>().PlayOneShot(breakSound);
        ParticleSystem part = GetComponent<ParticleSystem>();

        explodeCol = GetComponent<SphereCollider>();
        explodeCol.isTrigger = true;

        gameObject.GetComponent<ParticleSystemRenderer>().material = partMat;

        Destroy(explodeCol, explodeTimer);
        Destroy(gameObject, part.startLifetime);

        if(autoGenerateParticleSpeed) {
            part.startSpeed = explodeRadius * 1.75f;
        }

        if(autoGenerateKnockBackForce) {
            knockbackForce = explodeRadius * 4.5f;
            //Debug.Log("Force: " + knockbackForce);
        }
	}

    void Update () {
        //transform.localScale = Vector3.Lerp(transform.localScale, explodeRadius, explodeSpeed * Time.deltaTime);
        if(explodeCol) {
            //explodeCol.bounds.Expand(Vector3.Lerp(explodeCol.bounds.extents, explodeDiameter, explodeSpeed * Time.deltaTime));
            explodeCol.radius = (Mathf.Lerp(explodeCol.radius, explodeRadius, explodeSpeed * Time.deltaTime));
            //Debug.Log(explodeCol.bounds.size);
        }
    }

    void OnDrawGizmos() {
        if(explodeCol && DebugGM.DrawExplodeWireframes)
            Gizmos.DrawWireSphere(transform.position, explodeCol.radius);
    }

    void OnTriggerEnter(Collider other) {

        RaycastHit hit;

        //Debug.Log("Hitting: " + other.gameObject.name);

        // If there is a line of sight to collided object...
        if (Physics.Linecast(transform.position, other.gameObject.transform.position, out hit)) {

            if(hit.collider == other) {
                // If an object comes into contact with explosion and has a rigidbody...
                Rigidbody rb = other.GetComponent<Rigidbody>();       

                if(rb) {
                    // Send the thing flying!
                    rb.AddForce((other.gameObject.transform.position - transform.position).normalized * knockbackForce, ForceMode.Impulse);

                    // If collided object is a player, make them a ragdoll so they do not negate explosion force.
                    if (other.gameObject.tag == "Player") {
                        other.GetComponent<ShrapnelGamePlayerMover>().RagDolled = true;
                    }
                }
            }
        }
    }
}
