using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ZKuP
{
    /// <summary>
    /// Interaktionslogik für XMessageBox.xaml
    /// </summary>
    public partial class XMessageBoxWithReturn : MetroWindow
    {
        System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
        int timerCount = 0;
        string _errorMessageIfNothingProvided = "";
        string _Watermark = "";

        public string Result { get; set; }

        public XMessageBoxWithReturn(string Title, string Message, MessageBoxImage image, bool withTimer = true, string Watermark = "", string errorMessageIfNothingProvided = "", bool alwaysOnTop = true)
        {
            InitializeComponent();

            if (withTimer)
            {
                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Tick += Timer_Tick;
                timer.Start();
            }

            _errorMessageIfNothingProvided = errorMessageIfNothingProvided;
            _Watermark = Watermark;

            this.Title = Title;
            this.Message.Text = Message;
            this.Topmost = alwaysOnTop;
            if (!string.IsNullOrWhiteSpace(Watermark)) MahApps.Metro.Controls.TextBoxHelper.SetWatermark(tbTextBox, _Watermark);

            var selectedImage = System.Drawing.SystemIcons.Error;
            switch (image)
            {
                case MessageBoxImage.Error:
                    selectedImage = System.Drawing.SystemIcons.Error;
                    break;
                case MessageBoxImage.Information:
                    selectedImage = System.Drawing.SystemIcons.Information;
                    break;
                case MessageBoxImage.Question:
                    selectedImage = System.Drawing.SystemIcons.Question;
                    break;
                case MessageBoxImage.Warning:
                    selectedImage = System.Drawing.SystemIcons.Warning;
                    break;
                default:
                    selectedImage = System.Drawing.SystemIcons.Error;
                    break;
            }

            this.Image.Source = Helper.ConvertBitmapToImage(selectedImage.ToBitmap());
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            timerCount += 1;
            btnBox.Content = $"OK - {(10 - timerCount).ToString()}";

            if (timerCount >= 10)
                Application.Current.Shutdown();
        }

        private void btnBox_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbTextBox.Text) && !string.IsNullOrWhiteSpace(_errorMessageIfNothingProvided))
            {
                MessageBox.Show($"{_Watermark} muss angegeben werden!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            Result = tbTextBox.Text;

            this.DialogResult = true;
            this.Close();
        }

        private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbTextBox.Text) && !string.IsNullOrWhiteSpace(_errorMessageIfNothingProvided))
            {
                e.Cancel = true;
                MessageBox.Show($"{_Watermark} muss angegeben werden!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else
            {
                Result = tbTextBox.Text;

                this.DialogResult = true;
            }
        }
    }
}
