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

namespace FLY.OBJComponents.Client
{
    public class PLCProxySystemServiceClient : FObj, IPLCProxySystemService, IPropertyOpt
    {
        public Dictionary<string, INotifyPropertyChanged> ObjNames { get; private set; }

        public bool IsConnected { get; private set; } = false;

        SyncPropServiceClient syncPropServiceClient;
        /// <summary>
        /// 与PLC连接成功
        /// </summary>
        public bool IsConnectedWithPLC { get; set; } = false;

        IFConn mConn;
        UInt32 mServerID;

        public event PropertyChangedEventHandler PropertyChanged;

        public PLCProxySystemServiceClient(UInt32 serverID, Dictionary<string, INotifyPropertyChanged> objnames)
        {
            mServerID = serverID;
            ObjNames = objnames;

            syncPropServiceClient = new SyncPropServiceClient(
                serverID + 1,
                new Dictionary<string, INotifyPropertyChanged> {
                    {".", this }
                });
        }
        public UInt32[] GetIDs()
        {
            return new UInt32[] { ID,
                syncPropServiceClient.ID
            };
        }


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

            IsConnected = from.IsConnected;
            if (from.IsConnected)
            {

                CurrObjSys.SenseConfigEx(mConn, mServerID, ID,
                    0xffffffff, SENSE_CONFIG.ADD);
            }
        }
             

        #region IPLCProxySystemService
        public void FeedPlan(long planID)
        {
            PLCOS_OBJ_INTERFACE.Pack_FeedPlanRequest pack = new PLCOS_OBJ_INTERFACE.Pack_FeedPlanRequest()
            {
                planid = planID
            };
            string json = JsonConvert.SerializeObject(pack);

            FObjSys.Current.CallFunctionEx(mConn, mServerID, ID,
                PLCOS_OBJ_INTERFACE.CALL_FEED_PLAN, Misc.Converter.StringToBytes(json));
        }

        public void SetPlan(string objname, IEnumerable<string> propertynames, long planID)
        {
            PLCOS_OBJ_INTERFACE.Pack_SetPlanRequest pack = new PLCOS_OBJ_INTERFACE.Pack_SetPlanRequest()
            {
                objname = objname,
                propertyNames = propertynames,
                planid = planID
            };
            string json = JsonConvert.SerializeObject(pack);

            FObjSys.Current.CallFunctionEx(mConn, mServerID, ID,
                PLCOS_OBJ_INTERFACE.CALL_SET_PLAN, Misc.Converter.StringToBytes(json));
        }


        #endregion

        public string[] GetSyncPropNames()
        {
            return new string[] {
                "IsConnectedWithPLC"
            };
        }

        public string[] GetNoSyncPropNames()
        {
            return null;
        }

    }
}
