using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Windows;
using BPASmartClient.Helper;
using Microsoft.Toolkit.Mvvm.Input;
using BPASmartClient.DosingSystem.Model;
using Newtonsoft.Json;
using System.IO;
using System.Reflection;

namespace BPASmartClient.DosingSystem.ViewModel
{
    public class MainViewModel : ObservableObject
    {
        ObservableCollection<ActionMenu> menus = new ObservableCollection<ActionMenu>();

        private Permission _permission;
        public Permission permission
        {
            get { return _permission; }
            set
            {
                var res = menus.Where(p => Array.FindIndex(p.permission, s => s == value) >= 0).ToList();
                if (res != null && res.Count > 0)
                {
                    Menus.Clear();
                    res.ForEach((item) => { Menus.Add(item); });
                }
                _permission = value;
            }
        }


        public MainViewModel()
        {
            Json<LocaPar>.Read();
            TogglePag = new RelayCommand<object>(DoNavChanged);
            Login = new RelayCommand(() => { DoNavChanged("BPASmartClient.DosingSystem.View.AdminstratorsView_用户登录"); UserManagement = false; });
            PasswordChange = new RelayCommand(() =>
            {
                //DoNavChanged("PasswordChangeView.密码修改"); 
                UserManagement = false;
            });
            ExitLogin = new RelayCommand(() =>
            {
                SystemUtils.ShowScreenKeyboard();
                //DoNavChanged("LoginView.退出登录");
                UserManagement = false;
            });
            Config.GetInstance.Init();
            LoginRegister();
            MenuInit();
            permission = Permission.管理员;
            if (Menus.Count > 0) DoNavChanged(Menus.ElementAt(0).CommandParameter);
            DeviceInquire.GetInstance.Init();

        }

        private void LoginRegister()
        {
            ActionManage.GetInstance.Register(new Func<object, object>((o) =>
            {
                if (o != null && o is string str)
                {
                    var strs = str.Split("-=");
                    if (strs != null && strs.Length == 3)
                    {
                        var us = Global.userManager.userInfos.FirstOrDefault(p => p.UserName == strs[0]);
                        if (us != null && strs[1] == us.Password && strs[2] == us.permission.ToString())
                        {
                            permission = us.permission;
                            return string.Empty;
                        }
                    }
                }
                return "用户名或密码错误";
            }), "LoginBPASmartClient.DosingSystem");
        }

        private void MenuInit()
        {
            menus.Add(new ActionMenu()
            {
                MenuName = "配方设置",
                CommandParameter = "BPASmartClient.DosingSystem.View.RecipeSettingsView_配方设置",
                permission = new Permission[] { Permission.管理员, Permission.技术员 },
            });
            menus.Add(new ActionMenu()
            {
                MenuName = "设备列表",
                CommandParameter = "BPASmartClient.DosingSystem.View.DeviceListView_设备列表",
                permission = new Permission[] { Permission.管理员, Permission.技术员 },
            });
            menus.Add(new ActionMenu()
            {
                MenuName = "硬件状态",
                CommandParameter = "BPASmartClient.DosingSystem.View.HardwareStatusView_硬件状态",
                permission = new Permission[] { Permission.管理员, Permission.操作员, Permission.观察员, Permission.技术员 },
            });
            menus.Add(new ActionMenu()
            {
                MenuName = "报警记录",
                CommandParameter = "BPASmartClient.CustomResource.Pages.View.AlarmView_报警记录",
                permission = new Permission[] { Permission.管理员, Permission.操作员, Permission.观察员, Permission.技术员 },
            });
            menus.Add(new ActionMenu()
            {
                MenuName = "配方下发",
                CommandParameter = "BPASmartClient.DosingSystem.View.RecipeControlView_配方控制",
                permission = new Permission[] { Permission.管理员, Permission.操作员 },
            });
        }

        public void DoNavChanged(object obj)
        {
            if (obj != null && obj is string stobj)
            {
                var strs = stobj.Split('_');
                if (strs != null && strs.Length == 2)
                {
                    Type type;
                    if (!stobj.Contains("BPASmartClient.DosingSystem"))
                    {
                        type = Assembly.Load("BPASmartClient.CustomResource").GetType(strs[0]);
                    }
                    else type = Type.GetType(strs[0]);
                    var res = type?.GetConstructor(System.Type.EmptyTypes)?.Invoke(null);
                    if (res != null && res is FrameworkElement fe) MyWindow = fe;
                    WindowTitleName = strs[1];
                }
            }
        }

        public RelayCommand<object> TogglePag { get; set; }

        public RelayCommand Login { get; set; }

        public RelayCommand PasswordChange { get; set; }

        public RelayCommand ExitLogin { get; set; }

        public ObservableCollection<ActionMenu> Menus { get; set; } = new ObservableCollection<ActionMenu>();

        public FrameworkElement MyWindow { get { return _mMyWindow; } set { _mMyWindow = value; OnPropertyChanged(); } }
        private FrameworkElement _mMyWindow;

        public string WindowTitleName { get { return _mWindowTitleName; } set { _mWindowTitleName = value; OnPropertyChanged(); } }
        private string _mWindowTitleName;

        public bool UserManagement { get { return _mUserManagement; } set { _mUserManagement = value; OnPropertyChanged(); } }
        private bool _mUserManagement;

        public bool AutoStart { get { return SystemHelper.GetInstance.IsAutoStart(); } set { SystemHelper.GetInstance.AutoStart(value); OnPropertyChanged(); } }
    }
}
