using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Aitex.Core.RT.IOCore;
using Aitex.Core.RT.SCCore;
using Aitex.Core.UI.MVVM;
using MECF.Framework.Common.IOCore;

namespace Venus_RT.Backends
{
    /// <summary>
    /// IOView.xaml 的交互逻辑
    /// </summary>
    public partial class IOView : UserControl
    {
        public IOView()
        {
            InitializeComponent();

            DataContext = new IOMonitorViewModel();
 
            this.IsVisibleChanged += IOView_IsVisibleChanged;
        }

        private void IOView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
 
            (DataContext as TimerViewModelBase).EnableTimer(IsVisible);
        }
 
    }

    public class IoValues
    {
        public ObservableCollection<NotifiableIoItem> DiItemList { get; set; }
        public ObservableCollection<NotifiableIoItem> DoItemList { get; set; }
        public ObservableCollection<NotifiableIoItem> AiItemList { get; set; }
        public ObservableCollection<NotifiableIoItem> AoItemList { get; set; }

        #region InitDefine
        public IoValues(string source)
        {
            DiItemList = new ObservableCollection<NotifiableIoItem>();

            List<DIAccessor> diItems = IO.GetDiList(source);
            if (diItems != null)
            {
                foreach (var diItem in diItems)
                {
                    NotifiableIoItem item = new NotifiableIoItem()
                    {
                        Name = diItem.Name,
                        Index = diItem.Index,
                        Description = diItem.Description,
                        BoolValue = diItem.Value,
                        Address = diItem.Addr,
                    };
                    DiItemList.Add(item);
                }
            }

            DoItemList = new ObservableCollection<NotifiableIoItem>();

            List<DOAccessor> doItems = IO.GetDoList(source);
            if (doItems != null)
            {
                foreach (var doItem in doItems)
                {
                    NotifiableIoItem item = new NotifiableIoItem()
                    {
                        Name = doItem.Name,
                        Index = doItem.Index,
                        Description = doItem.Description,
                        BoolValue = doItem.Value,
                        Address = doItem.Addr,
                    };
                    DoItemList.Add(item);
                }
            }

            AiItemList = new ObservableCollection<NotifiableIoItem>();

            List<AIAccessor> aiItems = IO.GetAiList(source);
            if (aiItems != null)
            {
                foreach (var aiItem in aiItems)
                {
                    NotifiableIoItem item = new NotifiableIoItem()
                    {
                        Name = aiItem.Name,
                        Index = aiItem.Index,
                        Description = aiItem.Description,
                        ShortValue = aiItem.Value,
                        Address = aiItem.Addr,
                    };
                    AiItemList.Add(item);
                }
            }


            AoItemList = new ObservableCollection<NotifiableIoItem>();

            List<AOAccessor> aoItems = IO.GetAoList(source);
            if (aoItems != null)
            {
                foreach (var aoItem in aoItems)
                {
                    NotifiableIoItem item = new NotifiableIoItem()
                    {
                        Name = aoItem.Name,
                        Index = aoItem.Index,
                        Description = aoItem.Description,
                        ShortValue = aoItem.Value,
                        Address = aoItem.Addr,
                        CanEdit = SC.GetValue<bool>("System.IsOpenIOEdit"),
                    };
                    AoItemList.Add(item);
                }
            }
        }

        #endregion


        public void UpdateData()
        {
            bool canEdit = SC.GetValue<bool>("System.IsOpenIOEdit");
            foreach (var item in DiItemList)
            {
                item.BoolValue = IO.DI[item.Name].Value;
                item.InvokePropertyChanged("BoolValue");
            }
            foreach (var item in DoItemList)
            {
                item.BoolValue = IO.DO[item.Name].Value;
                item.InvokePropertyChanged("BoolValue");
            }
            foreach (var item in AiItemList)
            {
                item.ShortValue = IO.AI[item.Name].Value;
                item.InvokePropertyChanged("ShortValue");
            }
            foreach (var item in AoItemList)
            {
                item.ShortValue = IO.AO[item.Name].Value;
                item.InvokePropertyChanged("ShortValue");
                item.CanEdit = canEdit;
                item.InvokePropertyChanged("CanEdit");
            }
        }
    }

    public class IOMonitorViewModel : TimerViewModelBase
    {

        public IoValues[] IoList { get; set; }
 
 
        public IOMonitorViewModel():base(nameof(IOMonitorViewModel))
        {
            //IoList = new IoValues[2];
            //for (int i = 0; i < IoList.Length; i++)
            //{
            //    IoList[i] = new IoValues("System.io"+(i+1));
            //}
            IoList = new IoValues[2];
            List<string> strs = new List<string>();
            strs.Add("PMA");
            strs.Add("PMB");
            for (int i = 0; i < IoList.Length; i++)
            {
                //IoList[i] = new IoValues("System.io"+(i+1));
                var t = new IoValues($"{strs[i]}.PLC");
                IoList[i] = t;
            }
        }

        protected override void Poll()
        {
            for (int i = 0; i < IoList.Length; i++)
            {
                IoList[i].UpdateData();
            }
            //string[] values = new string[128];
            //int i = 0;
            //foreach (var notifiableIoItem in Card1.DiItemList)
            //{
            //    notifiableIoItem.BoolValue = IO.DI[notifiableIoItem.Name].Value;
            //    notifiableIoItem.InvokePropertyChanged(nameof(notifiableIoItem.BoolValue));

            //    values[i++] = notifiableIoItem.BoolValue ? "1" : "0";
            //}

            //System.Diagnostics.Trace.WriteLine(string.Join(",", values));
        }
    }

}