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

public class Enemy : MonoBehaviour {

    bool playerInRoom;

    public float health; 
    [SerializeField]
    protected GameObject player;
    [SerializeField]
    protected PlayerControllerScript playerScript;
    [SerializeField]
    float speed;

    public int roomNumber; 

    public enum states {idle, followPlayer, attack};
    public states currentState;
    states previousState;

    [HideInInspector]
    public enum typeOfEnemy {DDOS, FireGooGoo, Boss};
    [SerializeField]
    protected typeOfEnemy enemy;

    bool shoot;
    bool soundCheck;
    public bool playerCollided;

    [SerializeField]
     protected GameObject bulletPrefab;
    GameObject bullet;


    // Use this for initialization
    void Start ()
    {
        playerCollided = false;
        shoot = true;
	}
	
	// Update is called once per frame
	void FixedUpdate ()
    {
        DecisionMaker(currentState);


        if (roomNumber == playerScript.roomNum)
            playerInRoom = true;
        else
            playerInRoom = false;
	}

    void DecisionMaker(states state)
    {
        switch (state)
        {
            case states.idle:
                IdleState();
                break;
            case states.followPlayer:
                switch (enemy)
                {
                    case typeOfEnemy.DDOS:
                        FollowPlayerState();
                        break;
                    case typeOfEnemy.FireGooGoo:
                        ChangeBehaviour(states.attack);
                        break;
                }
                
                break;
            case states.attack:
                AttackState();
                break;
            default:
                IdleState();
                break;

        }
    }
    void IdleState()
    {
        if (playerInRoom)
            ChangeBehaviour(states.followPlayer);

    }

    void FollowPlayerState()
    {
        transform.position += Seek() * speed;

        if (CloseToSomething(player.transform, 0) || playerCollided)
            ChangeBehaviour(states.attack);

        if (roomNumber != playerScript.roomNum)
            ChangeBehaviour(states.idle);
    }

    void AttackState()
    {
        if (!CloseToSomething(player.transform, 0))
            ChangeBehaviour(states.followPlayer);

        switch (enemy)
        {
            case typeOfEnemy.FireGooGoo:
                Shoot();
                break;
            case typeOfEnemy.DDOS:
                if (playerCollided && !playerScript.invincible)
                    {
                    playerScript.TakeDamage(1);
                    Destroy(this.gameObject);
                }
                if (!soundCheck)
                {
                    StartCoroutine(DDOSSoundTimer());
                    soundCheck = true;
                }
                break;
            case typeOfEnemy.Boss:

                break;
        }
    }

    void ChangeBehaviour(states newState)
    {
        previousState = currentState;
        currentState = newState;
    }
    #region Steering

    Vector3 Seek()
    {
        Vector3 steeringVelocity = new Vector3();

        if (player.transform.position.x < transform.position.x)
            steeringVelocity += Vector3.left;
        else if (player.transform.position.x > transform.position.x)
            steeringVelocity += Vector3.right;

        if (player.transform.position.y < transform.position.y)
            steeringVelocity += Vector3.down;
        else if (player.transform.position.y > transform.position.y)
            steeringVelocity += Vector3.up;

        return steeringVelocity;
    }
    #endregion

    void Shoot()
    {
        if ((shoot))
        {
            GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.position, Quaternion.identity);
            bullet.transform.position = transform.position;
            bullet.GetComponent<Bullet>().steering = Seek();
            StartCoroutine(ShootTimer());
        }

        ChangeBehaviour(states.followPlayer);
    }

    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Player")
            playerCollided = true;
    }

    void OnCollisionExit2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Player")
            playerCollided = false;
    }

    bool CloseToSomething(Transform gameObj, float close)
    {
        float distance;
        Vector3 distanceVector = new Vector3();

        if (gameObj.position.x < transform.position.x)
            distanceVector.x = transform.position.x - gameObj.position.x;
        else
            distanceVector.x = gameObj.position.x -  transform.position.x;

        if (gameObj.position.y < transform.position.y)
            distanceVector.y = transform.position.y - gameObj.position.y;
        else
            distanceVector.y = gameObj.position.y - transform.position.y;

        distance = (int)distanceVector.x + (int)distanceVector.y;

        if (distance <= close)
            return true;
        else return false;
    }

    IEnumerator ShootTimer()
    {
        shoot = false;
        yield return new WaitForSeconds(3);
        Destroy(bullet);
        shoot = true;
    }

    IEnumerator DDOSSoundTimer()
    {
        FindObjectOfType<AudioManagerScript>().PlayClip("DDOSEnemySound");
        yield return new WaitForSeconds(3);
        if(GameObject.Find("ddos") != null)
        {
            StartCoroutine(DDOSSoundTimer());
        }
    }
}
