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 WeatherForecastReader
    {

        private static weatherdata GetForecastWeatherObject(string fileName)
        {
            try 
            {
                XmlSerializer serializer = new XmlSerializer(typeof(weatherdata));
                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 (weatherdata)serializer.Deserialize(reader);                    
                }   //try
                finally
                {
                    reader.Close();
                }   //finally
            }   //try
            catch (AppNotInitiatedException)
            {
                throw;
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        private static List<WeatherForecast> GetForecastWeather(weatherdata weatherData, string city)
        {
            try
            {
                if (weatherData == null)
                {
                    return null;
                }   //if          
                List<WeatherForecast> weatherForecasts = new List<WeatherForecast>();
                foreach (var item in weatherData.forecast)
                {                    
                    if (item.GetType().Equals(typeof(weatherdataTime)))
                    {
                        WeatherForecast weatherForecast = new WeatherForecast();
                        weatherdataTime forecast = (weatherdataTime)item;
                        try
                        {
                            weatherForecast.City = city;
                            weatherForecast.Day = DateTime.Parse(forecast.day);
                            foreach (var tempItem in forecast.temperature)
                            {
                                if (tempItem.GetType().Equals(typeof(weatherdataTimeTemperature)))
                                {
                                    weatherdataTimeTemperature temperature = (weatherdataTimeTemperature)tempItem;
                                    try
                                    {
                                        weatherForecast.TempDay = double.Parse(temperature.day, CultureInfo.InvariantCulture);
                                        weatherForecast.TempEvening = double.Parse(temperature.eve, CultureInfo.InvariantCulture);
                                        weatherForecast.TempMax = double.Parse(temperature.max, CultureInfo.InvariantCulture);
                                        weatherForecast.TempMin = double.Parse(temperature.min, CultureInfo.InvariantCulture);
                                        weatherForecast.TempMorning = double.Parse(temperature.morn, CultureInfo.InvariantCulture);
                                        weatherForecast.TempNight = double.Parse(temperature.night, CultureInfo.InvariantCulture);
                                    }
                                    catch (Exception ex)
                                    {
                                        MyLogger.Log(ex);
                                    }   //catch
                                }   //if
                            }   //for
                            foreach (var symbolItem in forecast.symbol)
                            {
                                if (symbolItem.GetType().Equals(typeof(weatherdataTimeSymbol)))
                                {
                                    weatherdataTimeSymbol symbol = (weatherdataTimeSymbol)symbolItem;
                                    try
                                    {
                                        weatherForecast.WeatherImg = symbolItem.var;
                                    }
                                    catch (Exception ex)
                                    {
                                        MyLogger.Log(ex);
                                    }   //catch
                                }   //if
                            }   //for
                        }   //try
                        catch (Exception ex)
                        {
                            MyLogger.Log(ex);
                        }   //catch
                        weatherForecasts.Add(weatherForecast);
                    }   //for
                }   //for
                return weatherForecasts;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        public static List<WeatherForecast> GetForecastWeather(string city)
        {
            return WeatherForecastReader.GetForecastWeather(WeatherForecastReader.GetForecastWeatherObject(GlobalConsts.WEATHER_FORECAST_FILE), city);
        }
    }

}
