using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum InvisibleState
{
    None = -1,
    Normal = 0, //Õý³£×´Ì¬
    Hurt = 1,   //±»´ò×´Ì¬
    FindSoul = 2,   //ÕÒ»ê×´Ì¬
    FindPlayer = 3, //ÕÒÍæ¼Ò
    Boom = 4,   //±¬Õ¨
    Die = 5,    //ËÀÍö
}
public class ESpirits_Invisible : MonoBehaviour
{
    public InvisibleState state;
    public Rigidbody rb;
    public float moveSpeed;
    public float findSoulSpeed;
    public float minSpeed;
    public float upSpeed;
    [HideInInspector]
    public float boomSpeed;
    public float altitude;
    public float randomRageY;
    public float randomRageX;
    [HideInInspector]
    public float offsetY;
    [HideInInspector]
    public float offsetX;
    [HideInInspector]
    public Vector3 targetPos;
    public int stopsNumber;
    [HideInInspector]
    public int nowNumber;
    [HideInInspector]
    public int nowPlayer;
    public float back;
    [HideInInspector]
    public int hp;
    public Enemy enemy;
    [HideInInspector]
    public float posx;
    [HideInInspector]
    public Soul targetSoul;
    public int followNumber;
    [HideInInspector]
    public int haveSoulNumber;
    public SoulFollowEffect followEffect;
    private void Awake()
    {
        hp = enemy.hp;
    }
    private void Update()
    {
        OnState();
        //print(SoulInMap.souls.Count);
        if (enemy.hp <= 0)
        {
            ChangeState(InvisibleState.Die);
        }
    }
    void OnState()
    {
        
        switch (state)
        {
            case InvisibleState.Normal:
                
                if (haveSoulNumber != followNumber && SoulInMap.souls.Count != 0)
                {
                    ChangeState(InvisibleState.FindSoul);
                    return;
                }
                if (hp != enemy.hp)
                {
                    ChangeState(InvisibleState.Hurt);
                    return;
                }
                posx = PlayersInput.instance[nowPlayer].transform.position.x;
                targetPos = new Vector3(posx + offsetX, altitude + offsetY, 0);
                Goto(targetPos, moveSpeed);
                if (Vector3.Distance(targetPos, transform.position) < 1)
                {
                    offsetY = Random.Range(-randomRageY, randomRageY);
                    offsetX = Random.Range(-randomRageX, randomRageX);
                    nowNumber++;
                    if (nowNumber > stopsNumber)
                    {
                        nowNumber = 0;
                        nowPlayer = 1 - nowPlayer;
                    }
                }
                
                break;
            case InvisibleState.Hurt:
                if (haveSoulNumber != followNumber && SoulInMap.souls.Count != 0)
                {
                    ChangeState(InvisibleState.FindSoul);
                    return;
                }
                if (Vector3.Distance(targetPos, transform.position) < 1)
                {
                    ChangeState(InvisibleState.Normal);
                    return;
                }
                
                Goto(targetPos, moveSpeed);
                break;
            case InvisibleState.FindSoul:
                if (targetSoul.gameObject.activeSelf)
                {
                    if(Vector3.Distance(transform.position, targetSoul.transform.position) <= 1)
                    {
                        SoulInMap.souls.Remove(targetSoul);
                        targetSoul.gameObject.SetActive(false);
                        haveSoulNumber++;
                        followEffect.ShowSouls(haveSoulNumber);
                        if(haveSoulNumber == followNumber)
                        {
                            ChangeState(InvisibleState.FindPlayer);
                            break;
                        }
                        FindSoul();
                    }
                }
                else
                {
                    FindSoul();
                }
                if(targetSoul == null)
                {
                    ChangeState(InvisibleState.Normal);
                    break;
                }
                targetPos = targetSoul.transform.position;
                Goto(targetPos, findSoulSpeed);
                
                break;
            case InvisibleState.FindPlayer:
                if (Vector3.Distance(transform.position, 
                    enemy.targetCharacter.transform.position + Vector3.up * 0.5f)<= enemy.hateDistance)
                {
                    ChangeState(InvisibleState.Boom);
                    break;
                }
                boomSpeed += Time.deltaTime * upSpeed;
                Goto(enemy.targetCharacter.transform.position + Vector3.up*0.5f, boomSpeed);
                break;
            case InvisibleState.Boom:
                ChangeState(InvisibleState.Normal);
                break;
            default:
                break;
        }

    }
    public void ChangeState(InvisibleState newState)
    {
        switch (state)
        {
            case InvisibleState.Normal:
                break;
            case InvisibleState.Hurt:
                break;
            case InvisibleState.FindSoul:
                break;
            case InvisibleState.FindPlayer:
                break;
            case InvisibleState.Boom:
                break;
            default:
                break;
        }
        switch (newState)
        {
            case InvisibleState.Normal:
                hp = enemy.hp;
                offsetY = Random.Range(-randomRageY, randomRageY);
                offsetX = Random.Range(-randomRageX, randomRageX);
                break;
            case InvisibleState.Hurt:
                posx = transform.position.x;
                targetPos = new Vector3(posx + offsetX - back, altitude + offsetY, 0);
                
                break;
            case InvisibleState.FindSoul:
                FindSoul();
                break;
            case InvisibleState.FindPlayer:
                boomSpeed = minSpeed;
                enemy.ChosePlayer();
                break;
            case InvisibleState.Boom:
                rb.velocity = Vector3.zero;
                followEffect.ShowSouls(0);
                followEffect.Boom();
                haveSoulNumber = 0;
                break;
            case InvisibleState.Die:
                enemy.isDie = true;
                enemy.dieKeepTime = enemy.totalDieKeepTime;
                enemy.DropSouls();
                gameObject.SetActive(false);
                break;
            default:
                break;
        }
        state = newState;
    }
    public void Goto(Vector3 pos,float speed)
    {
        Vector3 target = (pos - transform.position).normalized;
        rb.velocity = target * speed;
    }
    public void FindSoul()
    {
        List<Soul> souls = SoulInMap.souls;
        if(souls.Count == 0)
        {
            targetSoul = null;
            return;
        }
        float minDistance = Vector3.Distance(souls[0].transform.position, transform.position);
        int targetSoulId = 0;
        for(int i = 1; i < souls.Count; i++)
        {
            float distance = Vector3.Distance(souls[i].transform.position, transform.position);
            if (distance < minDistance)
            {
                minDistance = distance;
                targetSoulId = i;
            }
        }
        targetSoul = souls[targetSoulId];
    }
}
