using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;

namespace FLY.Thick.Base.Server.OBJProxy
{
    public class Profile_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        #endregion

        IProfileService mProfile;

        public Profile_OBJProxy(int objsys_idx,UInt32 id, IProfileService profile)
            : base(objsys_idx)
        {
            ID = id;
            mProfile = profile;
            mProfile.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mProfile_PropertyChanged);
        }

        void mProfile_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "PName") ||
                (e.PropertyName == "Target") ||
                (e.PropertyName == "Alarm") ||
                (e.PropertyName == "Comp") ||
                (e.PropertyName == "Shift") ||
                (e.PropertyName == "BeginNo") ||
                (e.PropertyName == "EndNo") ||
                (e.PropertyName == "DataBeginNo") ||
                (e.PropertyName == "DataEndNo"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, ID, PROFILE_OBJ_INTERFACE.GET_PARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, PROFILE_OBJ_INTERFACE.PUSH_PARAMS,
                            buf);
                    }), this, MARKNO_PUSH_PARAMS);
            }
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case PROFILE_OBJ_INTERFACE.GET_PARAMS:
                    {
                        ProfileParam p = new ProfileParam()
                        {
                            pname = mProfile.PName,
                            target = mProfile.Target,
                            alarm = mProfile.Alarm,
                            comp = mProfile.Comp,
                            shift = mProfile.Shift,
                            beginNo = mProfile.BeginNo,
                            endNo = mProfile.EndNo,
                            dataBeginNo = mProfile.DataBeginNo,
                            dataEndNo = mProfile.DataEndNo
                        };
                        infodata = p.ToBytes();
                    } break;
                default:
                    infodata = null;
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case PROFILE_OBJ_INTERFACE.SET_PARAMS:
                    {
                        ProfileParam p = new ProfileParam();
                        if (!p.TryParse(infodata))
                            break;
                        mProfile.PName = p.pname;
                        mProfile.Target = p.target;
                        mProfile.Alarm = p.alarm;
                        mProfile.Comp = p.comp;
                        mProfile.Shift = p.shift;
                        mProfile.BeginNo = p.beginNo;
                        mProfile.EndNo = p.endNo;
                        mProfile.DataBeginNo = p.dataBeginNo;
                        mProfile.DataEndNo = p.dataEndNo;
                        mProfile.Apply();
                    } break;
            }
        }
        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid) 
            {
                case PROFILE_OBJ_INTERFACE.CALL_GETLIST:
                    {
                        mProfile.GetList(
                            new AsyncCBHandler(delegate(object AsyncState, object retdata)
                            {
                                ConnContext context = (ConnContext)AsyncState;
                                List<string> ss = (List<string>)retdata;
                                PROFILE_OBJ_INTERFACE.Pack_StringList p = new PROFILE_OBJ_INTERFACE.Pack_StringList();
                                p.list = (List<string>)retdata; 

                                CurrObjSys.PushCallFunctionEx(
                                    context.from,
                                    context.srcid,
                                    ID,
                                    context.magic,
                                    PROFILE_OBJ_INTERFACE.CALL_GETLIST,
                                    p.ToBytes());
                            }), new ConnContext(from, srcid, magic));
                    }break;
                case PROFILE_OBJ_INTERFACE.CALL_DEL:
                    { 
                        PROFILE_OBJ_INTERFACE.Pack_String p = new PROFILE_OBJ_INTERFACE.Pack_String();
                        if(!p.TryParse(infodata))
                            return;

                        mProfile.Del(p.data);
                    }break;
                case PROFILE_OBJ_INTERFACE.CALL_READ:
                    {
                        PROFILE_OBJ_INTERFACE.Pack_String p = new PROFILE_OBJ_INTERFACE.Pack_String();
                        if (!p.TryParse(infodata))
                            return;
                        mProfile.Read(
                            p.data,
                            new AsyncCBHandler(delegate(object AsyncState, object retdata)
                            {
                                ConnContext context = (ConnContext)AsyncState;

                                CurrObjSys.PushCallFunctionEx(
                                    context.from,
                                    context.srcid,
                                    ID,
                                    context.magic,
                                    PROFILE_OBJ_INTERFACE.CALL_READ,
                                    ((ProfileParam)retdata).ToBytes());
                            }), new ConnContext(from, srcid, magic));
                    }break;
            }
        }
    }
}
