using System;
using System.Configuration;
using Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using NLog;
using RestSharp;

namespace BLW_Log.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LeiDaController : ControllerBase
    {

        public readonly IMemoryCache _memoryCache;
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public readonly IConfiguration? _configurationManager;

        public LeiDaController(IMemoryCache cache, IConfiguration config)
        {
            _memoryCache = cache;
            _configurationManager = config;
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost()]
        public async Task<ReturnInfo> Login(LoginData data)
        {
            ReturnInfo r = new ReturnInfo();
            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("username", data.username);
                dic.Add("password", data.password);
                r = await Http.HttpPostSendData(dic, "/api/LogSystemlogin");
            }
            catch (Exception ex)
            {
                r.isok = false;
                r.response = ex.Message;
            }
            return r;
        }
        /// <summary>
        /// 获取RCU的状态
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost()]
        public async Task<ReturnInfo> GetRCUStatus(dynamic data)
        {
            ReturnInfo r = new ReturnInfo();
            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("code", data.code);
                dic.Add("creatDate", data.creatDate);
                dic.Add("roomNumber", data.roomNumber);
                r = await Http.HttpGetSendData(dic, "/api/GetRCUStatus");
            }
            catch (Exception ex)
            {
                r.isok = false;
                r.response = ex.Message;
            }
            return r;
        }

        /// <summary>
        /// 获取房型及其 回路信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost()]
        public async Task<ReturnInfo> GetRoomTypeAndModalsList(HotelInfo data)
        {
            ReturnInfo r = new ReturnInfo();
            try
            {
                string KKK = "GetRoomTypeAndModalsList_"+data.code;
                object ooo = _memoryCache.Get(KKK);
                if (ooo != null)
                {
                    r = (ReturnInfo)ooo;
                }
                else
                {
                    r = await Http.HttpGetSendData(data, "/api/GetRoomTypeAndModalsListLog");
                    _memoryCache.Set(KKK,r,new TimeSpan(0,50,0));
                }
            }
            catch (Exception ex)
            {
                r.isok = false;
                r.response = ex.Message;
            }
            return r;
        }
    }

}
