using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Net;

using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Client
{
    public class ProfileServiceClient : FObj, IProfileService
    {
        IFConn mConn;
        UInt32 mServerID;

        public ProfileServiceClient(UInt32 serverid)
        {
            mServerID = serverid;
        }

        #region IProfileService  接口
        string pname = "test_pname";
        public string PName
        {
            set
            {
                if (pname != value)
                {
                    pname = value;
                    NotifyPropertyChanged("PName");
                }
            }
            get { return pname; }
        }
        int target=3000;
        public int Target
        {
            get { return target; }
            set
            {
                if (target != value)
                {
                    target = value;
                    NotifyPropertyChanged("Target");
                }
            }
        }
        int alarm=100;
        public int Alarm
        {
            get { return alarm; }
            set
            {
                if (alarm != value)
                {
                    alarm = value;
                    NotifyPropertyChanged("Alarm");
                }
            }
        }

        double comp=1;
        public double Comp
        {
            get { return comp; }
            set
            {
                comp = value;
                NotifyPropertyChanged("Comp");
            }
        }

        int shift=0;
        public int Shift
        {
            get { return shift; }
            set
            {
                if (shift != value)
                {
                    shift = value;
                    NotifyPropertyChanged("Shift");
                }
            }
        }

        int beginNo=0;
        public int BeginNo
        {
            get { return beginNo; }
            set
            {
                if (beginNo != value)
                {
                    beginNo = value;
                    NotifyPropertyChanged("BeginNo");
                }
            }
        }

        int endNo=40;
        public int EndNo
        {
            get { return endNo; }
            set
            {
                if (endNo != value)
                {
                    endNo = value;
                    NotifyPropertyChanged("EndNo");
                }
            }
        }

        int dataBeginNo=2;
        public int DataBeginNo
        {
            get { return dataBeginNo; }
            set
            {
                if (dataBeginNo != value)
                {
                    dataBeginNo = value;
                    NotifyPropertyChanged("DataBeginNo");
                }
            }
        }

        int dataEndNo = 30;
        public int DataEndNo
        {
            get { return dataEndNo; }
            set
            {
                if (dataEndNo != value)
                {
                    dataEndNo = value;
                    NotifyPropertyChanged("DataEndNo");
                }
            }
        }


        /// <summary>
        /// 应用 &amp; 保存
        /// </summary>
        public void Apply() 
        {
            ProfileParam p = new ProfileParam()
            {
                pname = PName,
                target = Target,
                alarm = Alarm,
                comp = Comp,
                shift = Shift,
                beginNo = BeginNo,
                endNo = EndNo,
                dataBeginNo = DataBeginNo,
                dataEndNo = DataEndNo
            };
            CurrObjSys.SetValueEx(mConn, mServerID, ID,
                PROFILE_OBJ_INTERFACE.SET_PARAMS,
                p.ToBytes());
        }



        /// <summary>
        /// 获取产品列表, 返回类型为 List&lt;string&gt;
        /// </summary>
        /// <param name="AsyncDelegate"></param>
        /// <param name="AsyncState"></param>
        public void GetList(AsyncCBHandler AsyncDelegate, object AsyncState) 
        {
            CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
                PROFILE_OBJ_INTERFACE.CALL_GETLIST, 
                null, 
                AsyncDelegate, AsyncState);
        }

        /// <summary>
        /// 删除指定产品
        /// </summary>
        /// <param name="productname"></param>
        public void Del(string productname) 
        {
            PROFILE_OBJ_INTERFACE.Pack_String p = new PROFILE_OBJ_INTERFACE.Pack_String();
            p.data = productname;
            CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
                PROFILE_OBJ_INTERFACE.CALL_DEL, p.ToBytes());
        }

        /// <summary>
        /// 读取指定产品,返回类型为 ProfileParam
        /// </summary>
        /// <param name="productname"></param>
        /// <param name="AsyncDelegate"></param>
        /// <param name="AsyncState"></param>
        public void Read(string productname, AsyncCBHandler AsyncDelegate, object AsyncState) 
        {
            CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
                PROFILE_OBJ_INTERFACE.CALL_READ, 
                new PROFILE_OBJ_INTERFACE.Pack_String() { data = productname }.ToBytes(), 
                AsyncDelegate, AsyncState);
        }
        #endregion



        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            if (from.IsConnected)
            {
                //获取所有数据，设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    PROFILE_OBJ_INTERFACE.GET_PARAMS);

                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID,
                    0xffffffff,
                    SENSE_CONFIG.ADD);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case PROFILE_OBJ_INTERFACE.GET_PARAMS:
                    {
                        ProfileParam p = new ProfileParam();

                        if (!p.TryParse(infodata))
                            return;
                        PName = p.pname;
                        Target = p.target;
                        Alarm = p.alarm;
                        Comp = p.comp;
                        Shift = p.shift;
                        BeginNo = p.beginNo;
                        EndNo = p.endNo;
                        DataBeginNo = p.dataBeginNo;
                        DataEndNo = p.dataEndNo;

                    } break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {

            PushGetValue(from, srcid, infoid, infodata);


        }
        public override void PushCallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] retdata, AsyncCBHandler asyncDelegate, object asyncContext)
        {
            switch (funcid)
            {
                case PROFILE_OBJ_INTERFACE.CALL_GETLIST:
                    {
                        PROFILE_OBJ_INTERFACE.Pack_StringList p = new PROFILE_OBJ_INTERFACE.Pack_StringList();
                        if (!p.TryParse(retdata))
                            return;
                        asyncDelegate(asyncContext, p.list);
                    } break;
                case PROFILE_OBJ_INTERFACE.CALL_READ:
                    {
                        ProfileParam p = new ProfileParam();
                        if (!p.TryParse(retdata))
                            return;
                        asyncDelegate(asyncContext, p);
                    } break;
            }
        }

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion
    }
}