using FLY.Thick.Base.Common;
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.DynArea);
            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, DynArea dynArea)
        {
            this.dynArea = dynArea;
            this.iODefineService = iODefineService;

            for (int i = 0; i < 12; i++) {
                IStatus.Add(new FlyAdIoStatus()
                {
                    Number = $"i{i+1}"
                }); 
            };
            for (int i = 0; i < 4; 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), updateIsOn_In);
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.OStatus), updateIsOn_Out);

        }

        void updateIsOn_In() 
        {
            for (int i = 0; i < IStatus.Count(); i++)
            {
                IStatus[i].IsOn = !Misc.MyBase.CHECKBIT(dynArea.IStatus, i);
            }
        }
        void updateIsOn_Out()
        {
            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 IODefineCollection;
                if (reponse == null) {
                    return;
                }
                var list = reponse.List;
                if (list == null || list.Count() == 0)
                    return;

                update(IODefine.IOTYPE.Input, IStatus, reponse.InCount, list);
                update(IODefine.IOTYPE.Output, OStatus, reponse.OutCount, list);

                updateIsOn_In();
                updateIsOn_Out();
            }, this);
        }

        void update(
            IODefine.IOTYPE ioType,
            ObservableCollection<FlyAdIoStatus> ioStatus,
            int ioCnt,
            List<IODefine> list) 
        {
            //补够数量
            if (ioStatus.Count() != ioCnt && ioCnt > 0)
            {
                while (ioStatus.Count() > ioCnt)
                {
                    ioStatus.RemoveAt(ioStatus.Count() - 1);
                }
                while (ioStatus.Count() < ioCnt)
                {
                    int i = ioStatus.Count();
                    ioStatus.Add(new FlyAdIoStatus()
                    {
                        Number = $"i{i + 1}"
                    });
                }
            }

            //获取中文
            string[] iostatus_desps = new string[ioCnt];

            for (int i = 0; i < list.Count(); i++)
            {
                var ioDefine = list[i];
                if (ioDefine.IoType == ioType)
                {
                    if (ioDefine.Index < iostatus_desps.Count())
                    {
                        iostatus_desps[ioDefine.Index] = ioDefine.Description;
                    }
                }
            }

            //填入
            for (int i = 0; i < iostatus_desps.Count(); i++)
            {
                if (string.IsNullOrEmpty(iostatus_desps[i]))
                {
                    ioStatus[i].Desp = "";
                }
                else
                {
                    ioStatus[i].Desp = iostatus_desps[i];
                }
            }
        }
    }
    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 : WdIoTipVm
    {
        public WdIoTipVmUt()
        {

            for (int i = 0; i < 12; i++)
            {
                IStatus.Add(new FlyAdIoStatus()
                {
                    Number = $"i{i + 1}"
                });
            };
            for (int i = 0; i < 4; 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 = "报警信号";



        }
    }
}
