using System;
using System.Collections.Generic;
using System.Linq;

namespace NavisworksTransport.Utils
{
    /// <summary>
    /// 管理“按选择对象聚焦”的剖面盒锁定状态。
    /// 一旦剖面盒按当前选择启用，就保持该场景稳定，直到用户主动关闭。
    /// </summary>
    public class SelectionClipBoxLockState
    {
        private IReadOnlyList<object> _lockedSelection = Array.Empty<object>();

        public bool IsLocked => _lockedSelection.Count > 0;

        public int LockedSelectionCount => _lockedSelection.Count;

        public void LockSelection<T>(IEnumerable<T> selectedItems)
        {
            if (selectedItems == null)
            {
                throw new ArgumentNullException(nameof(selectedItems));
            }

            var lockedItems = selectedItems
                .Cast<object>()
                .Where(item => item != null)
                .ToList();

            if (lockedItems.Count == 0)
            {
                throw new InvalidOperationException("锁定剖面盒前必须提供至少一个选中对象。");
            }

            _lockedSelection = lockedItems;
        }

        public bool ShouldRefreshOnSelectionChanged(bool isSelectionClipBoxMode)
        {
            return isSelectionClipBoxMode && !IsLocked;
        }

        public void Clear()
        {
            _lockedSelection = Array.Empty<object>();
        }
    }
}
