using MaxwellFramework.Core.Common.Command;
using MwFramework.Controls.Components;
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using DelegateCommand = MwFramework.Controls.Components.DelegateCommand;

namespace MainShell.Resources.CustomControl
{
    public class PaginationViewModel : PropertyChangedBase
    {
        private int _currentPage = 1;
        private int _pageSize = 20;
        private int _totalItems;
        private int _jumpPage = 1; // 新增跳转页字段

        public List<int> PageSizeOptions { get; } = new List<int> { 20, 50, 100, 200 };

        public int CurrentPage
        {
            get => _currentPage;
            set
            {
                int validated = Math.Max(1, Math.Min(value, TotalPages == 0 ? 1 : TotalPages));
                if (SetAndNotify(ref _currentPage, validated))
                {
                    JumpPage = validated; // 页面变更时同步更新跳转输入框数值
                    PageChanged?.Invoke(this, EventArgs.Empty);
                    // 关键：通知命令状态刷新
                    (MoveFirstCommand as DelegateCommand)?.RaiseCanExecuteChanged();
                    (MoveBackCommand as DelegateCommand)?.RaiseCanExecuteChanged();
                    (MoveNextCommand as DelegateCommand)?.RaiseCanExecuteChanged();
                    (MoveLastCommand as DelegateCommand)?.RaiseCanExecuteChanged();
                }
            }
        }

        // 新增跳转页属性
        public int JumpPage
        {
            get => _jumpPage;
            set
            {
                if (SetAndNotify(ref _jumpPage, value))
                {
                    // 输入变化时刷新跳转按钮状态
                    (JumpToPageCommand as DelegateCommand)?.RaiseCanExecuteChanged();
                }
            }
        }

        public int PageSize
        {
            get => _pageSize;
            set
            {
                if (SetAndNotify(ref _pageSize, value))
                {
                    OnPropertyChanged(nameof(TotalPages));
                    CurrentPage = 1; // 内部会触发 PageChanged
                    PageChanged?.Invoke(this, EventArgs.Empty);
                    (JumpToPageCommand as DelegateCommand)?.RaiseCanExecuteChanged();
                }
            }
        }

        public int TotalItems
        {
            get => _totalItems;
            set
            {
                if (SetAndNotify(ref _totalItems, value))
                {
                    OnPropertyChanged(nameof(TotalPages));
                    // 刷新命令状态，防止数据更新后按钮不可用
                    (MoveNextCommand as DelegateCommand)?.RaiseCanExecuteChanged();
                    (MoveLastCommand as DelegateCommand)?.RaiseCanExecuteChanged();
                    (JumpToPageCommand as DelegateCommand)?.RaiseCanExecuteChanged();
                }
            }
        }

        public int TotalPages => (int)Math.Ceiling((double)_totalItems / Math.Max(1, _pageSize));

        // 优化后的命令，增加了 CanExecute 判断
        public ICommand MoveFirstCommand => new DelegateCommand(() => CurrentPage = 1, () => CurrentPage > 1);
        public ICommand MoveBackCommand => new DelegateCommand(() => CurrentPage--, () => CurrentPage > 1);
        public ICommand MoveNextCommand => new DelegateCommand(() => CurrentPage++, () => CurrentPage < TotalPages);
        public ICommand MoveLastCommand => new DelegateCommand(() => CurrentPage = TotalPages, () => CurrentPage < TotalPages);

        // 新增跳转命令
        public ICommand JumpToPageCommand => new DelegateCommand(
            () => CurrentPage = JumpPage, 
            () => JumpPage >= 1 && JumpPage <= TotalPages);

        public event EventHandler PageChanged;
    }
}
