using SWRIS.Core;
using SWRIS.Dtos;
using SWRIS.Events;
using SWRIS.Extensions;
using SWRIS.Models.ViewModel;
using SWRIS.Repository;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace SWRIS.Pages.RealTime
{
    /// <summary>
    /// ThreeRopesPage.xaml 的交互逻辑
    /// </summary>
    public partial class ThreeRopesPage : Page
    {
        private readonly IRecordRepository _recordRepository;
        public ThreeRopesViewModel ThreeRopesView { get; set; }
        public ThreeRopesPage()
        {
            InitializeComponent();
            _recordRepository = new RecordRepository();
            Loaded += OneRopePage_Loaded;
            Unloaded += OneRopePage_Unloaded;
            ThreeRopesView = new ThreeRopesViewModel
            {
                FirstEquipmentData = App.DataCenter.Equipments[0],
                SecondEquipmentData = App.DataCenter.Equipments[1],
                ThirdEquipmentData = App.DataCenter.Equipments[2],
                Records = _recordRepository.GetLastRecords().ToSimpleRecordDtos()
            };
            ThreeRopesView.FirstEquipment = App.Config.Equipments.First(c => c.IpAddress == ThreeRopesView.FirstEquipmentData.IpAddress);
            ThreeRopesView.SecondEquipment = App.Config.Equipments.First(c => c.IpAddress == ThreeRopesView.SecondEquipmentData.IpAddress);
            ThreeRopesView.ThirdEquipment = App.Config.Equipments.First(c => c.IpAddress == ThreeRopesView.ThirdEquipmentData.IpAddress);
            DataContext = ThreeRopesView;

            App.TcpServer.LiveStreamReceived += TcpServer_LiveStreamReceived;
        }
        private void OneRopePage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (ThreeRopesView.FirstEquipmentData.ChartSource != null)
            {
                firstChartLine.AddDataPoints(ThreeRopesView.FirstEquipmentData.ChartSource);
            }
            if (ThreeRopesView.SecondEquipmentData.ChartSource != null)
            {
                secondChartLine.AddDataPoints(ThreeRopesView.SecondEquipmentData.ChartSource);
            }
            if (ThreeRopesView.ThirdEquipmentData.ChartSource != null)
            {
                thirdChartLine.AddDataPoints(ThreeRopesView.ThirdEquipmentData.ChartSource);
            }
        }

        private void OneRopePage_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ThreeRopesView.FirstEquipmentData.ChartSource = firstChartLine.GetChartSource();
            ThreeRopesView.SecondEquipmentData.ChartSource = secondChartLine.GetChartSource();
            ThreeRopesView.ThirdEquipmentData.ChartSource = thirdChartLine.GetChartSource();
        }
        private async void TcpServer_LiveStreamReceived(object sender, LiveStreamReceivedEventArgs e)
        {
            if (e.IpAddress == ThreeRopesView.FirstEquipment.IpAddress)
            {
                if (ThreeRopesView.FirstEquipmentData.DetectionData?.DetectionNo == ThreeRopesView.FirstEquipmentData.DetectionNo)
                {
                    ThreeRopesView.FirstEquipmentData.ClearData();
                }
                await Task.Run(() =>
                {
                    firstChartLine.AddDataPoints((ThreeRopesView.FirstEquipmentData.AbsolutePosition, e.LiveStream), ThreeRopesView.FirstEquipmentData.InUseSensor);
                });
            }
            else if (e.IpAddress == ThreeRopesView.SecondEquipmentData.IpAddress)
            {
                if (ThreeRopesView.SecondEquipmentData.DetectionData?.DetectionNo == ThreeRopesView.SecondEquipmentData.DetectionNo)
                {
                    ThreeRopesView.SecondEquipmentData.ClearData();
                }
                await Task.Run(() =>
                {
                    secondChartLine.AddDataPoints((ThreeRopesView.SecondEquipmentData.AbsolutePosition, e.LiveStream), ThreeRopesView.SecondEquipmentData.InUseSensor);
                });
            }
            else if (e.IpAddress == ThreeRopesView.ThirdEquipmentData.IpAddress)
            {
                if (ThreeRopesView.ThirdEquipmentData.DetectionData?.DetectionNo == ThreeRopesView.ThirdEquipmentData.DetectionNo)
                {
                    ThreeRopesView.ThirdEquipmentData.ClearData();
                }
                await Task.Run(() =>
                {
                    thirdChartLine.AddDataPoints((ThreeRopesView.ThirdEquipmentData.AbsolutePosition, e.LiveStream), ThreeRopesView.ThirdEquipmentData.InUseSensor);
                });
            }
        }

        private void FirstEquipmentAlarm_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DamagesDialog damagesDialog = new DamagesDialog(ThreeRopesView.FirstEquipmentData.Alarms);
            damagesDialog.ShowDialog(true);
        }

        private void SecondEquipmentAlarm_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DamagesDialog damagesDialog = new DamagesDialog(ThreeRopesView.SecondEquipmentData.Alarms);
            damagesDialog.ShowDialog(true);
        }
        private void ThreeEquipmentAlarm_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DamagesDialog damagesDialog = new DamagesDialog(ThreeRopesView.ThirdEquipmentData.Alarms);
            damagesDialog.ShowDialog(true);
        }
        private void FirstParameterSetting_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ParameterDialog parameterDialog = new ParameterDialog(ThreeRopesView.FirstEquipment, ThreeRopesView.FirstEquipmentData.ClientSocket);
            parameterDialog.ShowDialog(true);
        }
        private void SecondParameterSetting_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ParameterDialog parameterDialog = new ParameterDialog(ThreeRopesView.SecondEquipment, ThreeRopesView.SecondEquipmentData.ClientSocket);
            parameterDialog.ShowDialog(true);
        }
        private void ThreeParameterSetting_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ParameterDialog parameterDialog = new ParameterDialog(ThreeRopesView.ThirdEquipment, ThreeRopesView.ThirdEquipmentData.ClientSocket);
            parameterDialog.ShowDialog(true);
        }
    }
}
