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

public class BossFSM : MonoBehaviour {

    public enum BossStates {
        IDLE,
        MOVE_TO_SCREEN,
        MOVE_LEFT,
        MOVE_RIGHT,
        DEAD
    }

   
    [SerializeField]
    BossStates currentState;

    public GameObject waypoint;
    public GameObject waypointLeft;
    public GameObject waypointRight;
    public GameObject targetPlayer;

    public float nextFire;
    public float fireRate;

    public float moveSpeed;
    public float idleTime;
    public GameObject projectile;
    public float maxlife;
    public float currentlife;
   // public Transform projectileExit;

    void Start() {

        maxlife = 20;
        currentState = BossStates.MOVE_TO_SCREEN;
        currentlife = maxlife;
        //projectileExit.position = this.transform.position;
    }

    void Update() {

        waypoint = GameObject.FindGameObjectWithTag("WP");
        waypointLeft = GameObject.FindGameObjectWithTag("WP_Right");
        waypointRight = GameObject.FindGameObjectWithTag("WP_Right");
        targetPlayer = GameObject.FindGameObjectWithTag("Player");

        switch (currentState) {
            case BossStates.MOVE_TO_SCREEN: UpdateMoveToScreenState(); break;
            case BossStates.IDLE: UpdateIdleState(); break;
            case BossStates.MOVE_LEFT: UpdateMoveLeftState(); break;
            case BossStates.MOVE_RIGHT: UpdateMoveRightState(); break;
            case BossStates.DEAD: UpdateDeadState(); break;
        }
    }

    public virtual void UpdateIdleState() {
        //play idle animation
        idleTime -= Time.deltaTime;

        if (idleTime <= 0) {
            currentState = BossStates.MOVE_LEFT;
        }
    }

    public virtual void UpdateMoveToScreenState() {
        //move monster to screen
        this.transform.position = Vector2.MoveTowards(this.transform.position, waypoint.transform.position, moveSpeed * Time.deltaTime);
        currentState = BossStates.IDLE;
    }

    public virtual void UpdateMoveLeftState() {
        Attack1();

        this.transform.position = Vector2.MoveTowards(this.transform.position, waypointLeft.transform.position, moveSpeed * Time.deltaTime);

        if (Vector2.Distance(this.transform.position, waypointLeft.transform.position) < 0.5f)
        {
            //waypointLeft.SetActive(false);
            currentState = BossStates.MOVE_RIGHT;
        }
    }

    public virtual void UpdateMoveRightState() {
        Attack1();

        this.transform.position = Vector2.MoveTowards(this.transform.position, waypointRight.transform.position, moveSpeed * Time.deltaTime);

        if (Vector2.Distance(this.transform.position, waypointRight.transform.position) < 0.5f)
        {
           // waypointRight.SetActive(false);
            currentState = BossStates.MOVE_LEFT;
        }
    }

    public virtual void UpdateDeadState() {
        //kill boss
        Destroy(this.gameObject);
    }

    void Attack1() {
        for (int i = 0; i < 3; i++)
        {
            ShootProjectile();
        }
    }

    public void ShootProjectile()
    {
        
            if (Time.time > nextFire)
            {
                nextFire = Time.time + fireRate;
                GameObject prj = Instantiate(projectile, this.transform.position, Quaternion.identity) as GameObject;
                prj.transform.SetParent(this.transform);
            }
        }
    
}
