using System.Collections;
using System.Collections.Generic;
using ScriptableObjectArchitecture;
using UnityEngine;
using UnityEngine.InputSystem;

public class Slider : MonoBehaviour
{
    [field: SerializeField] public int CurrentStep { get; private set; } = 1;

    [SerializeField] private InputActionAsset _playerControls = default;
    [SerializeField] private GameObject _sliderButton = default;

    [Header("Settings")]
    // [SerializeField] private float _startingPosition = 0;
    [SerializeField] private Transform _lowEndPosition = default;
    [SerializeField] private Transform _highEndPosition = default;
    [SerializeField] private int _steps = 1;

    [Header("Assets")]
    [SerializeField] private IntVariable _sliderStep = default;
    [SerializeField] private GameEvent _sliderStateUpdateEvent = default;
    [SerializeField] private GameEvent _selectableUpdateEvent = default;

    [Header("Refs")]
    [SerializeField] private Selectable _selectable = default;
    [SerializeField] private AudioSource _playOnSlide = default;

    private InputAction slidSliderAction;

    private void OnEnable()
    {
        _selectableUpdateEvent.AddListener(HandleSelectableChange);
    }

    private void OnDisable()
    {
        _selectableUpdateEvent.RemoveListener(HandleSelectableChange);
    }

    void Start()
    {
        // var currentAngles = _buttonArt.transform.localEulerAngles;
        // var newAngle = new Vector3(currentAngles.x, currentAngles.y, Mathf.Clamp(_startingPosition, _minPosition, _maxPosition));
        // _buttonArt.transform.localEulerAngles = newAngle;
    }

    private void HandleSelectableChange()
    {
        if (_selectable.Selected)
        {
            SetupInputListeners();
        }
        else
        {
            RemoveInputListeners();
        }
    }

    private void SetupInputListeners()
    {
        var playerControlsActionMap = _playerControls.FindActionMap("Player");
        slidSliderAction = playerControlsActionMap.FindAction("SlideSlider");

        slidSliderAction.performed += SlideSlider;
    }

    private void RemoveInputListeners()
    {
        var playerControlsActionMap = _playerControls.FindActionMap("Player");
        slidSliderAction = playerControlsActionMap.FindAction("SlideSlider");

        slidSliderAction.performed -= SlideSlider;
    }

    private void SlideSlider(InputAction.CallbackContext context)
    {
        int slideDirection = context.ReadValue<float>() > 0 ? 1 : -1;

        if ((CurrentStep == 1 && slideDirection == -1) || (CurrentStep == _steps && slideDirection == 1))
        {
            // do nothing
        }
        else
        {
            UpdateSliderState(slideDirection);
            UpdateSliderPosition(slideDirection);
        }
    }

    private void UpdateSliderState(int direction)
    {
        CurrentStep += direction;
        _sliderStep.Value = CurrentStep;
        _sliderStateUpdateEvent.Raise();
    }

    private void UpdateSliderPosition(int direction)
    {
        float distancePerStep = Vector3.Distance(_highEndPosition.position, _lowEndPosition.position) / (_steps - 1);

        var target = direction > 0 ? _highEndPosition : _lowEndPosition;
        Vector3 targetPosition = target.position;
        Vector3 sliderPosition = _sliderButton.transform.position;

        _sliderButton.transform.position = Vector3.MoveTowards(sliderPosition, targetPosition, distancePerStep);
        _playOnSlide.Play();
    }
}
