using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.Entities.FM;
using APT.Infrastructure.Core;
using APT.Utility;
using APT.PF.WebApi;
using IdentityModel.Client;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using TokenRequest = APT.Utility.TokenRequest;
using APT.Utility;
using APT.BaseData.Domain.Entities;
using System.Linq;
using IdentityModel;
using Microsoft.EntityFrameworkCore.Query;
using APT.BaseData.Domain.ApiModel.Platform;
using APT.BaseData.Domain.ApiModel;
using APT.Infrastructure.Api.Redis;

namespace APT.PF.WebApi.Controllers.Api.FM
{

    [Route("api/PF/HardCheck")]
    public partial class DataChannelController : AuthorizeApiController<T_PF_DATA_CHANNEL>
    {
        private int ErrCountLimit = 30;
        private int RAM = 32;
        [HttpGet, Route("Info")]
        public JsonActionResult<bool> Info(string checkResult)
        {
            return SafeExecute<bool>(() =>
            {
                //var redisManager = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<RedisManager>();
                int count = 0;
                if (checkResult.ToLower() != "true")
                {
                    if (CsRedisManager.KeyExists(RedisCacheKey.HardWareCheckErrCount))
                    {
                        count = CsRedisManager.StringGet<int>(RedisCacheKey.HardWareCheckErrCount);
                    }
                    count++;
                    CsRedisManager.StringSet(RedisCacheKey.HardWareCheckErrCount, count);
                    if (count > ErrCountLimit)
                    {
                        CsRedisManager.StringSet(RedisCacheKey.HardWareCheckErrCount, checkResult);
                    }
                }
                else
                {
                    CsRedisManager.StringSet(RedisCacheKey.HardWareCheckErrCount, count);
                    CsRedisManager.StringSet(RedisCacheKey.HardWareCheck, checkResult);
                }
                return true;
            });
        }


        [HttpPost, Route("UpdateInfo")]
        public JsonActionResult<bool> UpdateInfo([FromBody] ComputerInfo info)
        {
            return SafeExecute<bool>(() =>
            {
                //var redisManager = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<RedisManager>();
                if (decimal.Parse(info.CPU) > 0)
                {
                    CsRedisManager.StringSet(RedisCacheKey.HardWareCpuInfo, info.CPU);
                }
                CsRedisManager.StringSet(RedisCacheKey.HardWareRamInfo, info.RAM);
                return true;
            });
        }


    }
}