using System.Windows;

namespace MainShell.Common
{
    /// <summary>
    /// 基于多语言枚举消息码的消息弹窗封装。
    /// </summary>
    public static class LocalizedMessageBox
    {
        public static MessageBoxResult Show(MessageKey messageKey)
        {
            return Show(null, messageKey, MessageKey.TitleInfo, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(MessageKey messageKey, MessageKey captionKey)
        {
            return Show(null, messageKey, captionKey, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(MessageKey messageKey, MessageKey captionKey, MessageBoxButton button, MessageBoxImage icon)
        {
            return Show(null, messageKey, captionKey, button, icon, MessageBoxResult.None);
        }

        public static MessageBoxResult Show(Window owner, MessageKey messageKey, MessageKey captionKey, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult = MessageBoxResult.None)
        {
            return MwMessageBox.InvokeOnUiThread(() => ShowCore(owner, messageKey, captionKey, button, icon, defaultResult));
        }

        public static MessageBoxResult ShowFormat(MessageKey messageKey, MessageKey captionKey, MessageBoxButton button, MessageBoxImage icon, params object[] args)
        {
            return MwMessageBox.InvokeOnUiThread(() => ShowFormatCore(messageKey, captionKey, button, icon, args));
        }

        private static MessageBoxResult ShowCore(Window owner, MessageKey messageKey, MessageKey captionKey, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
        {
            string message = LanguageResourceHelper.GetString(messageKey);
            string caption = captionKey == MessageKey.None
                ? string.Empty
                : LanguageResourceHelper.GetString(captionKey);

            return MwMessageBox.Show(owner, message, caption, button, icon, defaultResult);
        }

        private static MessageBoxResult ShowFormatCore(MessageKey messageKey, MessageKey captionKey, MessageBoxButton button, MessageBoxImage icon, object[] args)
        {
            string message = LanguageResourceHelper.Format(messageKey, args);
            string caption = captionKey == MessageKey.None
                ? string.Empty
                : LanguageResourceHelper.GetString(captionKey);

            return MwMessageBox.Show(message, caption, button, icon);
        }
    }
}
