using System;
using Conjure.Input;
using Sirenix.OdinInspector;
using Obvious.Soap;
using UnityEngine;

namespace Conjure
{
    public struct MenuLines
    {
        public GameObject PlayerIsntJoined;
        public GameObject PlayerIsJoined;
    }
    
    public class JoinUi : SerializedMonoBehaviour
    {
        [SerializeField, ReadOnly] private InputManagerServer inputManagerServer;
        [SerializeField, Required] private InputServerReference inputManagerGameobjectReference;
        [SerializeField, Required] private MenuLines playerOneMenuLine;
        [SerializeField, Required] private MenuLines playerTwoMenuLine;

        private void Awake()
        {
            if (inputManagerGameobjectReference is null)
            {
                throw new NullReferenceException($"inputManagerGameobjectReference shouldn't be null on {transform.name}");
            }
            if (inputManagerGameobjectReference.Value is null)
            {
                throw new NullReferenceException($"inputManagerGameobjectReference shouldn't be null on {inputManagerGameobjectReference.name}");
            }
            inputManagerServer = inputManagerGameobjectReference.Value.GetComponent<InputManagerServer>();
        }

        public void UpdateVisual(int numberPlayer)
        {
            SetElementAOrBVisible(playerOneMenuLine, numberPlayer >= 1);
            SetElementAOrBVisible(playerTwoMenuLine, numberPlayer >= 2);
        }

        /// <summary>
        /// if true, A is visible and B hidden. If false, A is hidden and B visible
        /// </summary>
        /// <param name="menuLines"></param>
        /// <param name="isVisible"></param>
        private void SetElementAOrBVisible(MenuLines menuLines, bool isVisible)
        {
            menuLines.PlayerIsJoined.SetActive(isVisible);
            menuLines.PlayerIsntJoined.SetActive(!isVisible);
        }
    }
}