using LY.App.Common.Redis;
using LY.App.Device;
using LY.App.Model;
using LY.App.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
using System.Reactive.Joins;
namespace LY.App.Controllers
{
    /// 
    /// 首页控制器
    /// 
    [Route("api/[controller]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly DeviceService _deviceService;
        private readonly DeviceManager deviceManager = DeviceManager.Instance;
        private readonly PositionService _positionService;
        private readonly RedisService _redisService;
        private readonly AlarmService _alarmService;
        private readonly WeatherService _weatherService;
        /// 
        ///     
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public HomeController(DeviceService deviceService, PositionService positionService,
            RedisService redisService, AlarmService alarmService, WeatherService weatherService)
        {
            _deviceService = deviceService;
            _positionService = positionService;
            _redisService = redisService;
            _alarmService = alarmService;
            _weatherService = weatherService;
        }
        /// 
        /// 首页
        /// 
        /// 
        [HttpGet("index")]
        public async Task view()
        {
            ApiResult result = new ApiResult();
            var positions = await _positionService.Index();
            var alarmCount = await _redisService.GetOrSetAsync(RedisKeyList.index_data(),
                   async () => await _alarmService.IndexCount(),
                   TimeSpan.FromDays(1));
            var weather = await _redisService.GetOrSetAsync(RedisKeyList.Index_Weather,
                   async () => await _weatherService.GetWeather(),
            TimeSpan.FromHours(3));
            var data = await GetUserLocation();
            result.data = new
            {
                positions,
                alarmCount,
                weather,
                userLocation = data
            };
            return Ok(result);
        }
        private async Task> GetUserLocation()
        {
            var keys = await _redisService.GetAllKeysAsync("user_locationbyid_*");
            List result = new List();
            if (keys.Count > 0)
            {
                foreach (var item in keys)
                {
                    result.Add(await _redisService.GetAsync(item));
                }
            }
            return result;
        }
        /// 
        /// 同步移动端位置信息
        /// 
        /// 
        /// 
        [HttpPost("syncLocation")]
        public async Task SyncLocation(SyncLocation input)
        {
            string key = RedisKeyList.UserLocation(input.userId);
            await _redisService.SetAsync(key, new SyncLocation() { userId = input.userId, lat = input.lat, lon = input.lon }, TimeSpan.FromMinutes(1));
            return Ok(new ApiResult());
        }
    }
}