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

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

        // string accesskeyld = "IOT_ACCOUNT_TEST_CSYH";
        // string accesskeySecret = "AtR4g5sa24fdy6GII3hts75_CSYH";
        // string reqUrl = "https://voicetest.tysmartpos.com/api/bank/pushmsg";

        string accesskeyld = "IOT_ACCOUNT_PROD_W966HG08MCOXHNT2";
        string accesskeySecret = "F2jzl8vGkm0fMeDLyYo40wEbzdaiTX2ywj3YNgszJHAbVbcAYIJL9bzzS9lVPPL6";
        string reqUrl = "https://voice.tysmartpos.com/api/bank/pushmsg";

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

        // public void listen()
        // {
        //     while (true)
        //     {
        //         string content = RedisDbconn.Instance.RPop<string>("TianYuVoiceQueue");
        //         if (!string.IsNullOrEmpty(content))
        //         {
        //             try
        //             {
        //                 doSomething(content);
        //                 Thread.Sleep(10);
        //             }
        //             catch (Exception ex)
        //             {
        //                 LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "天喻云喇叭异常");
        //             }
        //         }
        //         else
        //         {
        //             Thread.Sleep(1000);
        //         }
        //     }
        // }

        public string doSomething(string OrderNo, string DeviceId, string Amount)
        {
            string result = "";
            try
            {
                SortedList<string, string> data = new SortedList<string, string>();
                data.Add("accessKeyId", accesskeyld); //由本公司统一提供给客户的唯一识别码(同accessKeyId)
                data.Add("requestId", OrderNo); //请求标识，用于唯一标识当前请求
                data.Add("timestamp", function.GetCurTimestamp().ToString()); //请求时间戳，格式如：1593532800000
                data.Add("deviceId", DeviceId); //设备编号
                data.Add("content", "来客八收款" + Amount + "元"); //（终端直接播报内容，支持 tts 设备可以直接播报汉字语音，不支持 tts 的设备要分开传 requestData、payType）
                // data.Add("requestData", "0.01"); //播报金额(（只送金额，单位：元)
                // data.Add("payType", "01"); //播报类型:  01-XX银行收款成功    02-支付宝收款成功  03-微信收款成功 10-取消支付
                result = post(data);
            }
            catch (Exception ex)
            {
                LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "天喻云喇叭异常");
            }
            return result;
        }

        private string post(SortedList<string, string> data)
        {
            string signString = function.BuildQueryString(data);
            data.Add("sign", hmac_sha256(signString));
            string req = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            function.WriteLog("请求地址：" + reqUrl, "天谕音响日志");
            function.WriteLog("请求参数：" + req, "天谕音响日志");
            string result = function.PostWebRequest(reqUrl, req, "application/json");
            function.WriteLog("响应数据：" + result, "天谕音响日志");
            return result;
        }

        #region hmac_sha256算法

        public string hmac_sha256(string message)
        {
            var encoding = new System.Text.UTF8Encoding();
            byte[] keyByte = encoding.GetBytes(accesskeySecret);
            byte[] messageBytes = encoding.GetBytes(message);
            using (var hmacsha256 = new HMACSHA256(keyByte))
            {
                byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                return Convert.ToBase64String(hashmessage);
            }
        }

        #endregion
    }
}