using BDU.Models;
using MahApps.Metro.Controls;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
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.Shapes;
using TSMasterCAN;

namespace BDU.Views
{
    /// <summary>
    /// CANCatchSingalView.xaml 的交互逻辑
    /// </summary>
    [AddINotifyPropertyChangedInterface]
    public partial class CANCatchSingalView : MetroWindow
    {
        public CANCatchSingalView()
        {
            InitializeComponent();
            DataContext ??= this;
            Loaded += (_, _) =>
            {
                报文 = [.. DBCParse.MsgDatabase[通道].Select(s => $"[0x{s.msg_id:X}]{s.msg_name}")];
            };
        }
        public BindingList<CANSignalModel> 配置 { get; set; } = new();
        public byte 通道 { get; set; } = 0;
        public List<string> 报文 { get; set; }
        public List<string> 信号 { get; set; }
        public int 选中报文 { get; set; }
        public int 选中信号 { get; set; }
        public int 采集间隔 { get; set; } = 1000;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var find = 配置.FirstOrDefault(s => s.Channel == 通道
            && s.MessageName == DBCParse.MsgDatabase[通道][选中报文].msg_name
             && s.SignalName == DBCParse.MsgDatabase[通道][选中报文].signal_Name[选中信号]);
            try
            {
                if (find is null)
                {
                    配置.Add(new()
                    {
                        Channel = 通道,
                        MessageName = DBCParse.MsgDatabase[通道][选中报文].msg_name,
                        SignalName = DBCParse.MsgDatabase[通道][选中报文].signal_Name[选中信号],
                        CatchID = Guid.NewGuid(),
                        LogInterval = TimeSpan.FromMilliseconds(采集间隔),
                        MessageID = DBCParse.MsgDatabase[通道][选中报文].msg_id
                    });
                }
                else
                {
                    find.LogInterval = TimeSpan.FromMilliseconds(采集间隔);
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var cbb = sender as ComboBox;
                if (cbb.SelectedIndex < 0) return;
                信号 = DBCParse.MsgDatabase[通道][cbb.SelectedIndex].signal_Name.ToList();
                选中信号 = 0;

            }
            catch (Exception)
            {
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (配置表格.SelectedIndex < 0) return;
            配置.RemoveAt(配置表格.SelectedIndex);
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (sender is not TextBox o) return;
            int temp = 0;
            try
            {
                temp = Convert.ToInt32(o.Text);
                选中报文 = 0;
            }
            catch (Exception)
            {

            }

            报文 = [.. DBCParse.MsgDatabase[temp].Select(s => $"[0x{s.msg_id:X}]{s.msg_name}")];
        }
    }
}
