using Panuon.WPF.UI;
using SWRIS.Core;
using SWRIS.Enums;
using SWRIS.Events;
using SWRIS.Extensions;
using SWRIS.Models;
using SWRIS.Models.ViewModel;
using System;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace SWRIS.Pages
{
    /// <summary>
    /// ParameterDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ParameterDialog : WindowX
    {
        private string _serialNo;
        private Socket _clientSocket;
        private EquipmentSettingViewModel Equipment { get; set; }

        public ParameterDialog(EquipmentModel equipment, Socket clientSocket)
        {
            InitializeComponent();
            _serialNo = equipment.SerialNo;
            _clientSocket = clientSocket;
            Equipment = equipment.ToEquipmentSettingViewModel();
            Equipment.IsConnected = _clientSocket != null && _clientSocket.Connected;
            App.TcpServer.SendGetTwistFactorData(_serialNo, _clientSocket);
            App.TcpServer.ClockResultReceived += TcpServer_ClockResultReceived;
            App.TcpServer.EncoderDirectionResultReceived += TcpServer_EncoderDirectionResultReceived;
            DataContext = Equipment;
        }

        private void TcpServer_EncoderDirectionResultReceived(object sender, EncoderDirectionResultReceivedEventArgs e)
        {
            Equipment.Parameter.EncoderDirection = e.Direction;
        }

        private void TcpServer_ClockResultReceived(object sender, ClockResultReceivedEventArgs e)
        {
            Equipment.Parameter.SystemTime = e.Timestamp.TimestampToDateTime();
        }

        protected void SetParameter()
        {
            ThreadPool.QueueUserWorkItem(act =>
            {
                EquipmentModel equipment = Equipment.ToEquipmentModel();
                if (_clientSocket != null && _clientSocket.Connected)
                {
                    EquipmentDataModel equipmentData = App.DataCenter.Equipments.FirstOrDefault(c => c.IpAddress == equipment.IpAddress);
                    equipment.ToEquipmentDataModel(equipmentData);
                    StopDetection(equipmentData);
                    Thread.Sleep(1000);
                    App.TcpServer.SendSyncParameters(equipment.Parameter, Equipment.SerialNo, _clientSocket);
                    Thread.Sleep(300);
                    App.TcpServer.SendTurnLiveStreamData(true, _serialNo, _clientSocket);
                    Thread.Sleep(1000);
                    StartDetection(equipmentData);

                }
                // 更新设备数据
                if (ConfigHelper.SetEquipment(equipment))
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        Thread.Sleep(500);
                        NoticeBox.Show("参数设置成功", "通知", MessageBoxIcon.Success, true, 5000);
                        DialogResult = true;
                    });
                    App.UpdateEquipment(equipment);
                }
            });
        }
        private void StopDetection(EquipmentDataModel equipmentData)
        {
            equipmentData.RunningStatus |= RunningStatus.PreStop;
            App.TcpServer.SendStartOrStopDetection(false, _serialNo, _clientSocket);
        }
        private void StartDetection(EquipmentDataModel equipmentData)
        {
            equipmentData.RunningStatus |= RunningStatus.PreRunning;
            App.TcpServer.SendStartOrStopDetection(true, _serialNo, _clientSocket);
        }

        protected void GetEncoderDirection()
        {
            App.TcpServer.SendGetEncoderDirectionData(_serialNo, _clientSocket);
        }
        protected void SetEncoderDirection(EncoderDirection direction)
        {
            App.TcpServer.SendSetEncoderDirectionData(direction, _serialNo, _clientSocket);
        }
        protected string GetSerialNo()
        {
            return App.Config.GenerateNewSerialNo();
        }
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            SetParameter();
        }

        private void SetClockAndDirection_Click(object sender, RoutedEventArgs e)
        {
            if (Equipment.Parameter.SystemTime != null)
            {
                var systemTime = Equipment.Parameter.SystemTime.Value;
                App.TcpServer.SendSetClockData(systemTime.DateTimeToTimestamp(), _serialNo, _clientSocket);
            }
            Task.Delay(20).Wait();
            App.TcpServer.SendSetEncoderDirectionData(Equipment.Parameter.EncoderDirection, _serialNo, _clientSocket);
        }

        private void ReadClockAndDirection_Click(object sender, RoutedEventArgs e)
        {
            App.TcpServer.SendGetClockData(_serialNo, _clientSocket);
            Task.Delay(20).Wait();
            App.TcpServer.SendGetEncoderDirectionData(_serialNo, _clientSocket);
        }

        private void Clock_GotFocus(object sender, RoutedEventArgs e)
        {
            if (Equipment.Parameter.SystemTime == null)
            {
                Equipment.Parameter.SystemTime = DateTime.Now;
            }
        }

        private void SerialNo_GotFocus(object sender, RoutedEventArgs e)
        {
            if (Equipment.SerialNo.IsNullOrEmptyOrWhiteSpace())
            {
                Equipment.SerialNo = App.Config.GenerateNewSerialNo();
            }
        }

        private void SetSerialNo_Click(object sender, RoutedEventArgs e)
        {
            App.TcpServer.SendSerialNoRequest(Equipment.SerialNo, _clientSocket);
        }

        private void Upgrade_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var result = MessageBoxX.Show(this, "是否确认更新设备固件？此操作将会停止设备工作。", "警告",
               MessageBoxButton.YesNo,
               MessageBoxIcon.Warning, DefaultButton.YesOK);
            if (result == MessageBoxResult.Yes)
            {
                App.TcpServer.SendUpgradeRequest(_serialNo, _clientSocket);
            }
           
        }
    }
}
