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

namespace FLY.OBJComponents.Server.OBJProxy
{

    public class PLCProxySystem_OBJProxy : FObj
    {
        IPLCProxySystemService plcos;
        SyncProp_OBJProxy syncProp_OBJProxy;
        /// <summary>
        /// 内部会使用 serverID,serverID+1
        /// </summary>
        /// <param name="objsys_idx"></param>
        /// <param name="serverID"></param>
        /// <param name="plcos"></param>
        public PLCProxySystem_OBJProxy(int objsys_idx, UInt32 serverID, IPLCProxySystemService plcos) : base(objsys_idx)
        {
            ID = serverID;
            this.plcos = plcos;

            syncProp_OBJProxy = new SyncProp_OBJProxy(
                objsys_idx, serverID + 1,
                new Dictionary<string, System.ComponentModel.INotifyPropertyChanged>
                {
                    { ".", plcos }
                });
        }
        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case PLCOS_OBJ_INTERFACE.CALL_SET_PLAN:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        PLCOS_OBJ_INTERFACE.Pack_SetPlanRequest pack = JsonConvert.DeserializeObject<PLCOS_OBJ_INTERFACE.Pack_SetPlanRequest>(json);
                        plcos.SetPlan(pack.objname, pack.propertyNames, pack.planid);
                    }
                    break;
                case PLCOS_OBJ_INTERFACE.CALL_SET_PLAN2:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        PLCOS_OBJ_INTERFACE.Pack_SetPlan2Request pack = JsonConvert.DeserializeObject<PLCOS_OBJ_INTERFACE.Pack_SetPlan2Request>(json);
                        plcos.SetPlan(pack.objname, pack.propertyNames, (planid, context) =>
                        {
                            ConnContext conn = context as ConnContext;
                            PLCOS_OBJ_INTERFACE.Pack_SetPlan2Reponse p = new PLCOS_OBJ_INTERFACE.Pack_SetPlan2Reponse() { planid = planid };
                            string s = JsonConvert.SerializeObject(p);
                            CurrObjSys.PushCallFunctionEx(conn.from, srcid, ID, magic, funcid, Misc.Converter.StringToBytes(s));

                        }, new ConnContext(from, srcid, magic));
                    }
                    break;
                case PLCOS_OBJ_INTERFACE.CALL_FEED_PLAN:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        PLCOS_OBJ_INTERFACE.Pack_FeedPlanRequest pack = JsonConvert.DeserializeObject<PLCOS_OBJ_INTERFACE.Pack_FeedPlanRequest>(json);
                        plcos.FeedPlan(pack.planid);
                    }
                    break;
                case PLCOS_OBJ_INTERFACE.CALL_REMOVE_PLAN:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        PLCOS_OBJ_INTERFACE.Pack_FeedPlanRequest pack = JsonConvert.DeserializeObject<PLCOS_OBJ_INTERFACE.Pack_FeedPlanRequest>(json);
                        plcos.RemovePlan(pack.planid);
                    }
                    break;
            }
        }
    }
}