using System;
using System.Linq;
using System.Data;
using System.Threading;
using Library;
using LitJson;
using MySystem.Models.Push;
using System.Collections.Generic;

namespace MySystem
{   
    public class AliyunPushHelper
    {
        public readonly static AliyunPushHelper Instance = new AliyunPushHelper();
        private AliyunPushHelper()
        {
        }

        public void Start()//启动
        {
            Thread thread = new Thread(threadStart);
            thread.IsBackground = true;
            thread.Start();
        }

        private void threadStart()
        {
            while (true)
            {
                string content = RedisDbconn.Instance.RPop<string>("AliyunPushQueue");
                if (!string.IsNullOrEmpty(content))
                {
                    try
                    {
                        DoSomeThing(content);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex, "SLS日志异常");
                    }
                }
                else
                {
                    Thread.Sleep(5000);
                }
            }
        }

        //要执行的方法
        public void DoSomeThing(string content)
        {
            JsonData data = JsonMapper.ToObject(content);
            string Account = data["Account"].ToString();
            if(Account == "14781419364")
            {
                return;
            }
            string Device = data["Device"].ToString();
            string Title = data["Title"].ToString();
            string Body = data["Body"].ToString();
            if(Device == "IOS")
            {
                AliyunPush.PushForIos(Account, Title, Body);
            }
            else
            {
                AliyunPush.Push(Account, Title, Body);
            }
        }
        
    }
}