using langguanApi.Common.Redis;
using langguanApi.Common;
using langguanApi.Extensions.AutoDI;
using langguanApi.Model;
using System.Linq.Expressions;
namespace langguanApi.Service
{
    /// 
    /// HomeService
    /// 
    [ServiceInjection(InjectionType.Singleton)]
    public class HomeService
    {
        private DeviceService _deviceService;
        private Hj212Service _hj212Service;
        private readonly IConfiguration _configuration;
        private CacheManager _cacheManager;
        private readonly WeatherService _weatherService;
        private readonly AlertService _alertService;
        /// 
        /// HomeService
        /// 
        /// 
        /// 
        public HomeService(DeviceService device, Hj212Service hj212Service,
            IConfiguration configuration, CacheManager cacheManager,
            WeatherService weatherService, AlertService alertService)
        {
            _deviceService = device;
            _hj212Service = hj212Service;
            _configuration = configuration;
            _cacheManager = cacheManager;
            _weatherService = weatherService;
            _alertService = alertService;
        }
        /// 
        /// view
        /// 
        /// 
        public async Task View()
        {
            var devices = await _deviceService.GetAsync();
            var cleanData = new
            {
                Yesterday = 0.8,
                LastWeek = 0.6,
            };
         
            Expression> filter = exp => true;
            var Realtime = await _hj212Service.Realtime();
            var getViewTop = await _hj212Service.GetViewTop();
            var alerts = await _alertService.IndexData();
            // 获取天气信息,缓存1小时,如果不存在,则调用WeatherService获取
            Func> getWeatherFunc = async () => await _weatherService.GetWeather();
            var weather = await _cacheManager.GetConvertVale(RedisKeylist.Weather, getWeatherFunc, 60 * 60);
            Func> getAriQualityFunc= async () => await _weatherService.GetAirQuality();
            var ariQuality = await _cacheManager.GetConvertVale(RedisKeylist.AriQuality, getAriQualityFunc, 60 * 120);
            return new ApiResult
            {
                code = 0,
                data = new
                {
                    home = new
                    {
                        center = new
                        {
                            lon = _configuration.GetValue("Home:Center:Lon"),
                            lat = _configuration.GetValue("Home:Center:Lat"),
                        },
                        title = _configuration.GetValue("Home:Title"),
                    },
                    devices,
                    ariQuality,
                    Realtime,
                    getViewTop,
                    weather,
                    alerts
                }
            };
        }
    }
}