using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using MegaRobo.Authentication;
using MegaRobo.C00225155.Entities.Enums;
using MegaRobo.Entities;
using MegaRobo.WpfInfrastructure.Abstractions;
using NetTaste;
using System;
using System.Collections.Generic;
using System.Linq;
using MegaRobo.C00225155.AppServer;
using System.Windows.Navigation;
using Common;
using SqlSugar;
using System.Collections.ObjectModel;
using SQLitePCL;

namespace MegaRobo.C00225155App.MenuViewModels
{
    public sealed class MainWindowViewModel : ViewModelBaseEx
    {
        private AuthService authService { get; set; }
        private AppConfigService appConfigService;

        private NavigationMenu selectedMenu;
        public NavigationMenu SelectedMenu
        {
            get => selectedMenu;
            set => SetProperty(ref selectedMenu, value);
        }

        private ObservableCollection<NavigationMenu> navigationMenus = new ObservableCollection<NavigationMenu>();
        public ObservableCollection<NavigationMenu> NavigationMenuList
        {
            get => navigationMenus;
            set => SetProperty(ref navigationMenus, value);
        }

        public RelayCommand SelectedMenuCommand { get; set; }

        private bool peifangIsEnabled = true;
        public bool PeifangIsEnabledState
        {
            get { return peifangIsEnabled; }
            set
            {
                SetProperty(ref peifangIsEnabled, value);
            }
        }

        protected override void LoadServices()
        {
            base.LoadServices();
            authService = this.CoreService.GetServiceInstance<AuthService>();
            authService.AuthStatusEvent += AuthService_AuthStatusEvent;
            appConfigService = this.CoreService.GetServiceInstance<AppConfigService>();
            WeakReferenceMessenger.Default.Register<SingleBoolPeifangMessage>(this, (r, m) =>
            {
                PeifangIsEnabledState = m.Value;
                LoadNavigationMenuList();
            });
        }

        private void AuthService_AuthStatusEvent(object sender, User e)
        {
            this.LoadNavigationMenuList();
        }

        protected override void LoadDatas()
        {
            base.LoadDatas();
            this.LoadNavigationMenuList();
        }

        private void LoadNavigationMenuList()
        {
            var curUserLevel = authService?.LoginUser?.AuthLevel;
            var navigationMenuList = new ObservableCollection<NavigationMenu>();
            navigationMenuList.Add(new NavigationMenu()
            {
                Descr = "主页面",
                Header = "主页面",
                DisplaySort = (int)ViewNames.HomeView,
                IconKind = "homeoutline",
                ViewName = ViewNames.HomeView.ToString()
            });

            navigationMenuList.Add(new NavigationMenu()
            {
                Descr = "配方流程",
                Header = "配方流程",
                DisplaySort = (int)ViewNames.FlowConfigView,
                IconKind = "chemicalweapon",
                ViewName = ViewNames.FlowConfigView.ToString(),
                IsEnabled = PeifangIsEnabledState,
            });

            navigationMenuList.Add(new NavigationMenu()
            {
                Descr = "试剂管理",
                Header = "试剂管理",
                DisplaySort = (int)ViewNames.AdditivePipetteConfigView,
                IconKind = "hydraulicoillevel",
                ViewName = ViewNames.AdditivePipetteConfigView.ToString()
            });

            navigationMenuList.Add(new NavigationMenu()
            {
                Descr = "设置",
                Header = "硬件地址",
                DisplaySort = (int)ViewNames.SettingView,
                IconKind = "databasecogoutline",
                ViewName = ViewNames.SettingView.ToString()
            });

            navigationMenuList.Add(new NavigationMenu()
            {
                Descr = "通讯调试",
                Header = "硬件调试",
                DisplaySort = (int)ViewNames.CommunicationDebugView,
                IconKind = "lanconnect",
                ViewName = ViewNames.CommunicationDebugView.ToString()
            });

            //navigationMenuList.Add(new NavigationMenu()
            //{
            //    Descr = "数据结果",
            //    Header = "数据结果",
            //    DisplaySort = (int)ViewNames.ReactBottleResultView,
            //    IconKind = "databasesearchoutline",
            //    ViewName = ViewNames.ReactBottleResultView.ToString()
            //});

            navigationMenuList.Add(new NavigationMenu()
            {
                Descr = "日志",
                Header = "日志查询",
                DisplaySort = (int)ViewNames.LoggerView,
                IconKind = "MathLog",
                ViewName = ViewNames.LoggerView.ToString()
            });

            navigationMenuList.Add(new NavigationMenu()
            {
                Descr = "用户",
                Header = "用户管理",
                DisplaySort = (int)ViewNames.UserView,
                IconKind = "accountsupervisor",
                ViewName = ViewNames.UserView.ToString()
            });

            //根据当前的用户等级，更新菜单栏
            NavigationMenuList = new ObservableCollection<NavigationMenu>();
            foreach (var item in navigationMenuList)
            {
                var vn = (ViewNames)Enum.Parse(typeof(ViewNames), item.ViewName);
                var needLevel = this.appConfigService.ViewNameAuthLevelsDic[vn];
                if (needLevel == AuthLevel.None || curUserLevel >= needLevel) NavigationMenuList.Add(item);
            }
        }

        protected override void Register()
        {
            base.Register();


            //WeakReferenceMessenger.Default.Send(new ShowViewMessage(this.SelectedMenu.ViewName)
            //{
            //    Data = SelectedMenu,
            //    IsCache = SelectedMenu.IsCache,
            //});

            this.SelectedMenuCommand =
                new RelayCommand(() =>
                    {
                        WeakReferenceMessenger.Default.Send(
                            new ShowViewMessage(this.SelectedMenu.ViewName)
                            {
                                Data = this.SelectedMenu,
                                IsCache = false
                            });
                    },
                () => this.SelectedMenu != null);
            //ShowWindowCommand = new RelayCommand<string>(OnShowWindowCommand);
        }

    }
}
