using System.Collections;
using UnityEngine;

namespace Shooter.Gameplay
{
    /// <summary>
    /// Стрелок-«челнок»: просыпается при появлении игрока, выходит вперёд, затем
    /// курсирует между двумя точками, останавливаясь, чтобы выстрелить очередью из двух
    /// пуль с анимацией.
    /// </summary>
    public class Enemy_ShooterB : Enemy
    {
        [Header("Стрельба и движение")]
        public GameObject m_BulletPrefab1;
        public GameObject m_FireParticlePrefab1;
        [Tooltip("Дальность курсирования влево/вправо")]
        public float m_WalkDistance = 3;

        void Start()
        {
            InitPosition = transform.position;
        }

        void Update()
        {
            CheckAlert();
            HandleFacePlayer();
            HandleDeath();
        }

        public override void StartAlert()
        {
            base.StartAlert();
            StartCoroutine(Co_EnterLevel());
        }

        IEnumerator Co_EnterLevel()
        {
            EnemyMovement movement = GetComponent<EnemyMovement>();
            movement.m_UsePhysics = false;
            movement.SetMoveTargetPosition(transform.position + m_StartWalkDistance * transform.forward);
            m_FacePlayer = false;
            while (!movement.m_ReachedTargetPosition)
                yield return null;

            yield return new WaitForSeconds(1f);
            StartCoroutine(Co_AttackLoop());
        }

        IEnumerator Co_AttackLoop()
        {
            EnemyMovement movement = GetComponent<EnemyMovement>();

            Vector3[] points =
            {
                transform.position + new Vector3(-m_WalkDistance, 0, 0),
                transform.position + new Vector3(m_WalkDistance, 0, 0),
            };

            int pointNum = 0;
            while (true)
            {
                // Едем к точке, повернувшись по направлению движения.
                movement.m_FaceMoveDirection = true;
                m_FacePlayer = false;
                movement.SetMoveTargetPosition(points[pointNum]);
                while (!movement.m_ReachedTargetPosition)
                    yield return null;

                pointNum = (pointNum == 0) ? 1 : 0;

                // Останавливаемся, поворачиваемся к игроку и стреляем.
                movement.m_FaceMoveDirection = false;
                m_FacePlayer = true;
                yield return new WaitForSeconds(1f);

                m_Animator.SetTrigger("Shoot");
                ShootBullet();

                yield return new WaitForSeconds(1f);
            }
        }

        public void ShootBullet()
        {
            for (int i = 0; i < 2; i++)
            {
                GameObject obj = Instantiate(m_BulletPrefab1);
                obj.transform.position = m_FirePoint.position;
                Vector3 dir = PlayerChar.m_Current.transform.position - transform.position;
                dir.y = 0;
                obj.transform.forward = Quaternion.Euler(0, -5f + i * 10, 0) * dir;
                obj.GetComponent<ProjectileCollision>().m_Creator = gameObject;
                Destroy(obj, 10);

                obj = Instantiate(m_FireParticlePrefab1);
                obj.transform.position = m_FirePoint.position;
                Destroy(obj, 3);
            }
        }

        void OnDrawGizmos()
        {
            Gizmos.DrawLine(transform.position, transform.position + m_StartWalkDistance * transform.forward);
            Gizmos.DrawLine(transform.position + m_WalkDistance * transform.right, transform.position - m_WalkDistance * transform.right);
        }
    }
}
