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

public class Station : MonoBehaviour
{
    public float maxPower;
    public Transform gauge;
    public float powerSuckDelay = 1;
    public float powerLevel;
    public float randomRayOffset;

    public LineRenderer lineRenderer;

    private IEnumerator drainCoroutine;
    private Transform lineTarget;
    public float maxBeamWidth;
    public float beamWidthFadeSeconds;

    // Use this for initialization
	void Start ()
	{
	    powerLevel = 0;
	    lineTarget = lineRenderer.transform;
        var midpoint =
            (lineRenderer.transform.position +
            lineTarget.position)/2;
        lineRenderer.SetPositions(new Vector3[]{
            lineRenderer.transform.position,
            midpoint,
            lineTarget.position,
        });
	    lineRenderer.endWidth = 0;
	}
	
	// Update is called once per frame
	void Update () {
		UpdatePowerBar();
	}

    void PowerUp(float delta)
    {
        powerLevel += delta;

        if (powerLevel >= maxPower)
            Debug.Log("Station at max power!");

        UpdatePowerBar();
    }

    private void UpdatePowerBar()
    {
        var level = powerLevel / maxPower;
        gauge.localScale = new Vector3(1, level, 1);
        var midpoint =
            (lineRenderer.transform.position +
            lineTarget.position)/2;
        lineRenderer.SetPosition(1, midpoint + randomRayOffset * Random.onUnitSphere);
        lineRenderer.SetPosition(2, lineTarget.position);
        lineRenderer.material.mainTextureOffset = new Vector2((4 * Time.time) % 1, 0);
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            lineRenderer.enabled = true;
            lineTarget = other.transform;
            drainCoroutine = DrainPower(other.GetComponent<Player>());
            StartCoroutine(drainCoroutine);
            StartCoroutine(LerpBeamEndWidth(maxBeamWidth, beamWidthFadeSeconds));
        }
    }

    private IEnumerator LerpBeamEndWidth(float tarvetValue, float duration)
    {
        float startValue = lineRenderer.endWidth;
        for (float t = 0; t < duration; t += Time.deltaTime)
        {
            lineRenderer.endWidth = Mathf.Lerp(startValue, tarvetValue, t / duration);
            yield return null;
        }
        if (Mathf.Approximately(tarvetValue, 0))
        {
            lineRenderer.enabled = false;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            //lineTarget = lineRenderer.transform;
            if (drainCoroutine != null)
                StopCoroutine(drainCoroutine);
            StartCoroutine(LerpBeamEndWidth(0, beamWidthFadeSeconds));
        }
    }

    private IEnumerator DrainPower(Player player)
    {
        

        while (true)
        {
            yield return new WaitForSeconds(powerSuckDelay);

            switchLineRenderer(player);

            if (player.PowerLevel >= 2f)
            {
                if (powerLevel >= maxPower)
                {
                    yield break;
                }
                
                PowerUp(1);
                player.PowerLevel -= 1f;
                UpdatePowerBar();
            }
        }
    }

    private void switchLineRenderer(Player player)
    {
        lineRenderer.enabled = false;
        if (player != null)
        {
            if (player.PowerLevel >= 2f)
            {
                lineRenderer.enabled = true;
            }
        }
    }
}
