using MainShell.Hardware;
using MaxwellFramework.Core.Interfaces;
using MwFramework.Controls.Components;
using MwFramework.ManagerService;
using Stylet;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml.Linq;

namespace MainShell.AlgorithmCalib.ViewModel
{
    public class MotionCalibContentsViewModel : Screen, IPage
    {
        private HardwareManager _hardware;
        public ICommand PrevCommand { get; set; }
        public ICommand NextCommand { get; set; }
        private IParameterManager _parameterManager;
        public string Name { get; set; } = "MotionCalib";
        private bool _isInit = false;

        private ObservableCollection<Screen> _screens;
        /// <summary>
        /// 需要显示的界面
        /// </summary>
        public ObservableCollection<Screen> Screens
        {
            get { return _screens; }
            set
            { SetAndNotify(ref _screens, value); }
        }
        private Screen _showScreenVM;
        /// <summary>
        /// 当前显示界面的VM
        /// </summary>
        public Screen ShowScreenVM
        {
            get { return _showScreenVM; }
            set
            { SetAndNotify(ref _showScreenVM, value); }
        }
        private string _currentCalibName;
        /// <summary>
        /// 当前标定名称，供UI标定
        /// </summary>
        public string CurrentCalibName
        {
            get { return _currentCalibName; }
            set { SetAndNotify(ref _currentCalibName, value); }
        }
        private string _progressText;
        /// <summary>
        /// 进度提示
        /// </summary>
        public string ProgressText
        {
            get { return _progressText; }
            set { SetAndNotify(ref _progressText, value); }
        }
        private int _ShowIndex;
        /// <summary>
        /// 当前显示界面的下标
        /// </summary>
        public int ShowIndex
        {
            get { return _ShowIndex; }
            set
            { SetAndNotify(ref _ShowIndex, value); }
        }
        private bool _IsEnablePrev;
        /// <summary>
        /// 上一个按钮是否启用
        /// </summary>
        public bool IsEnablePrev
        {
            get { return _IsEnablePrev; }
            set
            { SetAndNotify(ref _IsEnablePrev, value); }
        }
        private bool _IsEnableNext = true;
        /// <summary>
        /// 下一个按钮是否启用
        /// </summary>
        public bool IsEnableNext
        {
            get { return _IsEnableNext; }
            set
            { SetAndNotify(ref _IsEnableNext, value); }
        }

        public MotionCalibContentsViewModel(IParameterManager paraManager)
        {
            _hardware = IoC.Get<HardwareManager>();
            this._parameterManager = paraManager;
            PrevCommand = new DelegateCommand(() =>
            {
                OnPrev();
            });

            NextCommand = new DelegateCommand(() =>
            {
                OnNext();
            });
        }

        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            if (!_isInit)
            {
                Screens = new ObservableCollection<Screen>();
                Screens.Add(MotionMultipleCalibViewModel.Create("上相机运动系标定", "topCameraMotion", _hardware.CameraAxisManager.TopCameraAxisDevices));
                var cameraConfigs = new List<(string Name, string FileSaveName, List<HardwareDevice> Device)>
                {
                    ("上相机WS运动系标定","topWsCameraMotion",  _hardware.CameraAxisManager.TopCameraWsAxisDevices),
                    ("广角相机运动系标定","wideCameraMotion", _hardware.CameraAxisManager.WideCameraAxisDevices),
                    ("广角相机WS运动系标定","wideWsCameraMotion", _hardware.CameraAxisManager.WideCameraWsAxisDevices),
                    ("下相机运动系标定","bottomCameraMotion", _hardware.CameraAxisManager.BottomCameraAxisDevices),
                };
                foreach (var (name, fileSaveName, device) in cameraConfigs)
                {
                    Screens.Add(MotionCalibViewModel.Create(name, fileSaveName, device));
                }
                ShowIndex = 0;
                ShowScreenVM = Screens[0];
                UpdateNavigationState();
                _isInit = true;
            }
        }

        public void viewUnLoad()
        {
            // 关闭所有缓存的标定子窗口
            if (Screens != null)
            {
                foreach (var screen in Screens)
                {
                    if (screen is MotionMultipleCalibViewModel menuVm)
                    {
                        menuVm.CleanupCalibWindows();
                    }
                }
            }
        }

        private void OnPrev()
        {
            if (ShowIndex <= 0) return;

            ShowIndex--;
            ShowScreenVM = Screens[ShowIndex];
            UpdateNavigationState();  // ← 加上这行
        }

        private void OnNext()
        {
            if (ShowIndex >= Screens.Count - 1) return;

            ShowIndex++;
            ShowScreenVM = Screens[ShowIndex];
            UpdateNavigationState();  // ← 加上这行
        }

        /// <summary>
        /// 统一更新导航状态和当前相机信息
        /// </summary>
        private void UpdateNavigationState()
        {
            IsEnablePrev = ShowIndex > 0;
            IsEnableNext = ShowIndex < Screens.Count - 1;

            // 更新当前相机名称
            if (ShowScreenVM is MotionMultipleCalibViewModel camVm)
            {
                CurrentCalibName = camVm.CalibName;
            }

            if (ShowScreenVM is MotionCalibViewModel Vm)
            {
                CurrentCalibName = Vm.CalibName;
            }
            // 更新进度
            ProgressText = $"{ShowIndex + 1} / {Screens.Count}";
        }
    }
}
