using System.Linq;
using System;
using System.Reactive;
using System.Reactive.Linq;
using detect.gui.Api;
using detect.gui.Commons;
using detect.gui.Models;
using ReactiveUI;

namespace detect.gui.ViewModels;

    public class MainViewModel : ViewModelBase, IScreen
    {
        private RoutingState _router;
        public RoutingState Router
        {
            get => _router;
            set => this.RaiseAndSetIfChanged(ref _router, value);
        }

        /// <summary>
        /// 当前登录用户
        /// </summary>
        private UserModel? _currentUser;
        public UserModel? CurrentUser
        {
            get => _currentUser;
            set => this.RaiseAndSetIfChanged(ref _currentUser, value);
        }
        
        public bool IsHomeView { get; set; }
        public bool IsDetectTaskView { get; set; }
        public bool IsDeviceView { get; set; }
        public bool IsLogView { get; set; }
        public bool IsUserView { get; set; }
        public MainViewModel()
        {
            // ApiService.Instance().StartService();
            
            _router = new RoutingState();

            GotoCommand = ReactiveCommand.Create<string>((p) => Goto(p));

            LogoutCommand = ReactiveCommand.Create(Logout);

            Router.CurrentViewModel.Select(x => x)
                .Subscribe(v =>
                {
                    IsHomeView = v is HomeViewModel;
                    IsDetectTaskView = v is DetectTaskViewModel;
                    IsDeviceView = v is DeviceViewModel;
                    IsLogView = v is LogViewModel;
                    IsUserView = v is UserViewModel;
                    this.RaisePropertyChanged(nameof(IsHomeView));
                    this.RaisePropertyChanged(nameof(IsDetectTaskView));
                    this.RaisePropertyChanged(nameof(IsDeviceView));
                    this.RaisePropertyChanged(nameof(IsLogView));
                    this.RaisePropertyChanged(nameof(IsUserView));
                });
            
            RouterHelper.Init(Router);
        }

        public void Dispose()
        {
            // ApiService.Instance().StopService();
        }
        
        #region 路由
        public ReactiveCommand<string, Unit> GotoCommand { get; }

        public void Goto(string viewModelTypeName, object? param = null)
        {
            var name = Router.CurrentViewModel.ToEnumerable().FirstOrDefault()!.GetType().Name;
            if (name == viewModelTypeName) return;
            var viewModelType = viewModelTypeName.GetTypeByName();
            if (viewModelType == null) return;
            var viewModelItem = RouterHelper.FindViewModelItem(RouterHelper.ViewModelList, viewModelTypeName, null);
            viewModelItem?.Goto(param);
        }
        
        public void GoBack()
        {
            var v = Router.CurrentViewModel.ToEnumerable().FirstOrDefault();
            var viewModelItem = RouterHelper.FindViewModelItem(RouterHelper.ViewModelList, v!.GetType().Name, null);
            viewModelItem?.Goback();
        }
        #endregion

        #region 注销
        public ReactiveCommand<Unit, Unit> LogoutCommand { get; }

        private void Logout()
        {
            CurrentUser = null;
            Router.NavigationStack.Clear();
            Router.Navigate.Execute(new LoginViewModel());
        }
        #endregion
    }