using System;using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Serialization;

public class GreenEnemy : MonoBehaviour
{
    public bool goLeftInit  = false;

    public float walkSpeed;
    public float sprintSpeed;
    private bool _isSprinting= false; 

    public float attackPhaseDuration;

    private float _currentSpeed;

    private GameObject _attackPhaseIndicator;
    private SpriteRenderer _attackPhaseIndicatorSpriteRenderer;
    private bool _isAttacking = false;
    public float hitForce;

    private int _direction = 1;
    // public float sight;
    private BoxCollider2D _sightCollider; 
    
    
    private GameObject _attackRange;

    public GameObject player;

    private Rigidbody2D _playerRigidbody2D;
    
    // Start is called before the first frame update
    void Start()
    {
        _currentSpeed = walkSpeed;
        if (goLeftInit)
        {
            TurnAround();
        }

        // Attack phase color
        _attackPhaseIndicator = transform.Find("AttackPhaseIndicator").GameObject();
        _attackPhaseIndicatorSpriteRenderer = _attackPhaseIndicator.GetComponent<SpriteRenderer>();
        
        // Attack range
        _attackRange = transform.Find("AttackRange").GameObject();
        _attackRange.GetComponent<EnemyAttack>().HitByEnemy += OnHitByEnemy;
        _sightCollider = transform.GetComponent<BoxCollider2D>();
            
    }

    // Update is called once per frame
    void Update()
    {
         transform.position += Time.deltaTime*_currentSpeed*_direction*Vector3.right ;
    }

    void TurnAround()
    {
        _direction *= -1;
        transform.Rotate(Vector3.up, 180); 
    }

    IEnumerator AttackSequence()
    {
        _currentSpeed = 0f;
        _attackPhaseIndicator.SetActive(true);
        _attackPhaseIndicatorSpriteRenderer.color = Color.green;
        yield return new WaitForSeconds(attackPhaseDuration);
        _attackPhaseIndicatorSpriteRenderer.color = Color.yellow;
        yield return new WaitForSeconds(attackPhaseDuration);
        _attackPhaseIndicatorSpriteRenderer.color = Color.red;
        yield return new WaitForSeconds(attackPhaseDuration);
        _attackPhaseIndicator.SetActive(false);
        _attackRange.SetActive(true);
        _currentSpeed = _isSprinting ? sprintSpeed : walkSpeed;
        

    }

    IEnumerator OnCollisionStay2D(Collision2D coll) {
        // Changing direction
        if (coll.gameObject.CompareTag("Wall") )
        {
            
            TurnAround();
        }
        // Attacking
        if (coll.gameObject.CompareTag("Player") && !_isAttacking)
        {
            _isAttacking = true; 
            var speed = _currentSpeed;
            yield return AttackSequence();
            _isAttacking = false; 
        }
        
        yield return null;
    }

    private void OnTriggerEnter2D(Collider2D coll)
    {
        if (coll.gameObject.CompareTag("Player"))
        {
            _isSprinting = true;
            if (!_isAttacking)
            {
               
                _currentSpeed = sprintSpeed;
            }
        }
    }

    
    private void OnTriggerExit2D(Collider2D coll)
    {
        if (coll.gameObject.CompareTag("Player"))
        {
            _isSprinting = false; 
            if(!_isAttacking)
            {
              
                _currentSpeed = walkSpeed;
            } 
        }
    }

    public void OnHitByEnemy(Collider2D coll)
    {
        Debug.Log(coll.gameObject.tag);
        // coll.gameObject.GetComponent<Rigidbody2D>()
            // .AddForce(hitForce * _direction * Vector3.right, ForceMode2D.Impulse);
            
    }
}
