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

namespace FLY.IntegratedControl.Client
{
    public class ICSystemClient : FObj, IICSystemService, INotifyPropertyChanged
    {
        public BufferServiceClient<FlyData_IBCCtrl> CtrlList;
        #region IICSystemService
        /// <summary>
        /// 数据
        /// </summary>
        public ICData Item { get; } = new ICData();
        /// <summary>
        /// IBC控制历史列表参数
        /// </summary>
        public IBCCtrlListData CtrlListData { get; } = new IBCCtrlListData();

        SyncPropServiceClient syncPropServiceClient;
        /// <summary>
        /// PLC代理系统
        /// </summary>
        public IPLCProxySystemService PLCos { get; private set; }

        #endregion

        public bool IsConnected { get; set; } = false;
    
        IFConn mConn;
        UInt32 mServerID;

        public event PropertyChangedEventHandler PropertyChanged;

        public ICSystemClient()
        {
            mServerID = OBJ_INTERFACE.OBJ_INTERFACE.IBC_OBJ_ID;

            syncPropServiceClient = new SyncPropServiceClient(
                mServerID + 1,
                new Dictionary<string, INotifyPropertyChanged> {
                    {"Item", Item },
                    {"CtrlListData", CtrlListData }
                });

            PLCos = new PLCProxySystemServiceClient(
                OBJ_INTERFACE.OBJ_INTERFACE.IBC_OBJ_PLCOS_ID,
                new Dictionary<string, INotifyPropertyChanged> {
                    { "Item", Item }
                });


            CtrlList = new BufferServiceClient<FlyData_IBCCtrl>(OBJ_INTERFACE.OBJ_INTERFACE.IBC_OBJ_CTRLLIST_ID);
        }
        public UInt32[] GetIDs()
        {
            List<UInt32> IDs = new List<uint>();
            IDs.Add(ID);
            IDs.Add(syncPropServiceClient.ID);
            IDs.AddRange(((PLCProxySystemServiceClient)PLCos).GetIDs());
            IDs.Add(CtrlList.ID);
            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);
            }
        }

        

    }
}