using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Input;
using Aitex.Core.Common.DeviceData;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.SCCore;
using Aitex.Core.UI.MVVM;
using Aitex.Core.Util;
using Aitex.Core.Utilities;
using Aitex.Triton160.Common;

namespace Aitex.Triton160.UI.ViewModel
{
    public class HardwareConfigViewModel : UIViewModelBase
    {
        [Subscription(TritonDevice.StatisticsRfOnTime, SystemDeviceModule)]
        public AITStatisticsData RfStatistics
        {
            get;
            set;
        }

        [Subscription(TritonDevice.StatisticsPumpOnTime, SystemDeviceModule)]
        public AITStatisticsData PumpStatistics
        {
            get;
            set;
        }

        public ICommand SetConfigCommand
        {
            get;
            private set;
        }

        [IgnorePropertyChange]
        public ICommand ResetCommand
        {
            get;
            set;
        }

        [IgnorePropertyChange]
        public SCValue ConfigFeedBack 
        {
            get;
            set;
        }

        [IgnorePropertyChange]
        public SCValue ConfigSetPoint 
        {
            get;
            set;
        }

        public HardwareConfigViewModel()
            : base("HardwareConfigViewModel")
        {
            SetConfigCommand = new DelegateCommand<object>(SetConfig);
            ResetCommand = new DelegateCommand<object>(Reset);

            ConfigFeedBack = new SCValue();
            ConfigSetPoint = new SCValue();

            ConfigFeedBack.RetrieveAll();
            ConfigSetPoint.RetrieveAll();
        }

        void SetConfig(object param)
        {
            string stringParam = param.ToString();

            string[] values = stringParam.Split(';');
            foreach (var value in values)
            {
                if (string.IsNullOrEmpty(value))
                    continue;

                string[] props = value.Split('.');
                if (props.Length != 3)
                {
                    LOG.Error("Error SetConfig parameter" + param);
                    continue;
                }

                string scName = props[1] + "_" + props[2];
                object scValue = null;

                PropertyInfo[] property = typeof(SCValue).GetProperties();

                foreach (PropertyInfo fiGroup in property)
                {
                    if (fiGroup.Name == props[1])
                    {
                        object objGroup = fiGroup.GetValue(ConfigSetPoint, null);
                        foreach (PropertyInfo fiItem in objGroup.GetType().GetProperties())
                        {
                            if (fiItem.Name == props[2])
                            {
                                scValue = fiItem.GetValue(objGroup, null);
                                Triton160UiSystem.Instance.WCF.InvokeService_DoOperation(TritonOperation.SetConfig.ToString(),  scName, scValue);
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }

        void Reset(object param)
        {
            Triton160UiSystem.Instance.WCF.InvokeService_DoOperation(TritonOperation.DeviceOperation.ToString(), new object[] { param.ToString(), AITStatisticsOperation.Reset.ToString() });
        }

        public void UpdateConfig()
        {
            ConfigFeedBack.Update(Triton160UiSystem.Instance.WCF.Query.PollConfig(ConfigFeedBack.GetKeys()));
            ConfigSetPoint.Update(Triton160UiSystem.Instance.WCF.Query.PollConfig(ConfigSetPoint.GetKeys()));

            InvokeAllPropertyChanged();
        }

        protected override void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
        {
            if (RfStatistics == null)
            {
                string key = SystemDeviceModule + "." + TritonDevice.StatisticsRfOnTime;
                if (data.ContainsKey(key))
                {
                    AITStatisticsData statistics = data[key] as AITStatisticsData;
                    if (statistics != null)
                    {
                        //RfStatisticsPMIntervalSetPoint = statistics.PMInterval.ToString();
                        InvokePropertyChanged("RfStatisticsPMIntervalSetPoint");
                    }
                }
            }

            if (PumpStatistics == null)
            {
                string key = SystemDeviceModule + "." + TritonDevice.StatisticsPumpOnTime;
                if (data.ContainsKey(key))
                {
                    AITStatisticsData statistics = data[key] as AITStatisticsData;
                    if (statistics != null)
                    {
                        //PumpStatisticsPMIntervalSetPoint = statistics.PMInterval.ToString();
                        InvokePropertyChanged("PumpStatisticsPMIntervalSetPoint");
                    }
                }
            }
        }

    }
}