using UnityEngine;
using System.Collections.Generic;

public class RangedAttackBehaviour: MonoBehaviour
{
    [SerializeField]
    private float attackDuration;

    [SerializeField]
    private float firstCooldownTime = 3.0f;

    [SerializeField]
    private float cooldownTime;

    [SerializeField]
    private float hitTime;

    [SerializeField]
    private TriggerTracker[] hitVolumes = new TriggerTracker[0];

	[SerializeField]
	private float chargeUpMax;

	private float chargeUp;

    private Robot myRobot { get; set; }

    private Animator myAnimator { get; set; }

	[SerializeField]
	private Hero hero;

	public bool triggered = false;

    public bool isAttacking { get; private set; }

    private float attackTime { get; set; }

    private float cooldownTimeLeft { get; set; }

	bool isActive = false;

	public AudioClip hitSound;

	void Start()
    {
        myRobot = GetComponent<Robot>();
        myAnimator = GetComponent<Animator>();
		cooldownTimeLeft = firstCooldownTime;
        isAttacking = false;

        if (hero == null)
        {
            hero = FindObjectOfType<Hero>();
        }

		if (myAnimator != null)
		{
			myAnimator.Play("anim_mchammer_die");
		}
    }

    void Update()
    {
		if (!isActive) return;
        if (myRobot == null || hero == null)
        {
            return;
        }
	    if (!isAttacking)
	    {
		    cooldownTimeLeft -= Time.deltaTime;
	    }

        if (!isAttacking && triggered && cooldownTimeLeft <= 0.0f)
        {
            StartAttack();
        }

        UpdateAttack();

        if (!isAttacking)
        {
            cooldownTimeLeft -= Time.deltaTime;
        }
    }

	void chargeAttack()
	{
		chargeUp--;
		if (chargeUp <= 0)
			StartAttack ();
	}

    void StartAttack()
    {
        if (isAttacking)
        {
            Debug.LogError("Already attacking");
            return;
        }
		rigidbody.velocity = new Vector3(0,0,0);
		rigidbody.angularVelocity = new Vector3(0,0,0);

        if (myAnimator != null)
        {
            myAnimator.Play("anim_mchammer_attack");
        }

		if(audio != null)
		{
			//audio.clip = hitSound;
			//audio.Play ();
			audio.PlayOneShot(hitSound);
		}

        isAttacking = true;
        attackTime = 0.0f;
		cooldownTimeLeft = cooldownTime;
    }

    void UpdateAttack()
    {
        if (!isAttacking)
        {
            return;
        }

        float oldAttackTime = attackTime;
        attackTime += Time.deltaTime;

        if (oldAttackTime < hitTime && attackTime >= hitTime)
        {
            DoHit();
        }

        if (attackTime >= attackDuration)
        {
            StopAttack();
        }
    }

    static HashSet<Collider> DoHit_HitColliders = new HashSet<Collider>();

    void DoHit()
    {
        foreach (TriggerTracker hitVolume in hitVolumes)
        {
            foreach (Collider collider in hitVolume.colliders)
            {
                DoHit_HitColliders.Add(collider);
            }
        }

        foreach (Collider collider in DoHit_HitColliders)
        {
			// Do something
        }

        DoHit_HitColliders.Clear();
    }

    void StopAttack()
    {
        if (myAnimator != null)
        {
            myAnimator.Play("anim_mchammer_walk");
        }
        isAttacking = false;
        cooldownTimeLeft = cooldownTime;
    }

	public void activate()
	{
		if (!isActive)
		{
			myAnimator.Play("anim_mchammer_walk");
			isActive = true;
		}
	}
	public void deactivate()
	{
		isActive = false;
	}

	void OnTriggerStay(Collider other)
	{
		if (other.gameObject.tag == "Hero") triggered = true;
	}

	void OnTriggerExit(Collider other)
	{
		if (other.gameObject.tag == "Hero") {
				triggered = false;
			cooldownTimeLeft = cooldownTime;
		}
	}
}
