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

public class Opponent : Entity
{
    public override Vector2 RangeMultiplier => GlobalConfig.Instance.Current.enemyRangeMultiplier;
    public override float DamageMultiplier => GlobalConfig.Instance.Current.enemyDamageMultiplier;
    public override float AttackSpeedMultiplier => GlobalConfig.Instance.Current.enemyAttackSpeedMultiplier;
    public override float HpMultiplier => GlobalConfig.Instance.Current.enemyLifeMultiplier;
    public override float SpeedMultiplier => GlobalConfig.Instance.Current.enemySpeedMultiplier;

    private Vector2 _movementVector = Vector2.zero;
    private Rigidbody2D _rigidbody;
    private WaveObserver _observer;
    private int _observerIndex;
    private float _toughness;

    public override void Start()
    {
        base.Start();

        _rigidbody = GetComponent<Rigidbody2D>();
        Animation = gameObject.AddComponent<AnimationEntity>();
        _observer = WaveObserver.Instance;
        _toughness = Mathf.Round((Hp / 10) / 2);
        _observerIndex = _observer.NotifyEnemy(transform.position.y, _toughness);
    }

    public override void Update() 
    {
        base.Update();
        if(IsEnemyDetected) 
        {
            AttackEnemy();
        }
        else 
        {
            _movementVector.x = -Time.deltaTime * Speed;

            transform.position += (Vector3)_movementVector;

            Move();
        }
    }

    void AttackEnemy() 
    {
        //Attack Cooldown
        if(AttackInterval < AttackSpeedWait) 
        {
                Animation.PlayAttackAnim();

                AttackSpeedWait = 0f;
            }

            AttackSpeedWait += Time.deltaTime;
    }

    public override void Death()
    {
        _observer.NotifyDies(_observerIndex, _toughness, gameObject);
        base.Death();
    }

}
