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

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

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

    public float AttackPhaseDuration;

    private float _currentSpeed;

    private GameObject _attackPhaseIndicator;
    private SpriteRenderer _attackPhaseIndicatorSpriteRenderer;
    private bool _isAttacking = false;

    private bool _directionLeft;
    // public float sight;
    private BoxCollider2D _sightCollider; 

    // Start is called before the first frame update
    void Start()
    {
        _directionLeft = goLeftInit;
        _currentSpeed = goLeftInit ? -speedInit: speedInit;
        
        // Attack phase color
        _attackPhaseIndicator = transform.Find("AttackPhaseIndicator").GameObject();
        _attackPhaseIndicatorSpriteRenderer = _attackPhaseIndicator.GetComponent<SpriteRenderer>();

        _sightCollider = transform.GetComponent<BoxCollider2D>();
        _sightCollider.offset = goLeftInit ? -_sightCollider.offset : _sightCollider.offset;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = new Vector3(_currentSpeed * Time.deltaTime + transform.position.x, transform.position.y,
            transform.position.z);
    }

    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);

        if (_isSprinting)
        {
            _currentSpeed = _directionLeft ? -sprintSpeed : sprintSpeed;
        }
        else
        {
            _currentSpeed = _directionLeft ? -speedInit : speedInit;
        }
        
        
    }

    private void OnCollisionEnter2D(Collision2D coll)
    {
        // Changing direction
        if (coll.gameObject.CompareTag("Wall") )
        {
            _directionLeft = !_directionLeft;
            _currentSpeed = -_currentSpeed;
            _sightCollider.offset = -_sightCollider.offset; 
        }
    }

    IEnumerator OnCollisionStay2D(Collision2D coll) {
        // 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 = _directionLeft ? -sprintSpeed : sprintSpeed;
            }
        }
    }

    
    private void OnTriggerExit2D(Collider2D coll)
    {
        if (coll.gameObject.CompareTag("Player"))
        {
            _isSprinting = false; 
            if(!_isAttacking)
            {
                _currentSpeed = _directionLeft ? -speedInit : speedInit;
            } 
        }
    }
}
