using MainShell.AlgorithmCalib.Service;
using MainShell.Hardware;
using MaxwellFramework.Core.Interfaces;
using MwFramework.Controls.Components;
using MwFramework.ManagerService;
using Stylet;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;

namespace MainShell.AlgorithmCalib.ViewModel
{
    /// <summary>
    /// 融合标定页面主VM（对标 MotionCalibContentsViewModel）
    /// 管理 Die融合 和 Pad融合 两个子页面
    /// </summary>
    public class FusionCalibContentsViewModel : Screen, IPage
    {
        private readonly HardwareManager _hardware;
        private readonly FusionCalibMotionService _fusionCalibMotionService;
        private bool _isInit = false;

        public string Name { get; set; } = "FusionCalib";
        public ICommand PrevCommand { get; set; }
        public ICommand NextCommand { get; set; }

        private ObservableCollection<Screen> _screens;
        public ObservableCollection<Screen> Screens
        {
            get => _screens;
            set => SetAndNotify(ref _screens, value);
        }

        private Screen _showScreenVM;
        public Screen ShowScreenVM
        {
            get => _showScreenVM;
            set => SetAndNotify(ref _showScreenVM, value);
        }

        private string _currentCalibName;
        public string CurrentCalibName
        {
            get => _currentCalibName;
            set => SetAndNotify(ref _currentCalibName, value);
        }

        private string _progressText;
        public string ProgressText
        {
            get => _progressText;
            set => SetAndNotify(ref _progressText, value);
        }

        private int _showIndex;
        public int ShowIndex
        {
            get => _showIndex;
            set => SetAndNotify(ref _showIndex, value);
        }

        private bool _isEnablePrev;
        public bool IsEnablePrev
        {
            get => _isEnablePrev;
            set => SetAndNotify(ref _isEnablePrev, value);
        }

        private bool _isEnableNext = true;
        public bool IsEnableNext
        {
            get => _isEnableNext;
            set => SetAndNotify(ref _isEnableNext, value);
        }

        public FusionCalibContentsViewModel()
        {
            _hardware = IoC.Get<HardwareManager>();
            _fusionCalibMotionService = IoC.Get<FusionCalibMotionService>();
            PrevCommand = new DelegateCommand(() => OnPrev());
            NextCommand = new DelegateCommand(() => OnNext());
        }

        protected override void OnViewLoaded()
        {
            if (_isInit) return;

            Screens = new ObservableCollection<Screen>();

            // Pad融合（多区域，打开子窗口）
            Screens.Add(FusionMultipleCalibViewModel.Create(
                "Pad融合标定", "topCameraFusion",
                _hardware.CameraAxisManager.TopCameraAxisDevices,
                _hardware, _fusionCalibMotionService));

            // Die融合（单页面）
            Screens.Add(FusionCalibViewModel.Create(
                "Die融合标定", "topWsCameraFusion",
                _hardware.CameraAxisManager.TopCameraWsAxisDevices,
                _hardware, _fusionCalibMotionService, isWaferCalib: true));

            // 融合标定验证
            Screens.Add(FusionAlignVerifyViewModel.Create(
                 _hardware.CameraAxisManager.TopCameraWsAxisDevices,
                 _hardware, _fusionCalibMotionService));

            ShowIndex = 0;
            ShowScreenVM = Screens[0];
            UpdateNavigationState();
            _isInit = true;
        }

        public void viewUnLoad()
        {
            if (Screens != null)
            {
                foreach (var screen in Screens)
                {
                    if (screen is FusionMultipleCalibViewModel 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();
        }

        private void UpdateNavigationState()
        {
            IsEnablePrev = ShowIndex > 0;
            IsEnableNext = ShowIndex < Screens.Count - 1;

            if (ShowScreenVM is FusionMultipleCalibViewModel multiVm)
                CurrentCalibName = multiVm.CalibName;
            if (ShowScreenVM is FusionCalibViewModel calibVm)
                CurrentCalibName = calibVm.CalibName;

            ProgressText = $"{ShowIndex + 1} / {Screens.Count}";
        }
    }
}
