﻿using Dawodev.Southwind.Contracts;
using Dawodev.Southwind.Model.Abstractions;
using Dawodev.Southwind.Model.OpenWeatherMapAPI;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace Dawodev.Southwind.Data
{
    /// <summary>
    /// Spezifisches Repository für den Abruf von Wetterdaten des Providers OpenWeatherMap
    /// </summary>
    public class OpenWeatherMapRepository : IRepository
    {
        public async Task<WeatherRoot> GetWeatherByCoordinatesAsync(GeoCoordinate geoCoordinate, Units unit)
        {
            using (HttpClient client = new HttpClient())
            {
                string requestUri = string.Format(Constants.WEATHER_BY_COORDS_URI, geoCoordinate.Latitude, geoCoordinate.Longitude, unit.ToString().ToLower());

                var response = await client.GetStringAsync(requestUri);

                if (string.IsNullOrWhiteSpace(response))
                    return null;

                WeatherRoot weatherRoot = JsonConvert.DeserializeObject<WeatherRoot>(response);
                return weatherRoot;
            }
        }

        public async Task<WeatherRoot> GetWeatherByCityAsync(string cityName, Units unit)
        {
            using (HttpClient client = new HttpClient())
            {
                string requestUri = string.Format(Constants.WEATHER_BY_CITY_URI, cityName, unit.ToString().ToLower());

                var response = await client.GetStringAsync(requestUri);

                if (string.IsNullOrWhiteSpace(response))
                    return null;

                WeatherRoot weatherRoot = JsonConvert.DeserializeObject<WeatherRoot>(response);
                return weatherRoot;
            }
        }

        public async Task<WeatherForecastRoot> GetForecastByCoordinatesAsync(GeoCoordinate geoCoordinate, Units unit)
        {
            using (HttpClient client = new HttpClient())
            {
                string requestUri = string.Format(Constants.FORECAST_BY_COORDS_URI, geoCoordinate.Latitude, geoCoordinate.Longitude, unit.ToString().ToLower());

                var response = await client.GetStringAsync(requestUri);

                if (string.IsNullOrWhiteSpace(response))
                    return null;

                WeatherForecastRoot weatherForecastRoot = JsonConvert.DeserializeObject<WeatherForecastRoot>(response);
                return weatherForecastRoot;
            }
        }

        /// <summary>
        /// Recommended to call API by city ID to get unambiguous result for your city
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public async Task<WeatherForecastRoot> GetForecastByCityIdAsync(int cityId, Units unit)
        {
            using (HttpClient client = new HttpClient())
            {
                string requestUri = string.Format(Constants.FORECAST_BY_ID_URI, cityId, unit.ToString().ToLower());

                var response = await client.GetStringAsync(requestUri);

                if (string.IsNullOrWhiteSpace(response))
                    return null;

                WeatherForecastRoot weatherForecastRoot = JsonConvert.DeserializeObject<WeatherForecastRoot>(response);
                return weatherForecastRoot;
            }
        }
    }
}
