using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using System;
//using Ropin.Environmentally.MqttService;
//using static Ropin.Environmentally.MqttService.Controllers.ValueController;
using System.Collections.Generic;

namespace Ropin.Environmentally.LoRaService.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ValueController : ControllerBase
    {
        private readonly ILogger<ValueController> _logger;

        public IMemoryCache _memoryCache;

        public ValueController(ILogger<ValueController> logger, IMemoryCache memoryCache)
        {
            _logger = logger;
            _memoryCache = memoryCache;
        }
        public class TdevDevicePointSearchModel
        {
            public string[] groupnames { get; set; }
            public string[] names { get; set; }
            public int timeOut { get; set; } = 600;

        }
        //public class DeviceCommandModel
        //{
        //    public string Topic { get; set; }
        //    public string Command { get; set; }
        //    public int timeOut { get; set; } = 600;

        //}
        public class DevPointValue
        {
            public string id { get; set; }
            public string timestamp { get; set; }
            public string dataType { get; set; }
            public string name { get; set; }
            public string value { get; set; }
            public string status { get; set; }
            public string boxId { get; set; }
            public string connState { get; set; }
            public string connStateTimestamp { get; set; }

        }

        [HttpGet("Get")]
        public string Get(string boxNo)
        {
            return _memoryCache.Get(boxNo)?.ToString();
        }

        [HttpPost("GetDevicePointByAsync")]
        public async Task<IList<DevPointValue>> GetDevicePointByAsync([FromQuery] string boxNo, [FromBody] TdevDevicePointSearchModel searchModel)
        {
            //if (searchModel == null)
            //{
            //    return new ApiResult(ReturnCode.ArgsError);
            //}
            //searchModel.IsPagination = false;
            IList<DevPointValue> devPoints = new List<DevPointValue>();
            try
            {
                for (int i = 0; i < searchModel.names.Length; i++)
                {
                    string name = searchModel.names[i];
                    string groupname = searchModel.groupnames[i];
                    string englishname = TCPServer._mqttDevicePointNameMappingDic[boxNo + groupname + name];
                    LoRaDevicePoint mqttDevicePoint;
                    TCPServer._mqttDevicePointDic.TryGetValue(englishname, out mqttDevicePoint);
                    if (mqttDevicePoint != null)
                        devPoints.Add(new DevPointValue
                        {
                            id = mqttDevicePoint.Id,
                            timestamp = mqttDevicePoint.UpLoadTime.ToString("yyyy-MM-dd hh:mm:ss"),
                            dataType = mqttDevicePoint.DataType,
                            name = mqttDevicePoint.ChineseName,
                            value = mqttDevicePoint.Value,
                            status = mqttDevicePoint.BoxStatus,
                            boxId = mqttDevicePoint.BoxId,
                            connState = "1",
                            connStateTimestamp = mqttDevicePoint.UpLoadTime.ToString("yyyy-MM-dd hh:mm:ss"),
                        });
                }
                return devPoints;

            }
            catch (Exception ex)
            {
                //return new ApiResult(ReturnCode.GeneralError, ex.Message);
                return null;
            }
        }


    }
}