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;
using System.Text.Json;

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

        //[HttpGet]
        public async Task<string?> Get()  //请求方法行为
        {
            return "hello，这里是恒拓实时服务！";
        }
        //[HttpGet]
        public async Task<string> GetName(string Name)  //请求方法行为
        {
            return $"服务器收到字符串：{Name}";
        }
		//[HttpGet]
		public async Task<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());

			//public static string StringToJson(string JsonString)
			//{ // string 转换成Json JsonDocument jsonDocument = JsonDocument.Parse(JsonString);//这里序列化成json return JsonSerializer.Serialize(jsonDocument.RootElement); }//在main中调用
            
			try
            {
                //var result = StaticLibrary.MemoryCacheHelper.GetString(groupName);

                //Console.WriteLine(result);
                //return result;
                //return valueJson.ToString();
                return StaticLibrary.MemoryCacheHelper.GetString(groupName);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        //[HttpGet]
        public async Task<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());


            //!!!!重要，校验有无groupName

            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);
        //}
    }
}
