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;

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

            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.IsIOShowNo), 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 response = retData as IODefineCollection;
                if (response == null) {
                    return;
                }
                var list = response.List;
                if (list == null || list.Count() == 0)
                    return;

                update(IODefine.IOTYPE.Input, IStatus, dynArea.IsIOShowNo, response.InCount, list);
                update(IODefine.IOTYPE.Output, OStatus, dynArea.IsIOShowNo, response.OutCount, list);

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

        void update(
            IODefine.IOTYPE ioType,
            ObservableCollection<FlyAdIoStatus> ioStatus,
            bool isShowNo,
            int ioCnt,
            List<IODefine> list) 
        {
            ioStatus.Clear();
            for (int i = 0; i < ioCnt; i++) {
                string number;
                if (isShowNo)
                {
                    if (ioType == IODefine.IOTYPE.Input)
                    {
                        number = $"i{i + 1}";
                    }
                    else 
                    {
                        number = $"o{i + 1}";
                    }
                }
                else {
                    if (ioType == IODefine.IOTYPE.Input)
                    {
                        number = $"X{i}";
                    }
                    else
                    {
                        number = $"Y{i}";
                    }
                }
                ioStatus.Add(new FlyAdIoStatus()
                {
                    Number = number
                });
            }

            //获取中文
            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())
                    {
                        //描述翻译
                        string resourceName = $"str.FlyADIODefine.{ioDefine.Description}";
                        string state_msg = (string)Application.Current.TryFindResource(resourceName);
                        if (state_msg == null)
                        {
                            state_msg = ioDefine.Description;
                        }

                        iostatus_desps[ioDefine.Index] = state_msg;
                    }
                }
            }

            //填入
            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 < 16; i++)
            {
                IStatus.Add(new FlyAdIoStatus()
                {
                    Number = $"X{i}"
                });
            };
            for (int i = 0; i < 8; i++)
            {
                OStatus.Add(new FlyAdIoStatus()
                {
                    Number = $"Y{i}"
                });
            };

            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 = "报警信号";



        }
    }
}
