// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2022 Torsten Schache, Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;
using Gtk;
using Hiperscan.GuiExtensions.Dialog;
using Hiperscan.GtkExtensions.WindowIcons;

namespace Hiperscan.GtkExtensions.StandardDialogs
{
    public abstract class StandardDialog
    {
        protected StandardDialog(Gtk.Window window, DialogArgsTempleate arg)
        {
            this.Parent = window;
            this.Arg = arg;
            this.Message = arg.Message.Replace("&", "&#38;");
            this.Title = arg.Title.Replace("&", "&#38;");
        }

        protected virtual void ShowDialog(Gtk.DialogFlags dialog_flag = DialogFlags.Modal,
                                          Gtk.MessageType message_type = MessageType.Other,
                                          Gtk.ButtonsType button_type = ButtonsType.None)
        {

            Gtk.MessageDialog dialog = new Gtk.MessageDialog(this.Parent,
                                                             dialog_flag,
                                                             message_type,
                                                             button_type,
                                                             this.Message);

            dialog.UseMarkup = this.Arg.UseMarkup;
            this.ExtendDialog(dialog);
            dialog.Title = this.Title;

            ResponseType response = Gtk.ResponseType.None;
            dialog.WindowPosition = Gtk.WindowPosition.CenterOnParent;

            if (this.Arg.TimeOutMilliSeconds > 0)
            {
                GLib.Timeout.Add(this.Arg.TimeOutMilliSeconds, delegate
                {
                    dialog.Destroy();
                    return false;
                });
            }

            dialog.IconList = IconList.GetIconList(this.Arg.IconType);

            response = (Gtk.ResponseType)dialog.Run();
            dialog.Destroy();

            this.Arg.Response = StandardDialog.Convert(response);
        }

        protected virtual void ExtendDialog(Gtk.MessageDialog mdlg)
        {

        }

        public static DialogRespnseType Convert(Gtk.ResponseType response)
        {
            DialogRespnseType dialog_response = DialogRespnseType.None;

            switch (response)
            {
                case Gtk.ResponseType.Yes:
                    dialog_response = DialogRespnseType.Yes;
                    break;

                case Gtk.ResponseType.No:
                    dialog_response = DialogRespnseType.No;
                    break;

                case Gtk.ResponseType.Apply:
                    dialog_response = DialogRespnseType.Apply;
                    break;

                case Gtk.ResponseType.Cancel:
                    dialog_response = DialogRespnseType.Cancel;
                    break;

                case Gtk.ResponseType.DeleteEvent:
                    dialog_response = DialogRespnseType.DeleteEvent;
                    break;

                case Gtk.ResponseType.Ok:
                    dialog_response = DialogRespnseType.Ok;
                    break;

                default:
                    break;
            }

            return dialog_response;
        }

        public static void ShowStandardDialog(Gtk.Window parent, DialogArgsTempleate arg)
        {
            switch (arg.StandardDialogType)
            {
                case StandardDialogType.ErrorWithServiceMessage:
                case StandardDialogType.Error:
                    ErrorDialog.Run(parent, arg);
                    break;

                case StandardDialogType.ErrorWithHelp:
                    ErrorDialogWithHelp.Run(parent, arg);
                    break;

                case StandardDialogType.ErrorWithQuesteion:
                    ErrorDialogWithQuestion.Run(parent, arg);
                    break;

                case StandardDialogType.Info:
                    InfoDialog.Run(parent, arg);
                    break;

                case StandardDialogType.Warning:
                    WarningDialog.Run(parent, arg);
                    break;

                case StandardDialogType.Question:
                    QuestionDialog.Run(parent, arg);
                    break;

                case StandardDialogType.DontShowAgainQuestion:
                    DontShowAgainQuestionDialog.Run(parent, arg);
                    break;

                case StandardDialogType.DontShowAgainInfo:
                    DontShowAgainInfoDialog.Run(parent, arg);
                    break;

                case StandardDialogType.RemindSaving:
                    RemindSaveDialog.Run(parent, arg);
                    break;

                default:
                    break;
            }

            if (arg.WriteErrorToConsole)
            {
                string message = arg.CompleteErrorMessage;
                if (string.IsNullOrEmpty(message) == false)
                    Console.WriteLine(message);
            }
        }

        protected DialogArgsTempleate Arg { get; set; }
        protected string Title { get; set; }
        protected string Message { get; set; }
        protected Gtk.Window Parent { get; set; }
    }
}
