using HTCoreServiceApp.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HTCoreServiceApp.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class DataLiveCache : ControllerBase
    {

        [HttpGet]
        public string Get()  //请求方法行为
        {
            return "hello，这里是恒拓实时服务！";
        }
        [HttpGet]
        public string Getname(string name)  //请求方法行为
        {
            return $"服务器收到字符串：{name}";
        }
        [HttpGet]
        public string GetLiveGroupData(string groupName) 
        {
            //var cache = StaticLibrary.MemoryCacheHelper.Get<List<LiveGroupDictionary>>(driverName).Where(x=>x.GroupName == groupName).First();
            //var val = new List<LiveGroupDictionary>();
            //StaticLibrary.MemoryCacheHelper.TryGetValue(driverName,out val);
            //return LiveJsonIntegrate.MetaValues2Json(val.FirstOrDefault());

            try
            {
                return StaticLibrary.MemoryCacheHelper.GetString(groupName);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        [HttpGet]
        public string GetLiveTagData(string groupName, string tagName)
        {
            //var cache = StaticLibrary.MemoryCacheHelper.Get<List<LiveGroupDictionary>>(driverName).Where(x=>x.GroupName == groupName).First();
            //var val = new List<LiveGroupDictionary>();
            //StaticLibrary.MemoryCacheHelper.TryGetValue(driverName,out val);
            //return LiveJsonIntegrate.MetaValues2Json(val.FirstOrDefault());
            string value = "";
            if (tagName != null && tagName != "")
            {
                var jLiveData = StaticLibrary.MemoryCacheHelper.GetString(groupName);
                JObject valueJson = JObject.Parse(jLiveData);
                value = valueJson[tagName].ToString();
            }
            return value;
        }
        //[HttpGet]
        //public string GetLiveTagData(string driverName, string groupName ,string tagName)
        //{
        //var cache = StaticLibrary.MemoryCacheHelper.Get<List<LiveGroupDictionary>>(driverName).Where(x => x.GroupName == groupName).First();
        //return LiveJsonIntegrate.MetaTagValue2Json(cache, tagName);
        //}
    }
}
