using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Spine.Unity.Examples.SpineboyFootplanter;

public class Tower : Character
{
    private void Awake()
    {
        Init();
    }

    public bool GetAttack()
    {
        if (targetCharacter != null)
        {
            return true;
        }
        return false;
    }

    public void SearchTarget()
    {
        targetCharacter = searchTrigger.GetMinDisTarget(targetTypes, true);
    }

    public override void OnState()
    {
        base.OnState();
        SearchTarget();
        attackTime -= Time.deltaTime;
        dieKeepTime -= Time.deltaTime;
        bool isAttack = GetAttack();
        switch (state)
        {
            case CharacterState.Idle:
                if (isAttack)
                {
                    Attack1();
                    break;
                }
                break;
            case CharacterState.Attack:
                if (attackTime <= 0)
                {
                    ChangeState(CharacterState.Idle);
                    break;
                }
                break;
            case CharacterState.Die:
                if (dieKeepTime <= 0)
                {
                    gameObject.SetActive(false);
                    break;
                }
                break;
            default:
                break;
        }
    }

    public override void ChangeState(CharacterState newState)
    {
        switch (state)
        {
            case CharacterState.Idle:
                break;
            case CharacterState.Attack:
                aniCollider.Play("NotAttack", 1, 0);
                attackTarget = null;
                break;
            case CharacterState.Die:
                isDie = false;
                break;
            default:
                break;
        }
        CharacterState oldState = state;
        state = newState;
        switch (newState)
        {
            case CharacterState.Idle:
                ani.Play("idle", 0, 0);
                aniCollider.Play("Idle", 0, 0);
                rb.velocity = Vector3.zero;
                //animalAni.SetInteger("state", (int)PlayerState.Idle);
                break;
            case CharacterState.Attack:
                attackTarget = targetCharacter;
                if ((attackTarget.transform.position - transform.position).x > 0
                    && bodyTrans.localScale.x > 0)
                {
                    Turn();
                }
                else if ((attackTarget.transform.position - transform.position).x < 0
                    && bodyTrans.localScale.x < 0)
                {
                    Turn();
                }
                break;
            case CharacterState.Die:
                ani.Play("die", 0, 0);
                aniCollider.Play("Die", 0, 0);
                isDie = true;
                dieKeepTime = totalDieKeepTime;
                break;
            default:
                break;
        }
    }

    public override void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
    {
        hp -= damage;
        uiHp.Show(hp, totalHp);
        if (hp <= 0)
        {
            ChangeState(CharacterState.Die);
        }
    }

}
