using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using UIShare.PubEvent;
using MainModule.ViewModels;
using UIShare.ViewModelBase;


namespace MainModule.ViewModels
{
    public class MainViewModel : NavigateViewModelBase, IRegionMemberLifetime
    {
        #region 私有字段
        private bool IsInitialized = false;
        private string _expandedCellName = string.Empty;
        #endregion
        
        #region 属性
        public bool KeepAlive => true; // 保持存活
        
        public string ExpandedCellName
        {
            get => _expandedCellName;
            set => SetProperty(ref _expandedCellName, value);
        }
        #endregion
        
        #region 命令
        public ICommand LoadedCommand { get; set; }
        #endregion
        
        public MainViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            LoadedCommand = new DelegateCommand(OnLoaded);
            _eventAggregator.GetEvent<ExpandViewEvent>().Subscribe(OnCellExpandRequested);
        }
        
        #region 命令处理与事件
        private void OnLoaded()
        {
            if (IsInitialized) return;
            for (int i = 1; i <= 9; i++)
            {
                var parameters = new NavigationParameters { { "Name", $"TestCell{i}" } };
                _regionManager.RequestNavigate($"TestCell{i}", "ProtocolStartView", parameters);
            }
            IsInitialized = true;
        }
        
        // 不再物理搬迁视图，只需修改一个字符串属性 ExpandedCellName，
        // XAML 里每个单元的 Style.Triggers 会根据该值处理隐藏 / 跨越 3x3。
        private void OnCellExpandRequested(string cellName)
        {
            ExpandedCellName = cellName ?? string.Empty;
        }
        #endregion

        #region 重写
        public override bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // 之前帮你改好的单例复用逻辑（防止重复 new 和初始化视图）
            return true;
        }
        #endregion
    }
}