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

[RequireComponent(typeof(Rigidbody))]
public class Torch : MonoBehaviour {
    public Transform FirePoint;
    public ParticleSystem Particles;
    public Light Light;
    public Color NormalColor;
    public Color PanicColor;
    public float Lifetime;
    public float PanicLifetimeThreshold;
    public float PassTorchAnimationDuration;
    public float IgniteDuration;

    [Header("Sound effects")]
    public AudioSource TorchSound;
    public AudioClip[] FireClips;

    public bool Burning {
        get {
            return Lifetime > 0;
        }
    }

    private Rigidbody Body;

    private Quaternion PassRotation = Quaternion.LookRotation(Vector3.forward, (Vector3.right + Vector3.forward).normalized);
    private bool PassingTorch = false;
    private float TorchPassStarted = 0;
    private Transform PassTo;
    private Vector3 PassStartPosition;
    private Quaternion PassStartRotation;

    private Vector3 BaseScale;
    private float StartLifetime;

    private void Awake() {
        Body = GetComponent<Rigidbody>();
        BaseScale = transform.localScale;
        StartLifetime = Lifetime;
    }

    private void Update() {
        if (PassingTorch) {
            if (PassTo != null) {
                // Move to other, to-be-enflamed torch
                transform.position = Vector3.Lerp(
                    PassStartPosition,
                    PassTo.position - transform.rotation * FirePoint.localPosition,
                    (Time.time - TorchPassStarted) / PassTorchAnimationDuration);
                transform.localRotation = Quaternion.Slerp(
                    PassStartRotation,
                    PassRotation,
                    (Time.time - TorchPassStarted) / PassTorchAnimationDuration * 2f);
            }
            if (Time.time - TorchPassStarted >= IgniteDuration) {
                Lifetime = 0;
            }
        } else if (Burning) {
            // Move to origin
            transform.localPosition = Vector3.Lerp(transform.localPosition, Vector3.zero, 12f * Time.deltaTime);
            transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.LookRotation(Vector3.forward, Vector3.up), 12f * Time.deltaTime);
        }

        Lifetime -= Time.deltaTime;
        if (Lifetime <= 0) {
            Particles.Stop();
            Light.color = Color.Lerp(Light.color, Color.black, 4f * Time.deltaTime);
            TorchSound.volume = Mathf.Lerp(TorchSound.volume, 0, 8f * Time.deltaTime);
            if (Light.color == Color.black) {
                Light.enabled = false;
            }
            if (TorchSound.volume == 0) {
                TorchSound.enabled = false;
            }
        } else {
            bool Panic = Lifetime <= PanicLifetimeThreshold;
            Light.color = Color.Lerp(Light.color, Panic ? PanicColor : NormalColor, 4f * Time.deltaTime);

            if ((TorchSound.clip.length - TorchSound.time) <= Time.deltaTime || !TorchSound.isPlaying) {
                TorchSound.clip = FireClips[Random.Range(0, FireClips.Length)];
                TorchSound.time = 0;
                TorchSound.Play();
            }
            TorchSound.volume = 0.5f + 0.5f * (1f / (Mathf.Max(0, StartLifetime + 0.5f - Lifetime) * 4f + 1)) - (Panic ? 0.3f : 0);
        }
    }

    public void PassTorch(Transform target) {
        PassingTorch = true;
        PassTo = target;
        TorchPassStarted = Time.time;
        PassStartPosition = transform.position;
        PassStartRotation = transform.localRotation;
    }

    public void Drop() {
        Body.isKinematic = false;
        transform.parent = GameObject.FindGameObjectWithTag("World").transform;
    }
}
