using FLY.Thick.Base.IService;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
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 Unity;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// WdIOTip.xaml 的交互逻辑
    /// </summary>
    public partial class WdIOTip : FLY.ControlLibrary.WindowBigClose   
    {
        WdIoTipVm viewModel;

        public WdIOTip()
        {
            InitializeComponent();
        }
        
        [InjectionMethod]
        public void Init(IFlyAdIoDefineService iODefineService, ITDGageService gageService) {

            viewModel = new WdIoTipVm();
            viewModel.Init(iODefineService, gageService);
            this.DataContext = viewModel;
        }

    }

    public class WdIoTipVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        IFlyAdIoDefineService iODefineService;

        Base.Common.DynArea dynArea;
        public ObservableCollection<FlyAdIoStatus> IStatus { get; } = new ObservableCollection<FlyAdIoStatus>();
        public ObservableCollection<FlyAdIoStatus> OStatus { get; } = new ObservableCollection<FlyAdIoStatus>();

        public WdIoTipVm() { 
        
        }

        public void Init(IFlyAdIoDefineService iODefineService, ITDGageService gageService)
        {
            this.dynArea = gageService.DynArea;
            this.iODefineService = iODefineService;

            for (int i = 0; i < 16; i++) {
                IStatus.Add(new FlyAdIoStatus()
                {
                    Number = $"i{i+1}"
                }); 
            };
            for (int i = 0; i < 8; i++)
            {
                OStatus.Add(new FlyAdIoStatus()
                {
                    Number = $"o{i+1}"
                });
            };

            if (this.iODefineService is FObjBase.FObjServiceClient)
            {
                var client = this.iODefineService as FObjBase.FObjServiceClient;
                if (client.IsConnected)
                {
                    update();
                }
                client.PropertyChanged += Client_PropertyChanged;
            }
            else
            {
                update();
            }

            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.IStatus), () =>
            {
                for (int i = 0; i < IStatus.Count(); i++) {
                    IStatus[i].IsOn = !Misc.MyBase.CHECKBIT(dynArea.IStatus, i);
                }
            });
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.OStatus), () =>
            {
                for (int i = 0; i < OStatus.Count(); i++)
                {
                    OStatus[i].IsOn = !Misc.MyBase.CHECKBIT(dynArea.OStatus, i);
                }
            });

        }


        private void Client_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var client = this.iODefineService as FObjBase.FObjServiceClient;
            if (e.PropertyName == nameof(FObjBase.FObjServiceClient.IsConnected))
            {
                if (client.IsConnected)
                {
                    update();
                }
            }
        }

        void update()
        {
            iODefineService.GetIODefine((asyncContext, retData) => {

                var reponse = retData as List<IODefine>;
                if (reponse == null || reponse.Count() == 0)
                    return;
                string[] istatus_desps = new string[IStatus.Count()];
                string[] ostatus_desps = new string[OStatus.Count()];
                for (int i = 0; i < reponse.Count(); i++) {
                    var ioDefine = reponse[i];
                    if (ioDefine.IoType == IODefine.IOTYPE.Input)
                    {
                        if (ioDefine.Index < istatus_desps.Count()) {
                            istatus_desps[ioDefine.Index] = ioDefine.Description;
                        }
                    }
                    else {
                        if (ioDefine.Index < ostatus_desps.Count())
                        {
                            ostatus_desps[ioDefine.Index] = ioDefine.Description;
                        }
                    }
                }

                for (int i = 0; i < istatus_desps.Count(); i++) {
                    if (string.IsNullOrEmpty(istatus_desps[i]))
                    {
                        IStatus[i].Desp = "";
                    }
                    else
                    {
                        IStatus[i].Desp = istatus_desps[i];
                    }
                }

                for (int i = 0; i < ostatus_desps.Count(); i++)
                {
                    if (string.IsNullOrEmpty(ostatus_desps[i]))
                    {
                        OStatus[i].Desp = "";
                    }
                    else
                    {
                        OStatus[i].Desp = ostatus_desps[i];
                    }
                }
            }, this);
        }


    }
    public class FlyAdIoStatus : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 编号
        /// </summary>
        public string Number { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Desp { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public bool IsOn { get; set; }

    }

    public class WdIoTipVmUt : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;


        public ObservableCollection<FlyAdIoStatus> IStatus { get; } = new ObservableCollection<FlyAdIoStatus>();
        public ObservableCollection<FlyAdIoStatus> OStatus { get; } = new ObservableCollection<FlyAdIoStatus>();

        public WdIoTipVmUt()
        {

            for (int i = 0; i < 16; i++)
            {
                IStatus.Add(new FlyAdIoStatus()
                {
                    Number = $"i{i + 1}"
                });
            };
            for (int i = 0; i < 8; i++)
            {
                OStatus.Add(new FlyAdIoStatus()
                {
                    Number = $"o{i + 1}"
                });
            };

            IStatus[2-1].IsOn = true;
            IStatus[4-1].IsOn = true;

            IStatus[11 - 1].IsOn = true;

            OStatus[4 - 1].IsOn = true;


            IStatus[2 - 1].Desp = "归零信号";
            IStatus[3 - 1].Desp = "正向限位";
            IStatus[4 - 1].Desp = "反向限位";
            IStatus[7 - 1].Desp = "急停 & 手动正转";
            IStatus[8 - 1].Desp = "急停 & 手动反转";

            OStatus[4 - 1].Desp = "报警信号";



        }
    }
}
