using parkMonitor.controlPanel;
using parkMonitor.entity;
using parkMonitor.LOG;
using parkMonitor.model;
using parkMonitor.server.uiLogServer;
using parkMonitor.tools;
using parkMonitor.view.aboutWin;
using parkMonitor.view.deviceViewWin;
using parkMonitor.view.okBoxWin;
using parkMonitor.view.startDeviceClientWin;
using parkMonitor.view.startDeviceServerWin;
using parkMonitor.view.startNumMachineWin;
using parkMonitor.view.sysInfoWin;
using parkMonitor.viewModel;
using parkMonitor.viewModel.detail;
using PLC_Communication;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace parkMonitor.view.util
{
    /// <summary>
    /// MenuBox.xaml 的交互逻辑
    /// </summary>
    public partial class MenuBox : UserControl
    {
        Binding bindShowHeight = new Binding();
        public MenuBox()
        {
            InitializeComponent();

            ShowHeight = menuMain.ShowHeight;
            bindShowHeight.Source = menuMain;
            bindShowHeight.Path = new PropertyPath("ShowHeight");
            SetBinding(ShowHeightPro, bindShowHeight);
        }

        //ShowHeight
        public static readonly DependencyProperty ShowHeightPro = DependencyProperty.Register("ShowHeight", typeof(double), typeof(MenuBox), new PropertyMetadata(25.0));
        public double ShowHeight
        {
            get { return (double)GetValue(ShowHeightPro); }
            set { SetCurrentValue(ShowHeightPro, value); }
        }

        /// <summary>菜单-打开</summary>
        private void muOpen_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        /// <summary>菜单-关闭</summary>
        private void muClose_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        /// <summary>菜单-导入</summary>
        private void muImport_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        /// <summary>菜单-配置</summary>
        private void muConfig_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        /// <summary>菜单-退出</summary>
        private void muExit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MainWindow mainWin = MainModel.ins.mainWin;

            OkBoxWin win = new OkBoxWin();
            language.Language lng = language.Language.ins;

            win.show(mainWin, lng.exit, lng.sureExit);

            if (win.isOk)
            {
                mainWin.Close();
                System.Environment.Exit(0);
            }
        }

        ///// <summary>菜单-启动全部</summary>
        //private void muRunAll_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {}

        /// <summary>菜单-启动Web服务器</summary>
        private void muRunWebServer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Register register = new Register();
            //register.ShowDialog();
            //ManualParking mp = new ManualParking();
            //mp.ShowDialog();
        }

        /// <summary>菜单-启动号牌机</summary>
        private void muRunNumMachine_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FormModbus form = new FormModbus();
            form.ShowDialog();
        }

        /// <summary>菜单-重启上次流程</summary>
        private void muRunRestartProcess_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (sender is ImageMenuItem)
            {
                ImageMenuItem temp = ((ImageMenuItem)this.FindName("muRunRestartProcess"));
                MyTimer.restart = !MyTimer.restart;
                if (MyTimer.restart)
                {
                    temp.Content = "回滚已启动";
                    temp.Source = new BitmapImage(new Uri("/resource/image/map/circulate.png", UriKind.Relative));
                }
                else
                {
                    temp.Content = "回滚已关闭";
                    temp.Source = new BitmapImage(new Uri("/resource/image/map/circulate2.png", UriKind.Relative));
                }
            }
            //MessageBox.Show("sender:" + sender);
        }

        /// <summary>
        /// 控制面板，管理车位与缓冲位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void muControlPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ControlPanel.ins = new ControlPanel();
            ControlPanel.ins.ShowDialog();
        }

        ///// <summary>菜单-启动机械手</summary>
        //private void muRunRobot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-启动激光雷达</summary>
        //private void muRunLaser_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-启动测量系统</summary>
        //private void muRunMeasure_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-启动转运系统</summary>
        //private void muRunTray_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        /// <summary>菜单-主界面视图</summary>
        private void muMainView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        /// <summary>菜单-车位视图</summary>
        private void muCarView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-抓手视图</summary>
        //private void muRobotView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-转运视图</summary>
        //private void muTrayView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-测量系统视图</summary>
        //private void muMeasureView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-号牌机视图</summary>
        //private void muNumMachineView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-机械手日志</summary>
        //private void muRobotLog_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-转运系统日志</summary>
        //private void muTrayLog_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-测量系统日志</summary>
        //private void muMeasureLog_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-号牌机日志</summary>
        //private void muNumMachineLog_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-检查机械手</summary>
        //private void muCheckRobot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-检查转运系统</summary>
        //private void muCheckTray_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-检查测量系统</summary>
        //private void muCheckMeasure_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        ///// <summary>菜单-检查号牌机</summary>
        //private void muCheckNumMachine_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { }

        /// <summary>菜单-系统说明</summary>
        private void muExplain_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            SysInfoWin win = new SysInfoWin();
            win.show(MainModel.ins.mainWin);
        }

        /// <summary>菜单-帮助</summary>
        private void muHelp_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                string helPath = SysConst.rootPath() + @"/resource/data/help.chm";

                if (!File.Exists(helPath))
                {
                    return;
                }

                Process exep = new Process();
                exep.StartInfo.FileName = helPath;
                exep.StartInfo.WorkingDirectory = SysConst.rootPath();
                exep.StartInfo.Arguments = "";
                exep.StartInfo.CreateNoWindow = false;
                exep.StartInfo.UseShellExecute = true;
                exep.StartInfo.RedirectStandardOutput = false;
                exep.StartInfo.RedirectStandardError = false;
                //设置启动动作,确保以管理员身份运行
                //exep.StartInfo.Verb = "runas";
                exep.Start();

            }
            catch (Exception)
            {

            }
        }

        /// <summary>菜单-关于我们</summary>
        private void muAbout_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            AboutWin win = new AboutWin();
            win.show(MainModel.ins.mainWin);
        }

        private int getDevMenuIdx(DevMenuListBox lstBox)
        {
            if (lstBox == null)
            {
                return -1;
            }

            int idx = lstBox.SelectedIndex;
            if (idx < 0)
            {
                return -1;
            }

            return idx;
        }

        /// <summary>启动号牌机</summary>
        //private void lstRunNumMachine_SelectionChanged(object sender, SelectionChangedEventArgs e) {
        //	int idx = getDevMenuIdx(sender as DevMenuListBox);

        //	if(idx < 0) {
        //		return;
        //	}

        //	ObservableCollection<DevMenuItemVM> lstItem = MainModel.ins.mainWinVM.DevMenuItems.NumMachine;
        //	DevMenuItemVM item = lstItem[idx];

        //	//StartNumMachineWin win = new StartNumMachineWin();

        //	//win.show(MainModel.ins.mainWin, item);

        //	FormModbus form = new FormModbus();
        //	form.ShowDialog();
        //}

        /// <summary>启动号牌机-服务端</summary>
        //private void lstRunNumMachine_ServerClick(object sender, RoutedEventArgs e) {
        //	int idx = getDevMenuIdx(sender as DevMenuListBox);

        //	if(idx < 0) {
        //		return;
        //	}

        //	ObservableCollection<DevMenuItemVM> lstItem = MainModel.ins.mainWinVM.DevMenuItems.NumMachine;
        //	DevMenuItemVM item = lstItem[idx];

        //	StartDeviceServerWin win = new StartDeviceServerWin();

        //	win.show(MainModel.ins.mainWin, item);
        //}

        /// <summary>启动号牌机-客户端</summary>
        //private void lstRunNumMachine_ClientClick(object sender, RoutedEventArgs e) {
        //	int idx = getDevMenuIdx(sender as DevMenuListBox);

        //	if(idx < 0) {
        //		return;
        //	}

        //	ObservableCollection<DevMenuItemVM> lstItem = MainModel.ins.mainWinVM.DevMenuItems.NumMachine;
        //	DevMenuItemVM item = lstItem[idx];

        //	StartDeviceClientWin win = new StartDeviceClientWin();

        //	win.show(MainModel.ins.mainWin, item);
        //}

        /// <summary>启动激光雷达</summary>
        private void lstRunLaser_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>启动激光雷达-服务器</summary>
        private void lstRunLaser_ServerClick(object sender, RoutedEventArgs e) { }

        /// <summary>启动激光雷达-客户端</summary>
        private void lstRunLaser_ClientClick(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>启动托盘</summary>
        private void lstRunTray_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>启动托盘-服务器</summary>
        private void lstRunTray_ServerClick(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>启动托盘-客户端</summary>
        private void lstRunTray_ClientClick(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>启动机械手</summary>
        private void lstRunRobot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>启动机械手-服务器</summary>
        private void lstRunRobot_ServerClick(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>启动机械手-客户端</summary>
        private void lstRunRobot_ClientClick(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>启动光栅</summary>
        private void lstRunMeasure_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>启动光栅-服务器</summary>
        private void lstRunMeasure_ServerClick(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>启动光栅-客户端</summary>
        private void lstRunMeasure_ClientClick(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>启动超声波</summary>
        private void lstUltrasonic_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>启动传感器</summary>
        private void lstSensor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>号牌机视图</summary>
        private void lstNumMachineView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int idx = getDevMenuIdx(sender as DevMenuListBox);

            if (idx < 0)
            {
                return;
            }

            ObservableCollection<DevMenuItemVM> lstItem = MainModel.ins.mainWinVM.DevMenuItems.NumMachine;
            DevMenuItemVM item = lstItem[idx];

            //DeviceViewWin win = new DeviceViewWin();

            //win.show(MainModel.ins.mainWin, item);
        }

        /// <summary>转运系统视图</summary>
        private void lstTrayView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>机械手视图</summary>
        private void lstRobotView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>测量系统视图</summary>
        private void lstMeasureView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>号牌机预警</summary>
        private void lstNumMachineLog_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>转运系统预警</summary>
        private void lstTrayLog_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>机械手预警</summary>
        private void lstRobotLog_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>测量系统预警</summary>
        private void lstMeasureLog_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>检查号牌机</summary>
        private void lstCheckNumMachine_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>检查转运系统</summary>
        private void lstCheckTray_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>检查机械手</summary>
        private void lstCheckRobot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>检查测量系统</summary>
        private void lstCheckRaster_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

    }
}
