using FlappyBird.Core;
using UnityEngine;
using Zenject;

namespace FlappyBird.Pipes
{
    public class PipeView : MonoBehaviour, global::FlappyBird.Core.IPoolable
    {
        private const string UpperPipeName = "Pipe_Upper";
        private const string LowerPipeName = "Pipe_Lower";
        private const string ScoreTriggerName = "ScoreTrigger";

        private Transform _cachedTransform;
        private Transform _upperPipe;
        private Transform _lowerPipe;
        private Transform _scoreTrigger;
        private SpriteRenderer _upperRenderer;
        private SpriteRenderer _lowerRenderer;
        private BoxCollider2D _scoreTriggerCollider;
        private PipeScoreTrigger _pipeScoreTrigger;
        private FlappyBird.Core.IScoreManager _scoreManager;

        public float XPosition => _cachedTransform.position.x;

        [Inject]
        public void Construct(FlappyBird.Core.IScoreManager scoreManager)
        {
            _scoreManager = scoreManager;
        }

        private void Awake()
        {
            _cachedTransform = transform;
            _upperPipe = _cachedTransform.Find(UpperPipeName);
            _lowerPipe = _cachedTransform.Find(LowerPipeName);
            _scoreTrigger = _cachedTransform.Find(ScoreTriggerName);

            if (_upperPipe != null)
            {
                _upperRenderer = _upperPipe.GetComponent<SpriteRenderer>();
            }

            if (_lowerPipe != null)
            {
                _lowerRenderer = _lowerPipe.GetComponent<SpriteRenderer>();
            }

            if (_scoreTrigger != null)
            {
                _scoreTriggerCollider = _scoreTrigger.GetComponent<BoxCollider2D>();
                _pipeScoreTrigger = _scoreTrigger.GetComponent<PipeScoreTrigger>();

                if (_pipeScoreTrigger == null)
                {
                    _pipeScoreTrigger = _scoreTrigger.gameObject.AddComponent<PipeScoreTrigger>();
                }

                _pipeScoreTrigger.Initialize(_scoreManager);
            }
        }

        public void SetWorldPosition(Vector3 position)
        {
            _cachedTransform.position = position;
        }

        public void MoveLeft(float distance)
        {
            _cachedTransform.position += Vector3.left * distance;
        }

        public void ConfigureGap(float gapCenterY, float gapSize, float scoreTriggerWidth)
        {
            if (_upperPipe == null || _lowerPipe == null || _upperRenderer == null || _lowerRenderer == null)
            {
                return;
            }

            float upperHalfHeight = _upperRenderer.size.y * 0.5f;
            float lowerHalfHeight = _lowerRenderer.size.y * 0.5f;
            float gapHalf = gapSize * 0.5f;

            _upperPipe.localPosition = new Vector3(0f, gapCenterY + gapHalf + upperHalfHeight, 0f);
            _lowerPipe.localPosition = new Vector3(0f, gapCenterY - gapHalf - lowerHalfHeight, 0f);

            if (_scoreTrigger != null)
            {
                _scoreTrigger.localPosition = new Vector3(0f, gapCenterY, 0f);
            }

            if (_scoreTriggerCollider != null)
            {
                _scoreTriggerCollider.size = new Vector2(scoreTriggerWidth, gapSize);
                _scoreTriggerCollider.offset = Vector2.zero;
            }
        }

        public void OnSpawned()
        {
            if (_pipeScoreTrigger != null)
            {
                _pipeScoreTrigger.ResetTrigger();
            }
        }

        public void OnDespawned()
        {
        }
    }
}
