using FLY.OBJComponents.Client;
using FLY.OBJComponents.IService;
using FLY.Winder.Common;
using FLY.Winder.IService;
using FLY.Winder.OBJ_INTERFACE;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace FLY.Winder.Client
{
    public class WinderSystemServiceClient : FObj, IWinderSystemService, INotifyPropertyChanged
    {
        #region IIBCSystemService
        public WinderAccessory Accessory { get; private set; }

        public List<WinderInsideOutside> Items { get; private set; }

        /// <summary>
        /// PLC代理系统
        /// </summary>
        public IPLCProxySystemService PLCos { get; private set; }
        #endregion
        public bool IsConnected { get; set; } = false;

        IFConn mConn;
        UInt32 mServerID;

        public event PropertyChangedEventHandler PropertyChanged;
        SyncPropServiceClient syncPropServiceClient;
        
        /// <summary>
        /// 当前 INotifyPropertyChanged 对象 引发了 PropertyChanged,  是由于 接收到数据导致的
        /// </summary>
        public bool IsInPushValue {
            get
            {
                return syncPropServiceClient.IsInPushValue;
            }
        }
        public WinderSystemServiceClient()
        {
            mServerID = OBJ_INTERFACE.OBJ_INTERFACE.WINDER_OBJ_ID;

            Items = new List<WinderInsideOutside>();
            for (int i = 0; i < 2; i++)
            {
                Items.Add(new WinderInsideOutside());
            }
            Accessory = new WinderAccessory();

            
            syncPropServiceClient = new SyncPropServiceClient(
                mServerID + 1, 
                new Dictionary<string, INotifyPropertyChanged>
                {
                    {"Accessory", Accessory },
                    {"Items[0]", Items[0] },
                    {"Items[1]", Items[1] }
                });
            

            PLCos = new PLCProxySystemServiceClient(
                OBJ_INTERFACE.OBJ_INTERFACE.WINDER_OBJ_PLCOS_ID,
                new Dictionary<string, INotifyPropertyChanged> 
                {
                    {"Accessory", Accessory },
                    {"Items[0]", Items[0] },
                    {"Items[1]", Items[1] }
                });
        }
        public UInt32[] GetIDs()
        {
            List<UInt32> IDs = new List<uint>();
            IDs.Add(ID);
            IDs.Add(syncPropServiceClient.ID);
            IDs.AddRange(((PLCProxySystemServiceClient)PLCos).GetIDs());

            return IDs.ToArray();
        }

        public override void ConnectNotify(IFConn from)
        {
            mConn = from;

            IsConnected = from.IsConnected;
            if (from.IsConnected)
            {
                CurrObjSys.SenseConfigEx(mConn, mServerID, ID,
                    0xffffffff, SENSE_CONFIG.ADD);
            }
        }
    }
}