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

public class ESpirits_Float : MonoBehaviour
{
    [Header("出场飞升")]
    private bool isRise = true;
    public float maxHeight, minHeight;
    private float height;               //升到哪个高度后停止
    private float curHeight;
    private float speed = 1;
    private Vector3 origPos;

    [Header("漂浮锁定玩家")]
    public float intervalTime = 10f;  //漂浮间隔时间
    public float firstAttackTime = 2f;  //第一次攻击距出场的时间
    private Vector3 attackPos;        //漂浮中心
    public float followTime = 3;        //跟随玩家时间
    public float lockTime = 1.5f;     //锁定等待时间
    //public float attackTime = 10f;      //漂浮持续时间
    public GameObject fe;             //漂浮脚本
    public GameObject lockEffect;     //锁定特效
    private GameObject curFe;
    private GameObject curLock;
    private float pastTime;           //已经经过的时间

    private GameObject player1;
    private GameObject player2;

    private GameObject target;
    private bool isDoing = false;
    private bool isFollow = false;
    private bool isLock = false;
    private bool isAttack = false;

    private Enemy ene;

    private void Awake()
    {
        ene = GetComponent<Enemy>();
        curLock = Instantiate(lockEffect, new Vector3(0, -50, 0), new Quaternion(0, 0, 0, 0), null);
        curFe = Instantiate(fe, curLock.transform.position, new Quaternion(0, 0, 0, 0), null);
        curFe.GetComponent<FloatEffect>().isEnemy = true;
        curFe.SetActive(false);
        curLock.SetActive(false);
        player1 = PlayersInput.instance[0].gameObject;
        player2 = PlayersInput.instance[1].gameObject;
    }

    private void Start()
    {
        origPos = transform.position;
        curHeight = origPos.y;
        pastTime = intervalTime - firstAttackTime;
        height = Random.Range(minHeight, maxHeight);
    }

    private void OnDisable()
    {
        if (curFe != null)
        {
            curFe.SetActive(false);
        }
        if (curLock != null)
        {
            curLock.SetActive(false);
        }
    }

    private void MoveToHeight()
    {
        curHeight = Mathf.SmoothDamp(curHeight, height, ref speed, 0.02f);
        transform.position = new Vector3(origPos.x, curHeight, origPos.z);
    }

    private void ChoosePlayer()
    {
        player1 = PlayersInput.instance[0].gameObject;
        player2 = PlayersInput.instance[1].gameObject;
        float dis1 = Vector2.Distance(player1.transform.position, transform.position);
        float dis2 = Vector2.Distance(player2.transform.position, transform.position);
        if (dis1 < dis2)
        {
            target = player1;
        }
        else
        {
            target = player2;
        }
        //target = player1;
    }

    private void FollowPlayer()
    {
        if (target != null)
        {
            curLock.transform.position = target.transform.position;
        }
    }

    private void LockPlayer()
    {

    }

    public void FloatAttack()
    {
        curFe.transform.position = curLock.transform.position;
        curLock.SetActive(false);
        curFe.SetActive(true);
    }

    private void Update()
    {
        //出场飞升
        if (isRise)
        {
            MoveToHeight();
            if (curHeight - height <= 0.02f && curHeight - height >= -0.02f)
            {
                isRise = false;
                curHeight = height;
                transform.position = new Vector3(origPos.x, height, origPos.z);
            }
        }
        //隔一段时间发动一次锁定攻击
        if (!isRise && !isDoing && (ene.state == CharacterState.Idle || ene.state == CharacterState.Run || ene.state == CharacterState.Attack))
        {
            pastTime += Time.deltaTime;
            if (pastTime >= intervalTime)
            {
                ChoosePlayer();
                isDoing = true;
                isFollow = true;
                pastTime = 0;
                curLock.SetActive(true);
            }
        }
        //跟随玩家
        if (isFollow)
        {
            pastTime += Time.deltaTime;
            FollowPlayer();
            if (pastTime >= followTime)
            {
                isFollow = false;
                isLock = true;
                pastTime = 0;
            }
        }
        //锁定范围
        if (isLock)
        {
            pastTime += Time.deltaTime;
            LockPlayer();
            if (pastTime >= lockTime)
            {
                isLock = false;
                isAttack = true;
                pastTime = 0;
            }
        }
        //攻击
        if (isAttack)
        {
            FloatAttack();
            isAttack = false;
            isDoing = false;
        }
    }
}
