using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;
using Random = UnityEngine.Random;

public class ESpirits_Cook : MonoBehaviour
{
    public TextMeshProUGUI text;        //剩余串的数量文本
    private GameObject dia;             //文本框

    private float offset;

    private Transform cook;             //厨子
    private Animator ani;               //厨子动画
    private Collider col;               //厨子的碰撞体
    public int chuan;                   //厨子拥有多少串
    public bool isGood;                 //厨子是否是好厨子
    public GameObject[] colliders;      //一些要开要关的collider

    private float intervalTimePast;
    public float intervalTimeSell;      //卖串的间隔时间
    private bool isInterval = false;    //开始计算间隔时间

    public float minX;                  //厨子卖串的最小地址站
    public float maxX;                  //厨子卖串的最大地址站

    public float value;                 //加血程度百分比
    public GameObject effect;           //加血效果

    private Enemy ene;                  //厨子的enemy脚本

    private float activeTime;           //厨子已出现的时长
    public float activeAniTime;         //厨子出现动画的时长
    private bool isAct = false;         //厨子已经完整出现

    public float larger;                //顾客变大的程度
    public List<Character> customers;  //所有顾客

    public Character target;
    public SearchTrigger searchtrigger;

    public GameObject lockEffect;
    private GameObject curLock;
    //public GameObject cookKillEffect;

    private float pastTime;
    public float cookTime;
    public float seizeTime;
    public float chargeTime;

    private float destX;

    public enum cookState
    {
        sell = 0,
        run = 1,
        seize = 2,  //抓住
        back = 3,
        cook = 4,
        first = 5,  //刚出生
    }

    private cookState state;
    private GameObject food;    //被抓的食材

    private void Start()
    {
        cook = transform.parent;
        ene = cook.GetComponent<Enemy>();
        ani = ene.ani;
        col = GetComponent<Collider>();
        col.enabled = false;
        dia = text.transform.parent.gameObject;
        dia.SetActive(false);
        //customers = new List<GameObject>();
        destX = DestinationX();
        text.text = chuan.ToString();
        ene.noOnSearchState = true;
        //curLock = Instantiate(lockEffect, cook.transform.position, new Quaternion(0, 0, 0, 0), cook.transform);
    }

    private float DestinationX()
    {
        float x = Random.Range(minX, maxX);
        return x;
    }

    private void ToRush()
    {
        ChangeState(cookState.run);
    }

    private void TargetInteract()
    {
        target = searchtrigger.GetMinDisTarget(ene.targetTypes, ene.canHitFly);
        if (target != null)
        {
            if (state == cookState.sell && (target.gameObject.layer == 7 || target.gameObject.layer == 8 || target.gameObject.layer == 6))
            {
                if (!isInterval && chuan > 0)
                {
                    if (!customers.Exists(T => T == target))
                    {
                        isInterval = true;
                        chuan -= 1;
                        text.text = chuan.ToString();
                        ani.Play("attack_march", 0, 0);
                        target.HpUp(value, larger);
                        customers.Add(target);
                        //count += 1;
                        //Instantiate(effect, ga.transform.position, new Quaternion(0, 0, 0, 0), ga.transform);
                        if (chuan == 0)
                        {
                            customers = new List<Character>();
                            ani.Play("charge", 0, 0);
                            Invoke("ToRush", chargeTime);
                        }
                    }
                }
            }
            //抓人
            else if (state == cookState.run)
            {
                if (target.gameObject.layer == 6 || target.gameObject.layer == 7)
                {
                    food = target.gameObject;
                    if (food.layer == 7 && !target.isDie)
                    {
                        food.GetComponent<Demonic>().ChangeState(CharacterState.Coma);
                        //food.GetComponent<Demonic>().ChangeState(CharacterState.Die);
                        //cookKillEffect.SetActive(true);
                        ChangeState(cookState.seize);
                    }
                    else if (food.layer == 6 && !target.isDie)
                    {
                        food.GetComponent<PlayerController>().ChangeState(CharacterState.Coma);
                        //food.GetComponent<PlayerController>().ChangeState(CharacterState.Die);
                        //cookKillEffect.SetActive(true);
                        ChangeState(cookState.seize);
                    }
                    chuan = food.GetComponent<Character>().cookNum;
                    offset = cook.transform.position.x - food.transform.position.x;
                    text.text = chuan.ToString();
                }
            }
            //回家卖串
            /*
            else if (state == cookState.back && target.gameObject.layer == 8)
            {
                ChangeState(cookState.cook);
                if (food.layer == 7 && !target.isDie)
                {
                    food.GetComponent<Demonic>().ChangeState(CharacterState.Die);
                }
                else if (food.layer == 6 && !target.isDie)
                {
                    food.GetComponent<PlayerController>().ChangeState(CharacterState.Die);
                }
            }*/
        }
    }

    private void ChangeState(cookState cs)
    {
        switch (cs)
        {
            //卖串中
            case cookState.sell:
                dia.SetActive(true);
                ene.isBack = false;
                ene.canMove = false;
                //不能被攻击
                foreach (GameObject g in colliders)
                {
                    g.SetActive(false);
                }
                break;
            //冲刺中
            case cookState.run:
                dia.SetActive(false);
                foreach (GameObject g in colliders)
                {
                    g.SetActive(true);
                }
                ene.canMove = true;
                ene.ChangeState(CharacterState.Rush);
                break;
            //抓住你啦
            case cookState.seize:
                ene.canMove = false;
                break;
            //鲨人中
            case cookState.cook:
                ene.canMove = false;
                ani.Play("attack_march", 0, 0);
                break;
            //返回中
            case cookState.back:
                ene.canMove = true;
                ene.isBack = true;
                break;
            //首次登场
            case cookState.first:
                dia.SetActive(false);
                ene.canMove = true;
                foreach (GameObject g in colliders)
                {
                    g.SetActive(true);
                }
                break;
            default:
                break;
        }
        state = cs;
    }

    private void AimAtPlayer()
    {
        lockEffect.transform.position = transform.position;
    }

    private void StateAct(cookState cs)
    {
        switch (cs)
        {
            case cookState.back:
                if (cook.transform.position.x <= destX)
                {
                    ChangeState(cookState.cook);
                    if (food.layer == 7 && !target.isDie)
                    {
                        food.GetComponent<Demonic>().ChangeState(CharacterState.Die);
                    }
                    else if (food.layer == 6 && !target.isDie)
                    {
                        food.GetComponent<PlayerController>().ChangeState(CharacterState.Die);
                    }
                }
                food.transform.position = new Vector3(cook.transform.position.x - offset, food.transform.position.y,
                    food.transform.position.z);
                break;
            case cookState.cook:
                pastTime += Time.deltaTime;
                if (pastTime >= cookTime)
                {
                    pastTime = 0;
                    ChangeState(cookState.sell);
                }
                break;
            case cookState.run:
                //ene.targetCharacter = target.GetComponent<Character>();
                break;
            case cookState.sell:
                break;
            case cookState.first:
                if (cook.transform.position.x >= destX)
                {
                    ChangeState(cookState.sell);
                }
                break;
            case cookState.seize:
                pastTime += Time.deltaTime;
                if (pastTime >= seizeTime)
                {
                    pastTime = 0;
                    ChangeState(cookState.back);
                }
                break;
            default:
                break;
        }
    }

    private void Update()
    {
        if (!isAct)
        {
            activeTime += Time.deltaTime;
            if (activeTime >= activeAniTime)
            {
                dia.SetActive(true);
                isAct = true;
                col.enabled = true;
                ChangeState(cookState.first);
            }
        }
        else
        {
            StateAct(state);
        }
        if (isInterval)
        {
            intervalTimePast += Time.deltaTime;
            if (intervalTimePast >= intervalTimeSell)
            {
                intervalTimePast = 0;
                isInterval = false;
            }
        }
    }

    private void FixedUpdate()
    {
        if (isAct)
        {
            TargetInteract();
        }
    }
}
