using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public static class DuiOperation
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(DuiOperation));
        private static readonly string _uploadURL = "https://gw.duiopen.com";
        /// <summary>
        /// 开启设备更新上报
        /// </summary>
        /// <returns></returns>
        public static bool OpenUploadApplianceSwitch()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(_uploadURL);
            sb.Append("/dcas/v1/uploadApplianceSwitch");
            sb.Append("?apikey=b4b4c81c01e446b4b40f85b196c6aaec");
            sb.Append("&skillId=2020120900000014");
            try
            {
                string result = HttpWebRequestHelper.PutWebRequest(sb.ToString(), "{'status':1}");
                DuiResult duiResult = Newtonsoft.Json.JsonConvert.DeserializeObject<DuiResult>(result);
                if (duiResult.errId != 0)
                {
                    logger.Error(string.Format("DUI开启设备更新上报失败，url:{0},errId:{1}", sb.ToString(), duiResult.errId));
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(string.Format("DUI开启设备更新上报失败，url:{0},原因：{1}", sb.ToString(), ex));
            }
            return false;
        }
        /// <summary>
        /// 通知更新技能
        /// </summary>
        /// <param name="hotelName"></param>
        /// <param name="roomNumber"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static bool UploadDeviceFun(string hotelName, string roomNumber, string accessToken)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(_uploadURL);
            sb.Append("/dcas/v1/uploadAppliance");
            sb.Append("?apikey=b4b4c81c01e446b4b40f85b196c6aaec");
            sb.Append("&skillId=2020120900000014");
            //sb.Append("&productId=2020120900000014");
            sb.Append("&accessToken=" + accessToken);
            sb.Append("&group=" + roomNumber);
            sb.Append("&type=notify");
            try
            {
                string result = HttpWebRequestHelper.PutWebRequest(sb.ToString(), "");
                DuiResult duiResult = Newtonsoft.Json.JsonConvert.DeserializeObject<DuiResult>(result);
                if (duiResult.errId != 0)
                {
                    logger.Error(string.Format("DUI酒店({0})客房({1})同步技能失败。errId:{2}", hotelName, roomNumber, duiResult.errId));
                    return false;
                }
                //logger.Error(string.Format("DUI酒店({0})客房({1})已通知同步技能。", hotelName, roomNumber));
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(string.Format("DUI酒店({0})客房({1})调用DUI接口({2})失败：{3}", hotelName, roomNumber, _uploadURL, ex));
            }
            return false;
        }
    }

    internal class DuiResult
    {
        public int errId { get; set; }
        public int status { get; set; }
    }
}
