using UnityEngine;
using System.Collections;

//Points and fires a gun at the player regardless of height
public class SAM_Controller : MonoBehaviour
{
    public PlayerController attackTarget;
    public GameObject prefab_debrisOnDeath;

    [Space(18)] 
    [Header("Shooting")] 
    public Vector2 MinMax_DelayBeforeShoot;
    private float fTimeTillNextShot;

    private bool bDead = false;

    void Awake()
    {
        fTimeTillNextShot = Random.Range(MinMax_DelayBeforeShoot.x, MinMax_DelayBeforeShoot.y);
    }

	void Start () 
    {
	}
	
	// Update is called once per frame
	void Update () 
    {
        //attack the player always for now
	    attackTarget = attackTarget == null ? GameObject.Find("Player").GetComponent<PlayerController>() : attackTarget;

        //maybe shoot at it
        UpdateWeapons(attackTarget);

    }

    protected virtual void UpdateWeapons(PlayerController attackTarget)
    {
        if (attackTarget == null)
            return;

        Vector3 vToTarget = attackTarget.transform.position - transform.position;
        if (vToTarget.sqrMagnitude > 50.0f*50.0f)
            return;

        TM_Weapon weapon = GetComponent<TM_Weapon>();
        weapon.transform.up = vToTarget.normalized;

        fTimeTillNextShot -= Time.deltaTime;
        if (fTimeTillNextShot <= 0.0f)
        {
            weapon.StartBurst();
            fTimeTillNextShot = Random.Range(MinMax_DelayBeforeShoot.x, MinMax_DelayBeforeShoot.y);
        }
    }
}
