using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Library;
using LitJson;
using MySystem;

public class LogHelper
{
    public readonly static LogHelper Instance = new LogHelper();
    private LogHelper()
    { }

    public void WriteLog(string Content, string FileName, string BrandId = "0")
    {
        function.WriteLog(Content, FileName);
        // Dictionary<string, string> dic = new Dictionary<string, string>();
        // dic.Add("Topic", FileName);
        // dic.Add("Content", Content);
        // dic.Add("BrandId", BrandId);
        // RedisDbconn.Instance.AddList("LogQueue", Newtonsoft.Json.JsonConvert.SerializeObject(dic));
    }

    public void Start()
    {
        Thread th = new Thread(DoWorks);
        th.IsBackground = true;
        th.Start();
    }

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

    public void DoQueue(string content)
    { 
        JsonData JsonObj = JsonMapper.ToObject(content);
        string Topic = JsonObj["Topic"].ToString();
        string Cont = JsonObj["Content"].ToString();
        string BrandId = JsonObj["BrandId"].ToString();
        // SLS.WriteLog(DateTime.Now, Topic, Cont, new Dictionary<string, string>()
        // {
        //     {"BrandId", BrandId}
        // });
    }
}