using UnityEngine;
using System.Collections;

public class Bear : MonoBehaviour {


    Vector3 home;

    Vector3 velocity = Vector3.zero;

    Transform player;

    Vector3 target = Vector3.zero;

    public float movespeed = 3.0f;

    private Animator anim;
    private CharacterController cc;


    private float scared = 0.0f;

    private float dashing = 0.0f;

    private EnemyHealth eh;

    // Use this for initialization
    void Start() {
        eh = GetComponent<EnemyHealth>();
        player = FindObjectOfType<PlayerStats>().transform;
        home = transform.position;
        cc = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update() {



        float dashMulti = 1.0f;

        if (scared > 0) {
            scared -= Time.deltaTime;
            dashing = 0.3f;
            cc.Move((Vector3.up * -8) * Time.deltaTime * movespeed);
            return;
        }

        if (eh.invul > 0) {
            cc.Move((Vector3.up * -8) * Time.deltaTime * movespeed);
            return;
        }


        if (dashing > 0) {
            dashing -= Time.deltaTime;
            dashMulti = 1.4f;
        }


        Vector3 toPlayer = player.position - transform.position;



        if (toPlayer.magnitude < 13) {
            target = toPlayer;
            if (toPlayer.magnitude < 5) {
                dash();
            }

            if (dashing > 0 && toPlayer.magnitude < 3.5 && checkAngle(toPlayer)) {
                PlayerMovement pm = player.GetComponent<PlayerMovement>();
                pm.hurt(25);
            }

        } else {
            moveHome();
        }



        if (target != Vector3.zero) {
            target = new Vector3(target.x, 0, target.z);
            anim.SetFloat("speed", 1);
            float step = 5.0f * Time.deltaTime;
            Vector3 newDir = Vector3.RotateTowards(transform.forward, target, step, 0.0F);
            transform.rotation = Quaternion.LookRotation(newDir);

            cc.Move((transform.forward + Vector3.up * -8) * Time.deltaTime * movespeed * dashMulti);
        } else {
            cc.Move((Vector3.up * -8) * Time.deltaTime * movespeed);
            anim.SetFloat("speed", 0);
        }

    }

    public bool checkAngle(Vector3 toPlayer) {

        Vector3 pf = player.forward;
        pf = new Vector3(pf.x, 0, pf.z);

        Vector3 bf = new Vector3(toPlayer.x, 0, toPlayer.z);

        if (Vector3.Angle(pf, bf) <= 30.0f) {
            return true;
        }

        return false;
    }

    public void dash() {
        if (dashing <= 0) {
            
            dashing = 1.5f;
            anim.SetTrigger("dash");
        }
    }


    public void scare() {
        scared = 1.8f;
        anim.SetTrigger("scare");
    }

    private void moveHome() {
        Vector3 toHome = home - transform.position;
        if (toHome.magnitude > 3) {
            target = toHome;
        } else {
            target = Vector3.zero;
        }
    }
}
