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

public class MobLaser : MonoBehaviour
{

    private Rigidbody rb;
    private float y;
    private float speedMultiplier = 1;
    private float xMultiplier = 1;
    public float speed;
    public float maxUpwards;
    private bool loweringSpeed = false;
    public GameObject laserBuildup;
    public GameObject laser;
    public GameObject col;
    private float timeToWait = 0.1f;
    private bool finishedFirstShot = false;
    public int health;
    public float cooldown;
    private float currentCooldown;
    public GameObject explosion;
    public AudioClip clip;
    // Use this for initialization
    void Start()
    {
        rb = this.GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        if (speedMultiplier <= 0)
        {
            //xMultiplier makes it so the laser will stay put in place after it starts charging
            xMultiplier = 0;
            //We are gonna start charging the laser here
        }
        if (finishedFirstShot == true)
        {
            if (currentCooldown <= 0)
            {
                currentCooldown = cooldown;
                StopCoroutine(LowerSpeed());
                StartCoroutine(LowerSpeed());
            }
            else
            {
                currentCooldown -= Time.deltaTime;
            }
        }
    }
    void FixedUpdate()
    {
        y = Mathf.PingPong(Time.time * 4, maxUpwards * 2);
        rb.velocity = new Vector3(speed * xMultiplier, y - maxUpwards, 0) * speedMultiplier;
    }
    public void TakeDamage(int a)
    {
        health -= a;
        if (health <= 0)
        {
            GameObject newExp = Instantiate(explosion, this.transform.position, Quaternion.identity);
            Destroy(newExp, 0.25f);
            AudioSource.PlayClipAtPoint(clip, this.transform.position);
            Destroy(this.gameObject);
        }
    }

    private void OnTriggerEnter(Collider other)
    {

        if (other.tag == "LaserThreshold")
        {
            if (loweringSpeed == false)
            {
                loweringSpeed = true;
                StartCoroutine(LowerSpeed());
            }
        }
    }
    IEnumerator LowerSpeed()
    {
        for (float i = 1; i > 0; i -= 0.05f)
        {
            speedMultiplier = i;
            yield return new WaitForSeconds(timeToWait);
        }
        speedMultiplier = 0;
        ChargeLaser();
        yield return new WaitForSeconds(1);
        ShootLaser();
        yield return new WaitForSeconds(3);
        EndLaser();
    }
    public void ChargeLaser()
    {
        laserBuildup.SetActive(true);
    }
    public void ShootLaser()
    {
        col.SetActive(true);
        laser.SetActive(true);
    }
    public void EndLaser()
    {
        col.SetActive(false);
        laserBuildup.SetActive(false);
        laser.SetActive(false);
        //loweringSpeed = false;
        speedMultiplier = 1;
        timeToWait = Random.Range(0.1f, 0.2f);
        maxUpwards = Random.Range(2, 5);
        //StartCoroutine(LowerSpeed());
        finishedFirstShot = true;
    }
}
