using System;
using UnityEngine;
using Random = UnityEngine.Random;

public class BattleArena : MonoBehaviour
{
    [SerializeField] Battler _leftBattler;
    [SerializeField] Battler _rightBattler;
    [SerializeField] float _attackDelay = 0.5f;
    [SerializeField] Transform _leftCharacterPosition, _rightCharacterPosition, _leftCharacterStart, _rightCharacterStart;

    int rightHealth, leftHealth;
    float _nextAttackTime;
    
    [SerializeField] BattleManager _battleManager;
    [SerializeField] PlayerManager _playerManager;
    [SerializeField] Transform _exit;

    public bool Battling { get; private set; }
    public string LeftPlayerName {get; private set;}
    public string RightPlayerName {get; private set;}
    public Battler RightBattler => _rightBattler;
    public Battler LeftBattler => _leftBattler;
    public Battler Winner { get; private set; }


    void Awake()
    {
        _battleManager = FindObjectOfType<BattleManager>();
        _playerManager = FindObjectOfType<PlayerManager>();
    }

    void OnDrawGizmos()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(_leftCharacterStart.position, 1f);
        Gizmos.DrawWireSphere(_rightCharacterStart.position, 1f);

        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(_leftCharacterPosition.position, 1f);
        Gizmos.DrawWireSphere(_rightCharacterPosition.position, 1f);

        Gizmos.color = Color.Lerp(Color.yellow, Color.red, 0.5f);

        Gizmos.DrawLine(_leftCharacterStart.position, _leftCharacterPosition.position);
        Gizmos.DrawLine(_rightCharacterStart.position, _rightCharacterPosition.position);
    }

    public void NextBattle(string leftPlayer, string rightPlayer)
    {
        Debug.Log($"Assigned {leftPlayer} and {rightPlayer} to {gameObject.name}");
        Winner = null;

        LeftPlayerName = leftPlayer;
        RightPlayerName = rightPlayer;

        _leftBattler = _playerManager.Get(leftPlayer);
        _rightBattler = _playerManager.Get(rightPlayer);
        
        _leftBattler.Initialize(_leftCharacterStart, _leftCharacterPosition);
        _rightBattler.Initialize(_rightCharacterStart, _rightCharacterPosition);
        
       // _leftBattler.GetComponentInChildren<CharacterHealthBar>(true).gameObject.SetActive(true);
       // _rightBattler.GetComponentInChildren<CharacterHealthBar>(true).gameObject.SetActive(true);
    }

    void Update()
    {
        if (!Battling)
            return;
        
        if (_leftBattler.Health <= 0 || _rightBattler.Health <= 0)
        {
            Winner = _leftBattler.Health > 0 ? _leftBattler : _rightBattler;
            Winner.SetWin();
          //  Winner.GetComponentInChildren<CharacterHealthBar>(true).gameObject.SetActive(false);
            _battleManager.AddWinner(Winner);

            Battling = false;
            _leftBattler.ClearFightData();
            _rightBattler.ClearFightData();
        }

        if (Time.time >= _nextAttackTime)
            Attack();
    }

    void Attack()
    {
        _nextAttackTime = Time.time + _attackDelay + Random.Range(-0.1f, 0.1f);

        var leftRoll = Random.Range(0, 100) + _leftBattler.GetHitModifier();
        var rightRoll = Random.Range(0, 100) + _rightBattler.GetHitModifier();

        if (leftRoll > rightRoll)
            _rightBattler.TakeHit(_leftBattler.Attack(leftRoll));
        else if (leftRoll < rightRoll)
            _leftBattler.TakeHit(_rightBattler.Attack(rightRoll));
    }

    public void BeginBattle()
    {
        if (_leftBattler == null || _rightBattler == null)
            return;
        
        Battling = true;
        _nextAttackTime = Time.time + 1f;
        _leftBattler.PrepareForNewBattle();
        _rightBattler.PrepareForNewBattle();
    }

    public void RemovePastBattlers()
    {
        if (_leftBattler != null)
            _leftBattler.SetDestination(_exit);// transform.localPosition = new Vector3(1000, 1000, 1000);
        if (_rightBattler != null)
            _rightBattler.SetDestination(_exit);// = new Vector3(1000, 1000, 1000);

        _leftBattler = null;
        _rightBattler = null;
        LeftPlayerName = string.Empty;
        RightPlayerName = string.Empty;
    }
}