using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using log4net;
using System.Reflection;
using TZONE.Model;
using TZONE.GPSSocketServer.Common;

namespace TZONE.GPSSocketServer.Command.DD
{
    public class DataHandle : CommandBase<GPSSession, BinaryRequestInfo>
    {
        public override string Name
        {
            get
            {
                return "24-24";//$$
            }
        }

        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public override void ExecuteCommand(GPSSession session, BinaryRequestInfo requestInfo)
        {
            try
            {
                string dt = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.FFF");
                string msg = Encoding.Default.GetString(requestInfo.Body, 0, requestInfo.Body.Length).Trim('\0');

                var _chk = msg.IndexOf('|');
                tempdata model = new tempdata();
                //model.data_body = requestInfo.Body;

                model.server_time = dt;
                model.message = msg;
                if (25 < _chk || -1 == _chk)
                {
                    //old
                }
                else
                {
                    //new
                    string[] obj = msg.Split('|');
                    model.imei = obj[0].Substring(4);
                }

                if (IMEIFilter.GetIMEIContains(ref model))
                {
                    if (string.IsNullOrEmpty(model.product_model))
                    {
                        session.Send("$$ NO MODEL");
                        session.Close();
                        log.Error(msg);
                        return;
                    }
                    session.IMEI = model.imei;
                    model.EventTempData += model_EventTempData;
                    model.ExecuteEvent();
                }
                else
                {
                    session.Send("$$ NO IMEI");
                    session.Close();
                    log.Info(msg);
                    return;
                }
                DownData(session, session.IMEI);
            }
            catch (System.Exception ex)
            {
#if DEBUG
                log.Error("ERROR:" + Encoding.Default.GetString(requestInfo.Body, 0, requestInfo.Body.Length).Trim('\0') + " ---- " + BitConverter.ToString(requestInfo.Body, 0, requestInfo.Body.Length).Replace("-", ""));
#endif
                log.Error(ex.Message);
            }
        }

        public void model_EventTempData(tempdata _temp)
        {
            string typename = "TZONE." + _temp.product_model + ".DD.AnalyzeHandle";
            String assemblyName = "TZONE." + _temp.product_model + @".DD.dll";
            Assembly ass = Assembly.LoadFrom(assemblyName);
            Type type = ass.GetType(typename);//必须使用名称空间+类名称
            System.Reflection.MethodInfo method = type.GetMethod("AnalyzeData");//方法的名称
            object obj = ass.CreateInstance(typename);//必须使用名称空间+类名称;
            method.Invoke(obj, new object[] { _temp }); //实例方法的调用

        }

        private void DownData(GPSSession session, string imei)
        {
            if (GlobalVariables.GLOBAL_ON_LINE_DOWN.Contains(imei))
            {
                if (session.TrySend(GlobalVariables.GLOBAL_ON_LINE_DOWN[imei].ToString()))
                {
                    GlobalVariables.GLOBAL_ON_LINE_DOWN.Remove(imei);
                    DownCommand.update_OrderExec_IsExec(session.IMEI, DownCommand.Exec_OK);//指令执行成功
                }
                else
                {
                    DownCommand.update_OrderExec_IsExec(session.IMEI, DownCommand.Exec_FAIL);//指令执行失败
                }
            }
        }
    }
}
