using System.Windows.Forms;
using QC.QsprShared.GlobalData;
using QC.QsprShared.Trace;

namespace QC.QsprShared.Dialogs;

public class AppMessageDialog
{
	public static bool SuppressWarnings { get; set; }

	public static DialogResult Show(string text, DialogResult defaultResult, MessageDialogType msgdialogType)
	{
		return ShowCore(null, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0, defaultResult, msgdialogType);
	}

	public static DialogResult Show(string text, string caption, DialogResult defaultResult, MessageDialogType msgdialogType)
	{
		return ShowCore(null, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0, defaultResult, msgdialogType);
	}

	public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, DialogResult defaultResult, MessageDialogType msgdialogType)
	{
		return ShowCore(null, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0, defaultResult, msgdialogType);
	}

	public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, DialogResult defaultResult, MessageDialogType msgdialogType)
	{
		return ShowCore(null, text, caption, buttons, icon, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0, defaultResult, msgdialogType);
	}

	public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, DialogResult defaultResult, MessageDialogType msgdialogType)
	{
		return ShowCore(owner, text, caption, buttons, icon, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0, defaultResult, msgdialogType);
	}

	public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, DialogResult defaultResult, MessageDialogType msgdialogType)
	{
		return ShowCore(owner, text, caption, buttons, icon, defaultButton, (MessageBoxOptions)0, defaultResult, msgdialogType);
	}

	public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, DialogResult defaultResult, MessageDialogType msgdialogType)
	{
		return ShowCore(null, text, caption, buttons, icon, defaultButton, (MessageBoxOptions)0, defaultResult, msgdialogType);
	}

	public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions msgOptions, DialogResult defaultResult, MessageDialogType msgdialogType)
	{
		return ShowCore(null, text, caption, buttons, icon, defaultButton, msgOptions, defaultResult, msgdialogType);
	}

	private static DialogResult ShowCore(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions msgOptions, DialogResult result, MessageDialogType msgdialogType)
	{
		if (GlobalStatus.SuppressAllMessagesMode || (SuppressWarnings && (msgdialogType == MessageDialogType.Warning || msgdialogType == MessageDialogType.Information)))
		{
			WriteToTraceMsg(text, result, msgdialogType);
		}
		else
		{
			result = MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, msgOptions);
		}
		return result;
	}

	private static void WriteToTraceMsg(string text, DialogResult result, MessageDialogType msgdialogType)
	{
		switch (msgdialogType)
		{
		case MessageDialogType.CritialError:
			TraceMessage.WriteSuppressedMsg("Suppressed Critical Error Messages.", text + "; Action taken:" + result, TraceMessageType.Error);
			break;
		case MessageDialogType.Exception:
			TraceMessage.WriteSuppressedMsg("Suppressed Exception Messages.", text + "; Action taken:" + result, TraceMessageType.Exception);
			break;
		case MessageDialogType.Warning:
			TraceMessage.WriteSuppressedMsg("Suppressed Warning Messages.", text + "; Action taken:" + result, TraceMessageType.Warning);
			break;
		case MessageDialogType.Error:
			TraceMessage.WriteSuppressedMsg("Suppressed Error Messages.", text + "; Action taken:" + result, TraceMessageType.Error);
			break;
		case MessageDialogType.Information:
			TraceMessage.WriteSuppressedMsg("Suppressed Information Messages.", text + "; Action taken:" + result, TraceMessageType.Warning);
			break;
		case MessageDialogType.CriticalException:
			TraceMessage.WriteSuppressedMsg("Suppressed Critical Exception Messages.", text + "; Action taken:" + result, TraceMessageType.Exception);
			break;
		case MessageDialogType.CriticalWarning:
			TraceMessage.WriteSuppressedMsg("Suppressed Critical Warning Messages.", text + "; Action taken:" + result, TraceMessageType.Warning);
			break;
		case MessageDialogType.UIWarning:
			TraceMessage.WriteSuppressedMsg("Suppressed UI Warning Messages.", text + "; Action taken:" + result, TraceMessageType.Warning);
			break;
		default:
			TraceMessage.WriteSuppressedMsg("Suppressed Error Messages.", text + "; Action taken:" + result, TraceMessageType.Error);
			break;
		}
	}
}
