using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using TSMasterCAN;
using UIShare.UIViewModel;

namespace CANModule.ViewModels
{
    public class SelectCanMessageViewModel : BindableBase, IDialogAware
    {
        #region 属性
        private ObservableCollection<int> _ChannelList;
        public ObservableCollection<int> ChannelList
        {
            get => _ChannelList;
            set => SetProperty(ref _ChannelList, value);
        }
        private ObservableCollection<string> _messageList;
        public ObservableCollection<string> MessageList
        {
            get => _messageList;
            set => SetProperty(ref _messageList, value);
        }

        private ObservableCollection<string> _signalList;
        public ObservableCollection<string> SignalList
        {
            get => _signalList;
            set => SetProperty(ref _signalList, value);
        }

        private string _title = "CollectCANSignalSetting";
        public string Title
        {
            get => _title;
            set => SetProperty(ref _title, value);
        }
        private string _selectedMessage;
        public string SelectedMessage
        {
            get => _selectedMessage;
            set
            {
                if (SetProperty(ref _selectedMessage, value))
                {
                    if (value != null && value != "")
                    {
                        SignalList = new(DBCParse.MsgDatabase[MessageChannel].Where(x => $"[0x{x.msg_id:X}]{x.msg_name}" == value).First().signal_Name);
                    }
                }
            }
        }

        private string _selectedSignal;
        public string SelectedSignal
        {
            get => _selectedSignal;
            set => SetProperty(ref _selectedSignal, value);
        }
        private int _MessageChannel;
        public int MessageChannel
        {
            get => _MessageChannel;
            set
            {
                if (_MessageChannel != value)
                {
                    _MessageChannel=value;
                    MessageList = new(DBCParse.MsgDatabase[value].Select(s => $"[0x{s.msg_id:X}]{s.msg_name}"));
                    SignalList = new();
                    SelectedMessage = null;
                    SelectedSignal = null;
                    RaisePropertyChanged();
                }
            }
        }
        #endregion
        public ICommand CloseCommand { get; set; }
        public ICommand GetSelectedMessageCommand { get; set; }
        public ICommand GetSelectedSignalCommand { get; set; }
        public ICommand LoadCommand { get; set; }
        public DialogCloseListener RequestClose { get; set; }
        public SelectCanMessageViewModel()
        {
            CloseCommand = new DelegateCommand(Close);
            GetSelectedMessageCommand = new DelegateCommand(GetSelectedMessage);
            GetSelectedSignalCommand = new DelegateCommand(GetSelectedSignal);
            LoadCommand = new DelegateCommand(Load);

        }

        private void Load()
        {
            ChannelList = new ObservableCollection<int>(Enumerable.Range(0, 12));
        }

        private void GetSelectedSignal()
        {
            if (SelectedSignal!=null)
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    Clipboard.SetDataObject(SelectedSignal);
                });
            }
            else
            {

            }
           
        }

        private void GetSelectedMessage()
        {
            if (SelectedSignal != null)
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    Clipboard.SetDataObject(SelectedMessage.Split("]")[1]);
                });
            }
            else
            {

            }
        }

        private void Close()
        {
            RequestClose.Invoke();
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            MessageList = new(DBCParse.MsgDatabase[0].Select(s => $"[0x{s.msg_id:X}]{s.msg_name}"));
        }
    }
}
