using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Unicorn.Hitbox;
using UnityEngine;
using NotImplementedException = System.NotImplementedException;

namespace Unicorn.Boss
{
    internal enum UnicornPhase
    {
        Start, P1, P2, P3, End 
    }
    public class Tuna : LivingEntity
    {
        
        private UnicornPhase _unicornPhase = UnicornPhase.Start;
        private Queue<GameObject> _p1Ps = new Queue<GameObject>();
        private SpriteRenderer _renderer;
        private const float P1PC = 50;

        public Transform projContainer;
        public GameObject phase1Proj;
        
        
        private new void Start()
        {
            _renderer = GetComponent<SpriteRenderer>();
            StartCoroutine(Started());
            base.Start();
        }

        private IEnumerator Started()
        {
            Invulnerable = true;
            const float target = P1PC*5;
            while (_p1Ps.Count < target)
            {
                var instantiate = Instantiate(phase1Proj, projContainer);
                _p1Ps.Enqueue(instantiate);
                instantiate.SetActive(false);
                yield return new WaitForFixedUpdate();
            }
            StartCoroutine(Phase1());
        }

        // 수정된 Phase1 로직
        private IEnumerator Phase1()
        {
            Invulnerable = false;
            _unicornPhase = UnicornPhase.P1;
            const float angleStep = 360f / P1PC; // 각도 간격 (30도)

            for (var i = 0; i < P1PC; i++)
            {
                var dequeue = _p1Ps.Dequeue();
                dequeue.SetActive(true);
                dequeue.transform.position = transform.position;
                var targetAngle = transform.rotation.eulerAngles.z + (angleStep * i);
                dequeue.transform.rotation = Quaternion.Euler(0, 0, targetAngle);
                _p1Ps.Enqueue(dequeue);
            }
            yield return new WaitForSeconds(1);
            if (health < maxHealth / 2)
            {
                StartCoroutine(Phase2());
            }
            else
                StartCoroutine(Phase1());
        }

        private IEnumerator Phase2()
        {
            
        }


        protected override void OnKnockback(DamageInfo info)
        {   
            
        }

        protected override void OnDeath()
        {
            transform.DOScale(0f, 3f).SetEase(Ease.Linear).OnComplete(() =>
            {
                gameObject.SetActive(false);
                base.OnDeath();
            });
        }

        protected override void OnDamaged(DamageInfo info)
        {
            StopCoroutine(HitColor());
            StartCoroutine(HitColor());
            base.OnDamaged(info);
        }

        private IEnumerator HitColor()
        {
            _renderer.color = new Color(1f, 0.8f, 0.8f);
            yield return new WaitForSeconds(0.5f);
            _renderer.color = Color.white;
        }
    }
}