using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using BankTV.Consts;
using BankTV.Logger;
using BankTV.Exceptions;
using System.Windows;
using System.Xml;
using System.Net;
using BankTV.Weather;
using System.Xml.Serialization;
using BankTV.Model;
using BankTV.Model.Weather;
using System.Globalization;

namespace BankTV.Helpers.Weather
{
    class WeatherCurrentReader
    {

        private static current GetCurrentWeatherObject(string fileName)
        {
            try 
            {
                XmlSerializer serializer = new XmlSerializer(typeof(current));
                string weatherFolder = GlobalHelper.GetWeatherFolder();
                if (String.IsNullOrEmpty(weatherFolder))
                {
                    throw new AppNotInitiatedException("Unable to get weather folder.");
                }   //if
                string weatherFile = System.IO.Path.Combine(weatherFolder, fileName);
                if (!File.Exists(weatherFile))
                {
                    throw new AppNotInitiatedException("Unable to get weather file.");
                }   //if
                TextReader reader = new StreamReader(weatherFile);
                try
                {
                    return (current)serializer.Deserialize(reader);                    
                }   //try
                finally
                {
                    reader.Close();
                }   //finally
            }   //try
            catch (AppNotInitiatedException)
            {
                throw;
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                MyLogger.Log(ex.GetBaseException());
                return null;
            }   //catch
        }

        private static WeatherCurrent GetCurrentWeather(current currentWeather, string city)
        {
            try
            {
                if (currentWeather == null)
                {
                    return null;
                }   //if          
                WeatherCurrent weatherCurrent = new WeatherCurrent();
                weatherCurrent.City = city;
                foreach (var item in currentWeather.Items)
                {                    
                    if (item.GetType().Equals(typeof(currentCity)))
                    {
                        currentCity currentCity = (currentCity)item;                 
                        foreach  (var sun in currentCity.sun)
                        {
                            try
                            {
                                DateTime sunrise = DateTime.SpecifyKind(
                                    DateTime.Parse(sun.rise),
                                    DateTimeKind.Utc);
                                weatherCurrent.SunRise = sunrise.ToLocalTime();
                                DateTime sunset = DateTime.SpecifyKind(
                                    DateTime.Parse(sun.set),
                                    DateTimeKind.Utc);
                                weatherCurrent.SunSet = sunset.ToLocalTime();
                            }   //try
                            catch (Exception ex)
                            {
                                MyLogger.Log(ex);
                            }   //catch
                        }   //for sun
                    }   //if
                    if (item.GetType().Equals(typeof(currentTemperature)))
                    {
                        currentTemperature currentTemperature = (currentTemperature)item;
                        try
                        {
                            weatherCurrent.TempAvg = double.Parse(currentTemperature.value, CultureInfo.InvariantCulture);
                            weatherCurrent.TempMax = double.Parse(currentTemperature.max, CultureInfo.InvariantCulture);
                            weatherCurrent.TempMin = double.Parse(currentTemperature.min, CultureInfo.InvariantCulture);
                        }   //try
                        catch (Exception ex)
                        {
                            MyLogger.Log(ex);
                        }   //catch
                    }   //if
                    if (item.GetType().Equals(typeof(currentPressure)))
                    {
                        currentPressure currentPressure = (currentPressure)item;
                        try
                        {
                            weatherCurrent.Pressure = double.Parse(currentPressure.value, CultureInfo.InvariantCulture);
                        }   //try
                        catch (Exception ex)
                        {
                            MyLogger.Log(ex);
                        }   //catch
                    }   //if
                    if (item.GetType().Equals(typeof(currentWeather)))
                    {
                        currentWeather weather = (currentWeather)item;
                        weatherCurrent.WeatherImg = weather.icon;
                    }   //if
                    if (item.GetType().Equals(typeof(currentLastupdate)))
                    {
                        currentLastupdate lastUpdate = (currentLastupdate)item;
                        try
                        {
                            DateTime lastUpdateParsed = DateTime.SpecifyKind(
                                DateTime.Parse(lastUpdate.value),
                                DateTimeKind.Utc);
                            weatherCurrent.LastUpdate = lastUpdateParsed.ToLocalTime();
                        }   //try
                        catch (Exception ex)
                        {
                            MyLogger.Log(ex);
                        }   //catch
                    }   //if
                }   //for
                return weatherCurrent;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        private static WeatherCurrent GetCurrentWeather(string fileName, string city)
        {
            return WeatherCurrentReader.GetCurrentWeather(WeatherCurrentReader.GetCurrentWeatherObject(fileName), city);
        }

        public static WeatherCurrent GetCurrentWeather(string city)
        {
            return WeatherCurrentReader.GetCurrentWeather(WeatherCurrentReader.GetCurrentWeatherObject(GlobalConsts.WEATHER_CITY_FILE), city);
        }

        public static WeatherCurrent GetWeatherBialystok()
        {
            return WeatherCurrentReader.GetCurrentWeather(WeatherCurrentReader.GetCurrentWeatherObject(GlobalConsts.WEATHER_BIALYSTOK_FILE), GlobalConsts.WEATHER_BIALYSTOK_CITY);
        }

        public static WeatherCurrent GetWeatherGdansk()
        {
            return WeatherCurrentReader.GetCurrentWeather(WeatherCurrentReader.GetCurrentWeatherObject(GlobalConsts.WEATHER_GDANSK_FILE), GlobalConsts.WEATHER_GDANSK_CITY);
        }

        public static WeatherCurrent GetWeatherSzczecin()
        {
            return WeatherCurrentReader.GetCurrentWeather(WeatherCurrentReader.GetCurrentWeatherObject(GlobalConsts.WEATHER_SZCZECIN_FILE), GlobalConsts.WEATHER_SZCZECIN_CITY);
        }

        public static WeatherCurrent GetWeatherPoznan()
        {
            return WeatherCurrentReader.GetCurrentWeather(WeatherCurrentReader.GetCurrentWeatherObject(GlobalConsts.WEATHER_POZNAN_FILE), GlobalConsts.WEATHER_POZNAN_CITY);
        }

        public static WeatherCurrent GetWeatherWroclaw()
        {
            return WeatherCurrentReader.GetCurrentWeather(WeatherCurrentReader.GetCurrentWeatherObject(GlobalConsts.WEATHER_WROCLAW_FILE), GlobalConsts.WEATHER_WROCLAW_CITY);
        }

        public static WeatherCurrent GetWeatherWarszawa()
        {
            return WeatherCurrentReader.GetCurrentWeather(WeatherCurrentReader.GetCurrentWeatherObject(GlobalConsts.WEATHER_WARSZAWA_FILE), GlobalConsts.WEATHER_WARSZAWA_CITY);
        }

        public static WeatherCurrent GetWeatherRzeszow()
        {
            return WeatherCurrentReader.GetCurrentWeather(WeatherCurrentReader.GetCurrentWeatherObject(GlobalConsts.WEATHER_RZESZOW_FILE), GlobalConsts.WEATHER_RZESZOW_CITY);
        }

    }

}
