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>
    /// FourRopesPage.xaml 的交互逻辑
    /// </summary>
    public partial class FourRopesPage : Page
    {
        private readonly IRecordRepository _recordRepository;
        public FourRopesViewModel FourRopesView { get; set; }
        public FourRopesPage()
        {
            InitializeComponent();
            Loaded += OneRopePage_Loaded;
            Unloaded += OneRopePage_Unloaded;
            _recordRepository = new RecordRepository();
            FourRopesView = new FourRopesViewModel
            {
                FirstEquipmentData = App.DataCenter.Equipments[0],
                SecondEquipmentData = App.DataCenter.Equipments[1],
                ThirdEquipmentData = App.DataCenter.Equipments[2],
                FourthEquipmentData = App.DataCenter.Equipments[3],
                Records = _recordRepository.GetLastRecords().ToSimpleRecordDtos()
            };
            FourRopesView.FirstEquipment = App.Config.Equipments.First(c => c.IpAddress == FourRopesView.FirstEquipmentData.IpAddress);
            FourRopesView.SecondEquipment = App.Config.Equipments.First(c => c.IpAddress == FourRopesView.SecondEquipmentData.IpAddress);
            FourRopesView.ThirdEquipment = App.Config.Equipments.First(c => c.IpAddress == FourRopesView.ThirdEquipmentData.IpAddress);
            FourRopesView.FourthEquipment = App.Config.Equipments.First(c => c.IpAddress == FourRopesView.FourthEquipmentData.IpAddress);
            DataContext = FourRopesView;

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

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

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

        private void SecondEquipmentAlarm_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DamagesDialog damagesDialog = new DamagesDialog(FourRopesView.SecondEquipmentData.Alarms);
            damagesDialog.ShowDialog(true);
        }
        private void ThirdEquipmentAlarm_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DamagesDialog damagesDialog = new DamagesDialog(FourRopesView.ThirdEquipmentData.Alarms);
            damagesDialog.ShowDialog(true);
        }
        private void FourthEquipmentAlarm_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DamagesDialog damagesDialog = new DamagesDialog(FourRopesView.FourthEquipmentData.Alarms);
            damagesDialog.ShowDialog(true);
        }
        private void FirstParameterSetting_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ParameterDialog parameterDialog = new ParameterDialog(FourRopesView.FirstEquipment, FourRopesView.FirstEquipmentData.ClientSocket);
            parameterDialog.ShowDialog(true);
        }
        private void SecondParameterSetting_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ParameterDialog parameterDialog = new ParameterDialog(FourRopesView.SecondEquipment, FourRopesView.SecondEquipmentData.ClientSocket);
            parameterDialog.ShowDialog(true);
        }
        private void ThirdParameterSetting_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ParameterDialog parameterDialog = new ParameterDialog(FourRopesView.ThirdEquipment, FourRopesView.ThirdEquipmentData.ClientSocket);
            parameterDialog.ShowDialog(true);
        }
        private void FourthParameterSetting_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ParameterDialog parameterDialog = new ParameterDialog(FourRopesView.FourthEquipment, FourRopesView.FourthEquipmentData.ClientSocket);
            parameterDialog.ShowDialog(true);
        }

    }
}
