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.Currency;
using System.Xml.Serialization;
using BankTV.Model.Currency;
using System.Globalization;

namespace BankTV.Helpers.Currency
{
    class CurrencyHelper
    {

        private static currencies GetCurrenciesObject()
        {
            try 
            {
                XmlSerializer serializer = new XmlSerializer(typeof(currencies));
                string currencyConfigFolder = GlobalHelper.GetCurrencyFolder();
                if (String.IsNullOrEmpty(currencyConfigFolder))
                {
                    throw new AppNotInitiatedException("Unable to get currency config folder.");
                }   //if
                string currencyConfigFile = System.IO.Path.Combine(currencyConfigFolder, GlobalConsts.CURRENCY_CONFIG_FILE);
                if (!File.Exists(currencyConfigFile))
                {
                    throw new AppNotInitiatedException("Unable to get currency config folder.");
                }   //if
                TextReader reader = new StreamReader(currencyConfigFile);
                try
                {
                    return (currencies)serializer.Deserialize(reader);                    
                }   //try
                finally
                {
                    reader.Close();
                }   //finally
            }   //try
            catch (AppNotInitiatedException)
            {
                throw;
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        private static CurrencyItem GetCurrencyItem(currenciesCurrency currencyXml)
        {
            try
            {
                if (currencyXml == null)
                {
                    return null;
                }   //if
                CurrencyItem currency = new CurrencyItem();
                currency.Abbr = currencyXml.symbol;
                currency.ImageName = currencyXml.img;
                currency.BuyingPriceDirection = CurrencyHelper.GetCurrencyDirection(currencyXml.buying_direction);
                currency.SellingPriceDirection = CurrencyHelper.GetCurrencyDirection(currencyXml.selling_direction);
                try
                {
                    currency.BuyingPrice = decimal.Parse(currencyXml.buying_price, CultureInfo.InvariantCulture);
                    currency.SellingPrice = decimal.Parse(currencyXml.selling_price, CultureInfo.InvariantCulture);
                }   //try
                catch (Exception ex)
                {
                    MyLogger.Log(ex);
                }   //catch
                return currency;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch

        }

        private static List<CurrencyItem> GetCurrencyItems(currencies currenciesObject)
        {
            try
            {
                if (currenciesObject == null)
                {
                    throw new AppNotInitiatedException("Unable to get app configuration.");
                }   //if
                List<CurrencyItem> currencyItems = new List<CurrencyItem>();
                foreach (currenciesCurrency currencyXml in currenciesObject.Items)
                {
                    CurrencyItem item = CurrencyHelper.GetCurrencyItem(currencyXml);
                    if (currencyItems != null)
                    {
                        currencyItems.Add(item);
                    }   //if
                }   //for
                return currencyItems;
            }   //try
            catch (AppNotInitiatedException)
            {
                throw;
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        public static List<CurrencyItem> GetCurrencyItems()
        {
            try
            {
                currencies currenciesObject = CurrencyHelper.GetCurrenciesObject();
                if (currenciesObject == null)
                {
                    throw new AppNotInitiatedException("Unable to get currency configuration.");
                }   //if
                return CurrencyHelper.GetCurrencyItems(currenciesObject);
            }   //try
            catch (AppNotInitiatedException)
            {
                throw;
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        private static CurrencyItem.CurrencyPriceDirection GetCurrencyDirection(string direction)
        {
            if (String.IsNullOrEmpty(direction))
            {
                return CurrencyItem.CurrencyPriceDirection.NOCHANGE;
            }   //if
            if (direction.Equals(GlobalConsts.CURRENCY_DIRECTION_DOWN))
            {
                return CurrencyItem.CurrencyPriceDirection.DOWN;
            }   //if
            if (direction.Equals(GlobalConsts.CURRENCY_DIRECTION_UP))
            {
                return CurrencyItem.CurrencyPriceDirection.UP;
            }   //if
            return CurrencyItem.CurrencyPriceDirection.NOCHANGE;            
        }
    }

}
