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

public class Selectable : MonoBehaviour
{
    [field: SerializeField] public bool Selected { get; private set; } = false;
    public bool Locked
    {
        get
        {
            bool locked = _locks.Any(lck => lck != null ? lck.Value : false);
            bool ilocked = _invertedLocks.Any(lck => lck != null ? !lck.Value : false);
            return locked || ilocked;
        }
    }

    [SerializeField] private SelectableCollection _allSelectablesCollection = default;
    [SerializeField] private GameEvent _selectableUpdateEvent = default;
    [SerializeField] private List<BoolVariable> _locks = default;
    [SerializeField] private List<BoolVariable> _invertedLocks = default;

    private void Start()
    {
        _allSelectablesCollection.Add(this);
    }

    public void Select()
    {
        if (!Selected && !Locked)
        {
            foreach (var selectable in _allSelectablesCollection)
            {
                selectable.Deselect();
            }
            Selected = true;
            _selectableUpdateEvent.Raise();
        }
    }

    public void Deselect()
    {
        Selected = false;
    }
}
