using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using 常用工具集.Base;
using EasyModbus;
using MES.Utility.Core;
using System.Windows;
using 常用工具集.Utility.Network.Mitsubishi;
using System.Linq;
using Avalonia.Threading;

namespace 常用工具集.ViewModel._01PLC通信调试
{
    public class MC3E服务模拟ViewModel : ViewModelBase
    {
        internal static MC3EServer server;
        public bool Enabled1 { get; set; } = true;
        public bool Enabled2 { get; set; } = false;
        public int Port { get; set; } = 5001;
        private List<string> logMessageList = new List<string>();
        public string Message
        {
            get
            {
                return logMessageList.GetStrArray("\r\n");
            }
            set
            {
                NotifyPropertyChanged();
            }
        }
        public string ButtonText { get; set; } = "开启服务";

        private ushort _startAddess = 1;
        public ushort StartAddress
        {
            get { return _startAddess; }
            set
            {
                _startAddess = value;
                SetEveryList();
                NotifyPropertyChanged();
            }
        }

        public int SelectIndex1 { get; set; } = -1;
        public ObservableCollection<MyDataGrid> List1 { get; set; } = new ObservableCollection<MyDataGrid>();
        public DelegateCommand StartCmd { get; set; }
        public DelegateCommand CellClickCmd { get; set; }
        public MC3E服务模拟ViewModel()
        {
            StartCmd = new DelegateCommand(StartCmdFunc);
        }



        private void StartCmdFunc(object obj)
        {
            if (ButtonText == "开启服务")
            {
                server = new MC3EServer(Port);
                server.Start();
                //监听数据变化
                server.DataChangedEvent += DataChanged;
                server.LogChangedEvent += LogDataChanged;
                SetEveryList();
                Enabled1 = false;
                Enabled2 = true;
                ButtonText = "停止服务";
            }
            else
            {
                logMessageList.Clear();
                Message = Guid.NewGuid().ToString();
                server.Stop();
                server = null;
                List1.Clear();
                Enabled1 = true;
                Enabled2 = false;
                ButtonText = "开启服务";
            }
        }

        private void LogDataChanged(string message)
        {

            try
            {
                logMessageList.Insert(0, message);
                while (logMessageList.Count > 50)
                {
                    logMessageList.RemoveAt(logMessageList.Count - 1);
                }
            }
            catch (Exception) { }
            Message = Guid.NewGuid().ToString();


        }



        private void SetEveryList()
        {
            Dispatcher.UIThread.Invoke(() =>
            {
                ushort startAddress = StartAddress;
                List1.Clear();
                for (int i = startAddress; i < 10 + startAddress; i++)
                {
                    MyDataGrid data = new MyDataGrid { Which = 3, Address = i, Value2 = MC3EServer._dataStorage[i] };
                    List1.Add(data);
                }
            });

        }

        private void DataChanged(int address)
        {
            SetEveryList();
        }
    }
}