using UnityEngine;
using Zenject;
using FlappyBird.Core;

namespace FlappyBird.Gameplay.Bird
{
    /// <summary>
    /// Thin MonoBehaviour view for the bird.
    /// Responsibilities:
    ///   - Hold component references (Rigidbody2D)
    ///   - Forward Unity lifecycle events to the service
    ///   - Detect input in Update and delegate to IBirdController
    /// NO game logic lives here.
    /// </summary>
    [RequireComponent(typeof(Rigidbody2D))]
    public sealed class BirdView : MonoBehaviour
    {
        private IBirdController   _birdController;
        private IGameStateManager _gameStateManager;

        [Inject]
        private void Construct(IBirdController birdController, IGameStateManager gameStateManager)
        {
            _birdController   = birdController;
            _gameStateManager = gameStateManager;
        }
        // Unity lifecycle
        /// <summary>Update: ONLY input detection, no logic.</summary>
        private void Update()
        {
            if (_gameStateManager.CurrentState != GameState.Playing) return;
            if (IsJumpInputDetected())
                _birdController.Jump();
        }
        /// <summary>FixedUpdate: physics tick forwarded to the service.</summary>
        private void FixedUpdate()
        {
            _birdController.Tick();
        }
        // Private helpers
        /// <summary>
        /// Returns true when the player taps (touch) or clicks (mouse / Space).
        /// Supports mobile touch and desktop mouse / keyboard for editor testing.
        /// </summary>
        private static bool IsJumpInputDetected()
        {
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
                return true;
            if (Input.GetMouseButtonDown(0))
                return true;
            if (Input.GetKeyDown(KeyCode.Space))
                return true;
            return false;
        }
    }
}