using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {
    public float speed = 2f;
    public float angularspeed = 180f;
    public float targetMinDistance = 0f;

    public float clashDamage = 10;

    public GameObject projectile;
    public float shootRange = 6f;
    public float shootDelay = 0.8f;
    float shootCD = 0;

    int sideway = 0;

    //public bool sideMovement = false;

    Transform _player;
	// Use this for initialization
	void Start () {
        _player = GameObject.FindGameObjectWithTag("Player").transform;
        sideway = Random.Range(0, 1);
        if (sideway == 0) sideway = -1;
	}
	
	// Update is called once per frame
	void Update () {
        Control();

        if (projectile != null) {
            Vector2 dist = _player.transform.position - this.transform.position;
            /*
            if (dist.magnitude < shootRange && shootCD <= 0)
            {
                if (Mathf.DeltaAngle(Vector2.Angle(dist, this.transform.up), 0) < 5 || Mathf.DeltaAngle(Vector2.Angle(dist, this.transform.up), 180) < 5)
                {
                    GameObject proj = (GameObject)Instantiate(projectile, this.transform.position, this.transform.rotation);
                    shootCD = shootDelay;
                }
            }
            */

            RaycastHit2D hit = Physics2D.Raycast(this.transform.position + this.transform.up, this.transform.up);
            //Debug.Log(hit.point);
            if ( (hit.collider.gameObject.tag == "Player" || hit.collider.gameObject.tag == "Shield") && dist.magnitude < shootRange && shootCD <= 0)
            {
                GameObject proj = (GameObject)Instantiate(projectile, this.transform.position, this.transform.rotation);
                proj.name = "Projectile";
                shootCD = shootDelay;
            }
            else if (hit.collider.gameObject.tag == "Enemy")
            {
                this.transform.position += sideway * 1.5f * Vector3.right * Time.deltaTime;
            }
            else shootCD -= Time.deltaTime;
        }

	}

    void Control()
    {
        if (_player == null)
        {
            if (GameObject.FindGameObjectWithTag("Player") == null) return;
            else _player = GameObject.FindGameObjectWithTag("Player").transform;
        }

        //MOVEMENT
        //if ( (_player.transform.position - this.transform.position).magnitude > targetMinDistance )
        float dist = (_player.transform.position - this.transform.position).magnitude;
        float spd = 0;
        if (dist > targetMinDistance)
            spd = Mathf.Lerp(0, speed, 0.5f * (dist - targetMinDistance));
        else
            spd = Mathf.Lerp(0, -speed, 0.5f * (-dist + targetMinDistance));
        this.transform.position += spd * this.transform.up * Time.deltaTime;

        //ROTATION
        Quaternion r = Quaternion.LookRotation(this.transform.forward, _player.position - this.transform.position);
        this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, r, angularspeed * Time.deltaTime);


    }

    void OnCollisionEnter2D (Collision2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            FindObjectOfType<GameManager>().DamagePlayer(clashDamage);
            this.gameObject.GetComponent<Character>().Die(false);
        }
    }
}
