using System.Collections;
using System.Collections.Generic;
using UniRx;
using UnityEngine;

namespace Nan.Scripts
{
    public class Enemy2Move : MonoBehaviour
    {
        [SerializeField] private GameObject _enemyBullet;

        private static readonly List<Vector2> Directions = new List<Vector2>();

        private float _coolDown;
        private int _directionIndex;


        public PlayerControl Player { get; set; }


        private void Awake()
        {
            Directions.Add(new Vector2(0.0f, 1.0f));
            Directions.Add(new Vector2(1.0f, 1.0f));
            Directions.Add(new Vector2(1.0f, 0.0f));
            Directions.Add(new Vector2(1.0f, -1.0f));
            Directions.Add(new Vector2(0.0f, -1.0f));
            Directions.Add(new Vector2(-1.0f, -1.0f));
            Directions.Add(new Vector2(-1.0f, 0.0f));
            Directions.Add(new Vector2(-1.0f, 1.0f));

            _directionIndex = 0;
        }

        private void Update()
        {
            if (_coolDown < 0)
            {
                var dir = Directions[_directionIndex];
                EnemyShootBullet(dir);
                _coolDown = 0.2f;
                _directionIndex = (_directionIndex + 1) % Directions.Count;
            }
            _coolDown -= Time.deltaTime;

        }

        private void EnemyShootBullet(Vector3 dir)
        {
            var obj = Instantiate(_enemyBullet);
            obj.GetComponent<EnemyBullet>().Velocity = dir * 5;
            obj.transform.position = transform.position;
        }
    }
}