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 System.Configuration;

namespace BankTV.Helpers
{
    class GlobalHelper
    {

        public static string GetUserAppDataFolder()
        {
            try
            {
                string appConfigFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, GlobalConsts.APP_CONFIG_FOLDER);
                //string appConfigFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), GlobalConsts.APP_CONFIG_FOLDER);
                if (Directory.Exists(appConfigFolder))
                {
                    return appConfigFolder;
                }   //if
                Directory.CreateDirectory(appConfigFolder);
                return appConfigFolder;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        private static string GetAppConfigSubfolder(string subfolder)
        {
            try
            {
                if (String.IsNullOrEmpty(subfolder))
                {
                    return String.Empty;
                }   //if
                string appConfigFolder = GlobalHelper.GetUserAppDataFolder();
                if (String.IsNullOrEmpty(appConfigFolder))
                {
                    throw new Exception("Unable to get app configuration folder.");
                }   //if

                string subfolderPath = Path.Combine(appConfigFolder, subfolder);
                if (Directory.Exists(subfolderPath))
                {
                    return subfolderPath;
                }   //if
                Directory.CreateDirectory(subfolderPath);
                return subfolderPath;

            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        public static string GetLogsFolder()
        {
            return GlobalHelper.GetAppConfigSubfolder(GlobalConsts.LOG_FOLDER);
        }

        public static string GetImagesFolder()
        {
            return GlobalHelper.GetAppConfigSubfolder(GlobalConsts.IMAGE_FILES_FOLDER);
        }

        public static string GetFlashFolder()
        {
            return GlobalHelper.GetAppConfigSubfolder(GlobalConsts.FLASH_FILES_FOLDER);
        }

        public static string GetVideosFolder()
        {
            return GlobalHelper.GetAppConfigSubfolder(GlobalConsts.VIDEOS_FILES_FOLDER);
        }

        public static string GetNewsFolder()
        {
            return GlobalHelper.GetAppConfigSubfolder(GlobalConsts.NEWS_FILES_FOLDER);
        }

        public static string GetWeatherFolder()
        {
            return GlobalHelper.GetAppConfigSubfolder(GlobalConsts.WEATHER_FILES_FOLDER);
        }

        public static string GetStockFolder()
        {
            return GlobalHelper.GetAppConfigSubfolder(GlobalConsts.STOCK_FILES_FOLDER);
        }

        public static string GetCurrencyFolder()
        {
            return GlobalHelper.GetAppConfigSubfolder(GlobalConsts.CURRENCY_FILES_FOLDER);
        }

        public static string GetBottomBarNewsFolder()
        {
            return GlobalHelper.GetAppConfigSubfolder(GlobalConsts.BOTTOM_BAR_NEWS_FILES_FOLDER);
        }

        public static string getPolishDayOfWeek(DateTime date)
        {
            if (date == null)
            {
                return "--";
            }   //if
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Friday:
                    return "Piątek";
                case DayOfWeek.Monday:
                    return "Poniedziałek";
                case DayOfWeek.Saturday:
                    return "Sobota";
                case DayOfWeek.Sunday:
                    return "Niedziela";
                case DayOfWeek.Thursday:
                    return "Czwartek";
                case DayOfWeek.Tuesday:
                    return "Wtorek";
                case DayOfWeek.Wednesday:
                    return "Środa";
            }   //switch
            return "--";
        }

        public static int? GetIntValueFromAppConfig(string key)
        {
            if (String.IsNullOrWhiteSpace(key))
            {
                return null;
            }   //if
            try
            {
                MyLogger.Log(String.Format("{0} - {1}", key, ConfigurationManager.AppSettings.Get(key)));
                return int.Parse(ConfigurationManager.AppSettings.Get(key));
            }
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

        public static string GetValueFromAppConfig(string key)
        {
            if (String.IsNullOrWhiteSpace(key))
            {
                return null;
            }   //if
            try
            {
                MyLogger.Log(String.Format("{0} - {1}", key, ConfigurationManager.AppSettings.Get(key)));
                return ConfigurationManager.AppSettings.Get(key);
            }
            catch (Exception ex)
            {
                MyLogger.Log(ex);
                return null;
            }   //catch
        }

    }

}
