//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using NopSolutions.NopCommerce.Common.Audit;
using NopSolutions.NopCommerce.Common.Configuration.Settings;
using NopSolutions.NopCommerce.Common.Directory;
 

namespace NopSolutions.NopCommerce.Common.Localization
{
    /// <summary>
    /// Provides information about localization
    /// </summary>
    public class LocalizationManager
    {
        #region Methods
        /// <summary>
        /// Gets currency string
        /// </summary>
        /// <param name="Amount">Amount</param>
        /// <returns>Currency string without exchange rate</returns>
        public static string GetCurrencyString(decimal Amount)
        {
            bool ShowCurrency = true;
            Currency TargetCurrency = NopContext.Current.WorkingCurrency;
            return GetCurrencyString(Amount, ShowCurrency, TargetCurrency);
        }

        /// <summary>
        /// Gets currency string
        /// </summary>
        /// <param name="Amount">Amount</param>
        /// <param name="ShowCurrency">A value indicating whether to show a currency</param>
        /// <param name="TargetCurrency">Target currency</param>
        /// <returns>Currency string without exchange rate</returns>
        public static string GetCurrencyString(decimal Amount, bool ShowCurrency, Currency TargetCurrency)
        {
            string result = string.Empty;
            if (!String.IsNullOrEmpty(TargetCurrency.CustomFormatting))
            {
                result = Amount.ToString(TargetCurrency.CustomFormatting);
            }
            else
            {
                if (!String.IsNullOrEmpty(TargetCurrency.DisplayLocale))
                {
                    result = Amount.ToString("C", new CultureInfo(TargetCurrency.DisplayLocale));
                }
                else
                {
                    result = String.Format("{0} ({1})", Amount.ToString("N"), TargetCurrency.CurrencyCode);                   
                    return result;
                }
            }

            if (ShowCurrency && CurrencyManager.GetAllCurrencies().Count > 1)
                result = String.Format("{0} ({1})", result, TargetCurrency.CurrencyCode);
            return result;
        }

        /// <summary>
        /// Gets a resource string based on the specified ResourceKey property.
        /// </summary>
        /// <param name="ResourceKey">A string representing a ResourceKey.</param>
        /// <returns>A string representing the requested resource string.</returns>
        public static string GetLocaleResourceString(string ResourceKey)
        {
            Language language = NopContext.Current.WorkingLanguage;
            return GetLocaleResourceString(ResourceKey, language.LanguageID);
        }

        /// <summary>
        /// Gets a resource string based on the specified ResourceKey property.
        /// </summary>
        /// <param name="ResourceKey">A string representing a ResourceKey.</param>
        /// <param name="LanguageID">Language identifier</param>
        /// <returns>A string representing the requested resource string.</returns>
        public static string GetLocaleResourceString(string ResourceKey, int LanguageID)
        {
            return GetLocaleResourceString(ResourceKey, LanguageID, true);
        }

        /// <summary>
        /// Gets a resource string based on the specified ResourceKey property.
        /// </summary>
        /// <param name="ResourceKey">A string representing a ResourceKey.</param>
        /// <param name="LanguageID">Language identifier</param>
        /// <param name="LogIfNotFound">A value indicating whether to log error if locale string resource is not found</param>
        /// <returns>A string representing the requested resource string.</returns>
        public static string GetLocaleResourceString(string ResourceKey, int LanguageID, bool LogIfNotFound)
        {
            string result = string.Empty;
            LocaleStringResourceCollection resources = LocaleStringResourceManager.GetAllResourcesByLanguageID(LanguageID);

            foreach (LocaleStringResource lsr in resources)
                if (lsr.ResourceName == ResourceKey)
                {
                    result = lsr.ResourceValue;
                    break;
                }

            if (String.IsNullOrEmpty(result))
            {
                result = ResourceKey;
                if (LogIfNotFound)
                {
                    LogManager.InsertLog(LogTypeEnum.CommonError, "Resource string is not found", string.Format("Resource string ({0}) is not found. Language ID ={1}", ResourceKey, LanguageID));
                }
            }
            return result;
        }

        /// <summary>
        /// Gets or sets the default admin language
        /// </summary>
        public static Language DefaultAdminLanguage
        {
            get
            {
                int defaultAdminLanguageID = SettingManager.GetSettingValueInteger("Localization.DefaultAdminLanguageID");
                
                Language language = LanguageManager.GetLanguageByID(defaultAdminLanguageID);
                if (language != null)
                    return language;

                //TODO load language by LanguageCulture 
                Language defaultLanguage = new Language();
                defaultLanguage.LanguageCulture = "en-US";
                defaultLanguage.Published = true;
                defaultLanguage.Name = "English";
                defaultLanguage.DisplayOrder = 1;
                return defaultLanguage;
            }
            set
            {
                if (value != null)
                    SettingManager.SetParam("Localization.DefaultAdminLanguageID", value.LanguageID.ToString());
            }
        }

        /// <summary>
        /// Gets or sets the default site language
        /// </summary>
        public static Language DefaultLanguage
        {
            get
            {
                int defaultLanguageID = SettingManager.GetSettingValueInteger("Localization.DefaultLanguageID");

                Language language = LanguageManager.GetLanguageByID(defaultLanguageID);
                if (language != null)
                    return language;
                Language defaultLanguage = new Language();
                defaultLanguage.LanguageCulture = "en-US";
                defaultLanguage.Published = true;
                defaultLanguage.Name = "English";
                defaultLanguage.DisplayOrder = 1;
                return defaultLanguage;
            }
            set
            {
                if (value != null)
                    SettingManager.SetParam("Localization.DefaultLanguageID", value.LanguageID.ToString());
            }
        }
        #endregion
    }
}
