using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Thick.BlowingScan.OBJ_INTERFACE;
using FLY.Thick.BlowingScan.Server;
using FLY.Thick.BlowingScan.IService;
using FLY.Thick.BlowingScan.Common;
using FLY.Thick.Blowing.Server.OBJProxy;
using Newtonsoft.Json;

namespace FLY.Thick.BlowingScan.Server.OBJProxy
{
    public class BlowingScan_OBJProxy : Blowing_OBJProxy
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMSEXT = 2;
        const int MARKNO_PUSH_STATUS = 3;
        const int MARKNO_PUSH_BUFINFO = 4;
        #endregion


        IBlowingScanService mBlowing;



        public BlowingScan_OBJProxy(int objsys_idx,UInt32 id, IBlowingScanService blowing) :base(objsys_idx,id, blowing)
        {
            mBlowing = blowing;
            mBlowing.PropertyChanged += mBlowing_PropertyChanged;
        }

        void mBlowing_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (
                (e.PropertyName == "SolveCnt") ||
                (e.PropertyName == "SensorWidth")||
                (e.PropertyName == "Smooth")
                )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate ()
                    {
                        byte[] buf;
                        GetValue(null, 0, BLOWING_SCAN_OBJ_INTERFACE.GET_PARAMSEXT, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, BLOWING_SCAN_OBJ_INTERFACE.PUSH_PARAMSEXT,
                            buf);
                    }), this, MARKNO_PUSH_PARAMSEXT);
            }
            else if (
                (e.PropertyName == "FirstBM") ||
                (e.PropertyName == "LastBM")
                )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate ()
                    {
                        byte[] buf;
                        GetValue(null, 0, BLOWING_SCAN_OBJ_INTERFACE.GET_BUFINFO, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, BLOWING_SCAN_OBJ_INTERFACE.PUSH_BUFINFO,
                            buf);
                    }), this, MARKNO_PUSH_BUFINFO);
            }
        }
        
        public override void CallFunction(IFConn from, uint srcid,UInt32 magic, ushort funcid, byte[] infodata)
        {
            switch (funcid) 
            {
                case BLOWING_SCAN_OBJ_INTERFACE.CALL_GETBUF:
                    {
                        mBlowing.GetBufList(
                            new AsyncCBHandler(delegate (object AsyncState, object retdata)
                            {
                                ConnContext context = (ConnContext)AsyncState;
                                GetBufListReponse p = (GetBufListReponse)retdata;
                                string json = JsonConvert.SerializeObject(p);
                                CurrObjSys.PushCallFunctionEx(
                                    context.from,
                                    context.srcid,
                                    ID,
                                    context.magic,
                                    BLOWING_SCAN_OBJ_INTERFACE.CALL_GETBUF,
                                    Misc.Converter.StringToBytes(json));
                            }), new ConnContext(from, srcid, magic));
                    } break;
            }
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            base.GetValue(from, srcid, memid, out infodata);
            if (infodata != null)
                return;

            switch (memid) 
            {
                case BLOWING_SCAN_OBJ_INTERFACE.GET_PARAMSEXT:
                    {
                        BLOWING_SCAN_OBJ_INTERFACE.Pack_ParamsExt p = new BLOWING_SCAN_OBJ_INTERFACE.Pack_ParamsExt(){
                            solveCnt = mBlowing.SolveCnt,
                            sensorWidth = mBlowing.SensorWidth,
                            smooth = mBlowing.Smooth
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }break;
                    
                case BLOWING_SCAN_OBJ_INTERFACE.GET_BUFINFO:
                    {
                        BLOWING_SCAN_OBJ_INTERFACE.Pack_BM p = new BLOWING_SCAN_OBJ_INTERFACE.Pack_BM()
                        {
                            firstbm = mBlowing.FirstBM,
                            lastbm = mBlowing.LastBM
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                default:
                    infodata = null;
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            base.SetValue(from, srcid, memid, infodata);
            switch (memid)
            {
                case BLOWING_SCAN_OBJ_INTERFACE.SET_PARAMSEXT:
                    {

                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<BLOWING_SCAN_OBJ_INTERFACE.Pack_ParamsExt>(json);

                        mBlowing.SolveCnt = p.solveCnt;
                        mBlowing.SensorWidth = p.sensorWidth;
                        mBlowing.Smooth = p.smooth;
                        mBlowing.Apply();
                        
                    } break;
            }         
        }
    }

}