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

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

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

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

    private Demonic dem;            //厨子的demonic脚本

    private bool once = false;      //死一次
    private bool die = false;       //厨子命尽
    private bool toCatch = false;   //没串了去抓人
    private bool isRunning = false; //开跑

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

    private GameObject[] customers; //厨子的所有顾客
    private int count;              //顾客数量

    public float larger;            //顾客变大的程度

    public bool canMove = false;    //厨师可移动

    private GameObject player1;
    private GameObject player2;
    private GameObject target;

    public GameObject lockEffect;
    private GameObject curLock;

    public enum cookState
    {
        sell = 0,
        walk = 1,
        find = 2,
        run = 3,
        cook = 4,
        back = 5,
    }

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

    private void Start()
    {
        cook = transform.parent;
        dem = cook.GetComponent<Demonic>();
        ani = dem.ani;
        col = GetComponent<Collider>();
        col.enabled = false;
        dia = text.transform.parent.gameObject;
        dia.SetActive(false);
        customers = new GameObject[chuan];
        state = 0;
        curLock = Instantiate(lockEffect, cook.transform.position, new Quaternion(0, 0, 0, 0), cook.transform);
    }

    private void OnTriggerEnter(Collider other)
    {
        //路过发串
        if (state == cookState.sell && !die && other.gameObject.layer == 7 || other.gameObject.layer == 8 || other.gameObject.layer == 6)
        {
            if (chuan > 0)
            {
                GameObject ga = other.transform.parent.parent.parent.gameObject;
                Character ca = ga.GetComponent<Character>();
                if (Array.IndexOf(customers, ga) == -1)
                {
                    chuan -= 1;
                    text.text = chuan.ToString();
                    ani.Play("attack_march", 0, 0);
                    ca.HpUp(value, larger);
                    customers[count] = ga;
                    count += 1;
                    Instantiate(effect, ga.transform.position, new Quaternion(0, 0, 0, 0), ga.transform);
                    if (chuan == 0)
                    {
                        toCatch = true;
                        dem.ChangeState(CharacterState.Die);
                    }
                }
            }
        }
        //抓人
        else if (state == cookState.run || state == cookState.find || state == cookState.walk && !once)
        {
            if (other.gameObject.layer == 8)
            {
                food = other.gameObject;
                ChangeState(cookState.cook);
            }
        }
    }

    private void ChangeState(cookState cs)
    {
        switch (cs)
        {
            //卖串中
            case cookState.sell:
                canMove = false;
                //不能被攻击
                foreach (GameObject g in colliders)
                {
                    g.SetActive(false);
                }
                break;
            //走路中
            case cookState.walk:
                canMove = true;
                dia.SetActive(false);
                //能被攻击
                foreach (GameObject g in colliders)
                {
                    g.SetActive(true);
                }
                break;
            //发现目标
            case cookState.find:
                break;
            //冲刺中
            case cookState.run:
                canMove = true;
                foreach (GameObject g in colliders)
                {
                    g.SetActive(true);
                }
                break;
            //鲨人中
            case cookState.cook:
                break;
            //返回中
            case cookState.back:
                break;
            default:
                break;
        }
        state = cs;
    }

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

    private void StateAct(cookState cs)
    {
        switch (cs)
        {
            case cookState.cook:

                break;
            case cookState.run:
                break;
            case cookState.sell:
                break;
            default:
                break;
        }
    }

    private void Update()
    {
        if (!isAct)
        {
            activeTime += Time.deltaTime;
            if (activeTime >= activeAniTime)
            {
                dia.SetActive(true);
                isAct = true;
                col.enabled = true;
            }
        }
        if (die && !once)
        {
            once = true;
            col.enabled = false;
            dem.ChangeState(CharacterState.Die);
        }
    }
}
