using System.Collections;
using UnityEngine;

public class kilicli : MonoBehaviour
{
    private enum durum {normal,great,attack,gattack,geçiş}
    private durum anlık = durum.normal;
    Animator animator;
    public float hp = 40;
    private bool hareket = true;
    private bool sol = false;
    private bool dur = false;
    public float speed = 2f;
    private bool canmove = true;
    private bool bekle2 = true;
    private bool saldır = false;
    private bool saldırıyor = false;
    private bool great = false;
    void Start()
    {
        
        animator = GetComponent<Animator>();
    }

  
    void Update()
    {

        if (hp > 35)
        {
            anlık = durum.normal;
        }
        else if (hp == 30)
        {
            anlık = durum.attack;
        }
        else if (hp <= 25)
        {
            anlık = durum.geçiş;
        }
        
        if (!sol && canmove)
        {
            if (hareket)
            {
                transform.position += Vector3.right * speed * Time.deltaTime;
                transform.localScale = new Vector3(-3.52f, 2.93f, 1f);
                if (bekle2 == true)
                {
                    StartCoroutine(bekle());
                }

            }

        }
        if  (sol&&canmove)
        {
            if (hareket)
            {


                transform.position += Vector3.left * speed * Time.deltaTime;
                transform.localScale = new Vector3(3.52f, 2.93f, 1f);
                if (bekle2 == true)
                {
                    StartCoroutine(bekle());
                }
            }
        }
        
        
        switch (anlık)
        {
            case durum.normal:
                
                if (bekle2)
                {
                    StartCoroutine(bekle());
                }
                break;

            case durum.great:
                
                if (bekle2)
                {
                    StartCoroutine(bekle());
                }
                break;
            case durum.attack:
                if (!saldırıyor) // Coroutine başlamadıysa
                {
                    StartCoroutine(attack());
                }
                
                    break;
            case durum.geçiş:
                if(!great)
                {
                    StartCoroutine(geçiş());

                }
                break;


        }
        
    }
    

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Kılıç"))
        {
            hp -= 5;
        }
        if (other.CompareTag("Wall"))
        {
            sol = !sol;
        }
        
    }
    IEnumerator bekle()
    {
        bekle2 = false;
        if(anlık == durum.normal)
        {
            animator.SetBool("yürü", true);
        }
        if(anlık == durum.great)
        {
            animator.SetBool("gyürü", true);
        }
        float kumar = Random.Range(3f, 5f);
        yield return new WaitForSeconds(kumar);
        canmove = false;
        hareket = false;
        if (anlık == durum.great)
        {
            animator.SetBool("gyürü", false);
            animator.SetBool("gidle", true);
        }
        if(anlık == durum.normal)
        {
            animator.SetBool("yürü", false);
            animator.SetBool("idle", true);
        }
            
        
        yield return new WaitForSecondsRealtime(2.5f);
        if (anlık == durum.great)
        {
            animator.SetBool("gidle", false);
            animator.SetBool("gyürü", true);
           
        }
        if (anlık == durum.normal)
        {
            animator.SetBool("idle", false);
            animator.SetBool("yürü", true);
           
        }
        canmove = true;
        hareket = true;
        bekle2 = true;
        sol = !sol;

    }
    IEnumerator attack()
    {
        saldırıyor = true;
        canmove = false;
        hareket = false;
        animator.SetBool("attack", true);
        yield return new WaitForSecondsRealtime(5f);
        animator.SetBool("attack", false);
        yield return new WaitForSecondsRealtime(2f);
        canmove = true;
        hareket = true;
        anlık = durum.normal;
        saldırıyor = false;
        


    }
    IEnumerator geçiş ()
    {
        great = true;
        hareket = false;
        canmove = false;    
        animator.SetBool("değiş", true);
        yield return new WaitForSecondsRealtime(1.25f);
        animator.SetBool("değiş", false);
        hareket = true;
        canmove = true;
        anlık = durum.great;
    }
}