using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TZONE.Model;
using System.ServiceModel;
using log4net;
using System.Reflection;

namespace TZONE.GPSSocketServer.Common
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class IMEIFilter : TZONE.Contracts.IDown
    {
        static ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// 判断IMEI是否有效
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool GetIMEIContains(ref tempdata model)
        {
            if (GlobalVariables.GLOBAL_ON_LINE_IMEI.Contains(model.imei))
            {
                string[] _v = GlobalVariables.GLOBAL_ON_LINE_IMEI[model.imei].ToString().Split('|');
                model.product_model = _v[1];
                model.PID = 0; //Convert.ToInt32(_v[0]);
                model.type_id = Convert.ToInt32(_v[2]);
                return true;
            }
            else
            {
                return ResetIMEIHash(ref model);
            }
        }

        /// <summary>
        /// 重新获取全局imei激活
        /// </summary>
        public static bool ResetIMEIHash(ref tempdata model)
        {
#if DEBUG
            log.Debug("ON_LINE_IMEI:-->" + GlobalVariables.GLOBAL_ON_LINE_IMEI.Count.ToString());
            foreach (string item in GlobalVariables.GLOBAL_ON_LINE_IMEI.Values)
            {
                log.Debug("ON_LINE_IMEI:--> " + item);
            }
#endif
            //GlobalVariables.GLOBAL_ON_LINE_IMEI.Clear();
            DataAccess.data_Facade dal = new DataAccess.data_Facade();
            //List<tempdata> list = dal.GetProducts(0, "");
            //if (list.Count > 0)
            //{
            //    foreach (tempdata item in list)
            //    {
            //        string _v = item.PID + "|" + item.product_model + "|" + item.type_id;
            //        if (GlobalVariables.GLOBAL_ON_LINE_IMEI.Contains(item.imei))
            //        {
            //            GlobalVariables.GLOBAL_ON_LINE_IMEI[item.imei] = _v;
            //        }
            //        else { GlobalVariables.GLOBAL_ON_LINE_IMEI.Add(item.imei, _v); }
            //    }
            //}
            tempdata m = dal.GetModel(model.imei);
            if (m != null)
            {
                model.PID = 0; //m.PID;
                model.product_model = m.product_model;
                model.type_id = m.type_id;
                string _v = model.PID + "|" + model.product_model + "|" + model.type_id;
                if (GlobalVariables.GLOBAL_ON_LINE_IMEI.Contains(model.imei))
                {
                    GlobalVariables.GLOBAL_ON_LINE_IMEI[model.imei] = _v;
                }
                else { GlobalVariables.GLOBAL_ON_LINE_IMEI.Add(model.imei, _v); }
                return true;
            }
            else
            {
                return false;
            }
        }

        /************************************************************************/
        /*                             DOWN                                     */
        /************************************************************************/

        public System.Collections.Hashtable GetOnLineIMEI()
        {
            return GlobalVariables.GLOBAL_ON_LINE_IMEI;
        }

        public System.Collections.Hashtable DownCommand(Dictionary<string, string> parm)
        {
#if DEBUG
            foreach (KeyValuePair<string, string> kvp in parm)
                log.Debug("DOWN:-->" + kvp.Key + " -- " + kvp.Value);
#endif
            try
            {
                foreach (KeyValuePair<string, string> kvp in parm)
                {
                    if (GlobalVariables.GLOBAL_ON_LINE_DOWN.Contains(kvp.Key))
                    {
                        GlobalVariables.GLOBAL_ON_LINE_DOWN[kvp.Key] = kvp.Value;
                    }
                    else
                    {
                        GlobalVariables.GLOBAL_ON_LINE_DOWN.Add(kvp.Key, kvp.Value);
                    }
                }
                return GlobalVariables.GLOBAL_ON_LINE_DOWN;
            }
            catch
            {
                return GlobalVariables.GLOBAL_ON_LINE_DOWN;
            }
        }
    }
}
