using System.Collections.Generic;
using Canvases.Components;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;
using Utilities;
using Utilities.Extensions;

namespace Canvases.Menu.Rebind
{
    public class RebindUI : MonoBehaviour
    {
        [SerializeField] private ImageUIComponent background;
        [SerializeField] private GameObject rebindMenuContent;
        [SerializeField] private TextUIComponent currentControlScheme;
        [SerializeField] private ButtonUIComponent resetAllButton;
        [SerializeField] private RectTransform scrollRectContent;
        [SerializeField] private TextUIComponent rebindOverlayText;
        [SerializeField] private RebindActionUI rebindPrefab;
        [SerializeField] private ButtonUIComponent applyButton;

        private PlayerInputAction playerInputActionRef;

        private RebindActionUI firstButton;

        private readonly List<RebindActionUI> rebindUIs = new List<RebindActionUI>();
        private bool isInitialized;

        private void Start()
        {
            // Bind to OnInputDeviceChanged
        }

        private void OnEnable()
        {
            applyButton.OnClick += OnApplyPressed;
        }

        private void OnDisable()
        {
            applyButton.OnClick -= OnApplyPressed;
        }

        private void OnDestroy()
        {
            // Unbind to OnInputDeviceChanged
        }

        private void OnApplyPressed()
        {
            // Save changes
            // Hide
        }
        

        private void OnResetAll()
        {
            playerInputActionRef.RemoveAllBindingOverrides();
            UpdateAllRebindUI();
            // UpdateBindings
        }

        private void AddBindingsButton(string deviceName)
        {
            currentControlScheme.Text = $"< {deviceName.ToUpper()} >";
            ReadOnlyArray<InputAction> inputActions = playerInputActionRef.Player.Get().actions;
            bool first = true;
            foreach (var inputAction in inputActions)
            {
                foreach (var mainBinding in BindingsIconsUtil.GetRelevantMainBindings(inputAction, deviceName))
                {
                    SpawnButton(inputAction, mainBinding, first);
                    first = false;
                }
            }
        }

        private void SpawnButton(InputAction inputAction, int mainBindingIndex, bool first)
        {
            RebindActionUI actionButton = Instantiate(rebindPrefab, scrollRectContent);
            rebindUIs.Add(actionButton);
            actionButton.name = $"Rebind UI for {inputAction.name}";
            actionButton.Initialize(mainBindingIndex, inputAction, rebindOverlayText, OnUpdateBindingUIEvent);
            if (first)
            {
                firstButton = actionButton;
            }
        }

        private void OnUpdateBindingUIEvent(string deviceLayoutName, string mainControlPath)
        {
            UpdateAllRebindUI();
            // UpdateBindings
        }

        private void UpdateAllRebindUI()
        {
            foreach (RebindActionUI rebindActionUI in rebindUIs)
            {
                rebindActionUI.UpdateBindingDisplay(false);
            }
        }

        private void OnInputDeviceChanged(string newDevice)
        {
            rebindUIs.Clear();
            scrollRectContent.DestroyChildren();
            AddBindingsButton(newDevice);
        }
        
    }
}