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

public class MachineGunScript : MonoBehaviour
{
    //bool firingMode = false;
    EnemyControl EC;
    GameObject target;

    public new Camera camera;
    public GameObject bullet;
    public int damage;
    public float range;
    public float reload;

    public GameObject vehicle;
    public Vector3 offset;

    float timer;
    PoweredObject power;

    public Text modeDisplay;

    void Start()
    {
        EC = GameObject.Find("EnemyControlObject").GetComponent<EnemyControl>();
        timer = reload;
        power = GetComponent<PoweredObject>();
    }

    void Update()
    {
        transform.position = vehicle.transform.position + offset;
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //if (firingMode)
            //{
            //    firingMode = false;
            //}
            //else
            //{
            //    firingMode = true;
            //    modeDisplay.text = "Attack Mode";
            //
            //}
        }
        timer += Time.deltaTime;
        //print(timer);
        #region MouseAim
        //Vector3 mousePos = camera.ViewportToWorldPoint(Input.mousePosition)/* + new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0.0f)*/;
        //mousePos.Normalize();
        //transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg);
        #endregion
        #region AutoAim
        if (target == null)
        {
            for (int i = 0; i < EC.enemies.Count; i++)
            {
                if (target == null || Vector3.Distance(this.gameObject.transform.position, EC.enemies[i].transform.position) < Vector3.Distance(this.gameObject.transform.position, target.transform.position))
                {
                    //if (Vector3.Distance(this.gameObject.transform.position, EC.enemies[i].transform.position) <= range) 
                    //{
                        target = EC.enemies[i];
                    //}
                }
            }
        }
        else
        {
            Vector3 targetPos = target.transform.position;
            //float hyp = Mathf.Pow(Mathf.Pow((targetPos.x - transform.position.x), 2) + Mathf.Pow((targetPos.y - transform.position.y),2),0.5f);
            float dis = Vector3.Distance(this.gameObject.transform.position, target.transform.position);
            float opp = targetPos.y - transform.position.y;
            transform.rotation = Quaternion.Euler(0, 0, -90 + (Mathf.Sin(opp / dis) * Mathf.Rad2Deg));
            //print(dis + " - " + opp + " - " + (opp / dis) + " - " + Mathf.Sin(opp / dis) * Mathf.Rad2Deg);
            //transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(targetPos.y, targetPos.x) * Mathf.Rad2Deg);
        }
        #endregion
        if (/*Input.GetKey(KeyCode.Mouse0) && */ power.GetPower() > 0 && timer > (reload * Mathf.Pow(power.GetPower(), -1.0f)) && target != null /*&& firingMode == true*/)
        {
            GameObject newBullet = Instantiate(bullet, transform.position, Quaternion.identity);
            newBullet.AddComponent<Rigidbody2D>();
            newBullet.GetComponent<Rigidbody2D>().gravityScale = 0.0f;
            newBullet.GetComponent<Rigidbody2D>().mass = 1.0f;
            newBullet.AddComponent<BulletScript>();
            newBullet.GetComponent<BulletScript>().SetDamage(damage);
            newBullet.tag = this.tag;
            newBullet.GetComponent<Rigidbody2D>().AddForce(transform.up * 1000 * power.GetPower() * Mathf.Pow(newBullet.GetComponent<Rigidbody2D>().mass, -1.0f) * Time.deltaTime, ForceMode2D.Impulse);
            timer = 0.0f;
        }
    }
}
