using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SlnMesnac.Business;
using SlnMesnac.WPF.Page;
using SlnMesnac.WPF.Page.History;
using SlnMesnac.WPF.Page.LogInfo;
using SlnMesnac.WPF.Page.Version;
using System;
using System.Windows;

namespace SlnMesnac.WPF.ViewModel
{
    public class MainWindowViewModel: ViewModelBase
    {
        private readonly ILogger<MainWindowViewModel> _logger;
        private readonly TagScanBusiness tagScanBusiness;

        private IndexControl indexControl = new IndexControl();
        private HistoryControl historyControl = new HistoryControl();
        private LogInfoControl logInfoControl = new LogInfoControl();

        #region 参数定义
        /// <summary>
        /// RFID状态
        /// </summary>
        private int _RfidStatus = 0;
        public int RfidStatus
        {
            get { return _RfidStatus; }
            set { _RfidStatus = value; RaisePropertyChanged(nameof(RfidStatus)); }
        }

        /// <summary>
        /// 相机状态
        /// </summary>
        private int _CamStatus = 0;
        public int CamStatus
        {
            get { return _CamStatus; }
            set { _CamStatus = value; RaisePropertyChanged(nameof(CamStatus)); }
        }

        public System.Windows.Controls.UserControl _content;

        public System.Windows.Controls.UserControl UserContent
        {
            get { return _content; }

            set
            {
                _content = value;
                RaisePropertyChanged(nameof(UserContent));
            }
        }
        #endregion

        #region 事件定义
        /// <summary>
        /// 界面跳转按钮事件
        /// </summary>
        public RelayCommand<object> ControlOnClickCommand { get; set; }

        /// <summary>
        /// 打开系统键盘
        /// </summary>
        public RelayCommand OpenSystemKeyboardCommand { get; set; }

        /// <summary>
        /// 窗体控制
        /// </summary>
        public RelayCommand<object> FormControlCommand { get; set; }
        #endregion

        public MainWindowViewModel()
        {

            var sp = App.ServiceProvider;

           _logger = App.ServiceProvider.GetService<ILogger<MainWindowViewModel>>();
            tagScanBusiness = App.ServiceProvider.GetService<TagScanBusiness>();

            ControlOnClickCommand = new RelayCommand<object>(obj => ControlOnClick(obj));
            FormControlCommand = new RelayCommand<object>(x => FormControl(x));

            UserContent = indexControl;

            tagScanBusiness.RefreshDeviceStatusEvent += (rfid, cam) =>
            {
                if (rfid)
                {
                    RfidStatus = 2;
                }
                else
                {
                    RfidStatus = 1;
                }

                if (cam)
                {
                    CamStatus = 2;
                }
                else
                {
                    CamStatus = 1;
                }
            };

        }

        /// <summary>
        ///  窗体控制
        /// </summary>
        /// <param name="obj"></param>
        private void FormControl(object obj)
        {
            try
            {
                string controlType = obj as string;
                switch (controlType)
                {
                    // 关闭当前窗口
                    case "Exit":
                        //Environment.Exit(0);
                        Application.Current.Shutdown();
                        break;
                    case "Generate":
                        
                        break;
                    // 还原 或者 最大化当前窗口
                    case "Normal":
                        if (Application.Current.MainWindow.WindowState == WindowState.Normal)
                        {
                            Application.Current.MainWindow.WindowState = WindowState.Maximized;
                            break;
                        }
                        if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
                        {
                            Application.Current.MainWindow.WindowState = WindowState.Normal;
                            break;
                        }
                        break;

                    // 最小化当前窗口
                    case "Minimized":
                        Application.Current.MainWindow.WindowState = WindowState.Minimized;
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("窗体控制逻辑异常", ex);
            }
        }

        /// <summary>
        /// 界面跳转
        /// </summary>
        private void ControlOnClick(object obj)
        {
            try
            {
                string info = obj as string;
                switch (info)
                {
                    case "Index":
                        UserContent = indexControl; break;
                    case "History":
                        UserContent = historyControl; break;
                    case "LogInfo":
                        UserContent = logInfoControl; break;
                    case "Version":
                        VersionInfoWindow versionInfoWindow = new VersionInfoWindow();
                        versionInfoWindow.Topmost = true; // 设置为最顶层
                        versionInfoWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen; // 设置为居中
                        versionInfoWindow.ShowDialog();
                        break;
                    default: break;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("界面跳转逻辑异常", ex);
            }
        }

    }
}
