using System;
using System.Collections.Generic;
using System.Linq;
using Script.Gameplay.Interface;
using UnityEngine;
using Core;
using Script.Gameplay.Global;

namespace Script.Gameplay.Player
{
    public class PlayerController : MonoBehaviour, IDamageable
    {
        [SerializeField] private int MaxHealth = 100;

        private int currentHealth;
        public int CurrentHealth
        {
            get => currentHealth;
            private set
            {
                currentHealth = value;
                OnHealthChanged?.Invoke(currentHealth);
            }
        }

        public event Action<int> OnHealthChanged;
        public event Action OnDeath;

        private PlayerMoveController playerMoveController;
        private PlayerCameraController playerCameraController;

        private void Awake()
        {
            playerMoveController = GetComponent<PlayerMoveController>();
            playerCameraController = GetComponent<PlayerCameraController>();

            CurrentHealth = MaxHealth;

            ControllerLocator.Instance.Register(this);
        }

        private void Start()
        {
            OnDeath += GameFlowManager.Instance.RestartGame;
        }

        public void TakeDamage(int damage)
        {
            CurrentHealth -= damage;
            if (CurrentHealth <= 0)
            {
                CurrentHealth = 0;
                OnDeath?.Invoke();
            }
        }

        public void Heal(int heal)
        {
            CurrentHealth += heal;
            if (CurrentHealth > MaxHealth)
            {
                CurrentHealth = MaxHealth;
            }
        }
    }
}