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 BankTV.Config.Local;
using System.Xml.Serialization;

namespace BankTV.Helpers.Config.Local
{
    class XmlLocalConfigHelper
    {

        private static local_configuration GetLocalConfigObject()
        {
            try 
            {
                XmlSerializer serializer = new XmlSerializer(typeof(local_configuration));
                string appConfigFolder = GlobalHelper.GetUserAppDataFolder();
                if (String.IsNullOrEmpty(appConfigFolder))
                {
                    throw new AppNotInitiatedException("Unable to get app config folder.");
                }   //if
                string appConfigFile = System.IO.Path.Combine(appConfigFolder, GlobalConsts.APP_LOCAL_CONFIG_FILE);
                if (!File.Exists(appConfigFile))
                {
                    throw new AppNotInitiatedException("Unable to get app config folder.");
                }   //if
                TextReader reader = new StreamReader(appConfigFile);
                try
                {
                    return (local_configuration)serializer.Deserialize(reader);
                }   //try
                finally
                {
                    reader.Close();
                }   //finally
            }   //try
            catch (AppNotInitiatedException)
            {
                throw;
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        private static local_configuration GetLocalConfigObject(String fileName)
        {
            if (String.IsNullOrWhiteSpace(fileName))
            {
                fileName = GlobalConsts.APP_LOCAL_CONFIG_FILE;
            }   //if
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(local_configuration));
                string appConfigFolder = GlobalHelper.GetUserAppDataFolder();
                if (String.IsNullOrEmpty(appConfigFolder))
                {
                    throw new AppNotInitiatedException("Unable to get app config folder.");
                }   //if
                string appConfigFile = System.IO.Path.Combine(appConfigFolder, fileName);
                if (!File.Exists(appConfigFile))
                {
                    appConfigFile = System.IO.Path.Combine(appConfigFolder, GlobalConsts.APP_LOCAL_CONFIG_FILE);
                    if (!File.Exists(appConfigFile))
                    {
                        throw new AppNotInitiatedException("Unable to get app config folder.");
                    }   //if
                }   //if
                TextReader reader = new StreamReader(appConfigFile);
                try
                {
                    return (local_configuration)serializer.Deserialize(reader);
                }   //try
                finally
                {
                    reader.Close();
                }   //finally
            }   //try
            catch (AppNotInitiatedException)
            {
                throw;
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        public static string GetCurrentCity()
        {
            try
            {
                local_configuration xmlConfig = XmlLocalConfigHelper.GetLocalConfigObject();
                return xmlConfig.city;
            }
            catch (AppNotInitiatedException)
            {
                return GlobalConsts.CITY;
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                throw;
            }
        }

        public static string GetCurrentCity(String param)
        {
            if (String.IsNullOrWhiteSpace(param))
            {
                return XmlLocalConfigHelper.GetCurrentCity();
            }   //if
            try
            {
                String fileName = String.Format("{0}-{1}{2}", GlobalConsts.APP_LOCAL_CONFIG_FILE_PREFIX, param, GlobalConsts.APP_LOCAL_CONFIG_FILE_SUFFIX);
                local_configuration xmlConfig = XmlLocalConfigHelper.GetLocalConfigObject(fileName);
                return xmlConfig.city;
            }
            catch (AppNotInitiatedException)
            {
                return GlobalConsts.CITY;
            }   //catch
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                throw;
            }
        }

    }

}
