using SWRIS.Events;
using SWRIS.Extensions;
using SWRIS.Models.ViewModel;
using SWRIS.Repository;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace SWRIS.Pages.RealTime
{
    /// <summary>
    /// TwoRopesPage.xaml 的交互逻辑
    /// </summary>
    public partial class TwoRopesPage : Page
    {
        private readonly IRecordRepository _recordRepository;
        public TwoRopesViewModel TwoRopesView { get; set; }
        public TwoRopesPage()
        {
            InitializeComponent();
            Loaded += OneRopePage_Loaded;
            Unloaded += OneRopePage_Unloaded;
            _recordRepository = new RecordRepository();
            TwoRopesView = new TwoRopesViewModel
            {
                FirstEquipmentData = App.DataCenter.Equipments[0],
                SecondEquipmentData = App.DataCenter.Equipments[1],
                Records = _recordRepository.GetLastRecords().ToSimpleRecordDtos()
            };
            TwoRopesView.FirstEquipment = App.Config.Equipments.First(c => c.IpAddress == TwoRopesView.FirstEquipmentData.IpAddress);
            TwoRopesView.SecondEquipment = App.Config.Equipments.First(c => c.IpAddress == TwoRopesView.SecondEquipmentData.IpAddress);
            DataContext = TwoRopesView;
            App.TcpServer.LiveStreamReceived += TcpServer_LiveStreamReceived;
        }
        private void OneRopePage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (TwoRopesView.FirstEquipmentData.ChartSource != null)
            {
                firstChartLine.AddDataPoints(TwoRopesView.FirstEquipmentData.ChartSource);
            }
            if (TwoRopesView.SecondEquipmentData.ChartSource != null)
            {
                secondChartLine.AddDataPoints(TwoRopesView.SecondEquipmentData.ChartSource);
            }
        }

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

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

        private void SecondEquipmentAlarm_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DamagesDialog damagesDialog = new DamagesDialog(TwoRopesView.SecondEquipmentData.Alarms);
            damagesDialog.ShowDialog(true);
        }

        private void FirstParameterSetting_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ParameterDialog parameterDialog = new ParameterDialog(TwoRopesView.FirstEquipment, TwoRopesView.FirstEquipmentData.ClientSocket);
            parameterDialog.ShowDialog(true);
        }
        private void SecondParameterSetting_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ParameterDialog parameterDialog = new ParameterDialog(TwoRopesView.SecondEquipment, TwoRopesView.SecondEquipmentData.ClientSocket);
            parameterDialog.ShowDialog(true);
        }
    }
}
