using MahApps.Metro.Controls;
using signotec.STPadLibNet;
using System;
using System.Collections.Generic;
using System.Drawing;
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 Signature.xaml
    /// </summary>
    public partial class Signature : MetroWindow
	{
        public System.Drawing.Bitmap Result { get; set; }
        public byte[] ResultByte { get; set; }
        //private STPadLibControl _stPad = new STPadLibControl();
        System.Windows.Threading.DispatcherTimer confirmedTimer = new System.Windows.Threading.DispatcherTimer();

        Window _owner = null;

        public Signature(DisclaimerType type, string Name = "", string Firma_Abteilung = "", string Kennzeichen_Handynr = "", bool showDisclaimer = true, string ansprechpartner = "", Window owner = null)
        {
            InitializeComponent();

            _owner = owner ?? this;

            confirmedTimer.Interval = TimeSpan.FromMilliseconds(200);
            confirmedTimer.Tick += ConfirmedTimer_Tick;

            try
            {
                //var dev = 1;
                if (signoPad._stPad.DeviceGetCount() <= 0)
                {
                    MessageBox.Show("Kein Unterschriftenpad gefunden\nMeldung 0x04", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                signoPad._stPad = new STPadLibControl();
                STPadLibControlHost.Child = signoPad._stPad;
                signoPad._stPad.ControlMirrorDisplay = MirrorMode.Signature;


                //Add return Value to Bestätigen Button
                switch (type)
                {
                    case DisclaimerType.Parken:
                        signoPad.ShowDisclaimerParken(Name, Firma_Abteilung, Kennzeichen_Handynr, showDisclaimer);
                        break;
                    case DisclaimerType.McFit:
                        signoPad.ShowDisclaimerMcFit(Name, Firma_Abteilung, Kennzeichen_Handynr, showDisclaimer);
                        break;
                    case DisclaimerType.Gruen:
                        signoPad.ShowDisclaimerGruen(Name, Kennzeichen_Handynr, showDisclaimer);
                        break;
                    case DisclaimerType.Rot:
                        signoPad.ShowDisclaimerRot(Name, Kennzeichen_Handynr, showDisclaimer);
                        break;
                    case DisclaimerType.Rueck:
                        signoPad.ShowDisclaimerRueck(ansprechpartner, showDisclaimer);
                        break;
                    default:
                        signoPad.ShowDisclaimerParken(Name, Firma_Abteilung, Kennzeichen_Handynr, showDisclaimer);
                        break;
                }

                confirmedTimer.Start();
            }
            catch (Exception ex)
            {
                Log.WriteLog(ex.ToString());
                MessageBox.Show($"Es ist ein Fehler aufgetreten\n\nInterne Meldung: {ex.Message}");
            }
        }

        private void ConfirmedTimer_Tick(object sender, EventArgs e)
        {
            if (signoPad.SignatureConfirmed)
            {
                STPadLibControlHost.Visibility = Visibility.Collapsed;
                confirmedTimer.Stop();

                if (signoPad.Result != null)
                {
                    imgSignature.Source = Helper.ConvertBitmapToImage(signoPad.Result);
                    btnConfirm.IsEnabled = true;
                }
                else
                {
                    MessageBox.Show("Fehler beim Erfassen der Unterschrift\nBitte erneut versuchen", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                    Log.WriteLog("Fehler bei Erfassen der Unterschrift");
                    //signoPad._stPad.SignatureRetry();
                    this.Close();

                }
            }
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            //System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Tiff;
            //SignatureImageFlag options = signotec.STPadLibNet.SignatureImageFlag.Timestamp | signotec.STPadLibNet.SignatureImageFlag.TimestampRelToImage | signotec.STPadLibNet.SignatureImageFlag.DontSmooth; ;


            //int resolution = 300;
            //int width = 0;
            //int height = 0;
            //int penWidth = 0;
            //System.Drawing.Color penColor = System.Drawing.Color.FromArgb(255, 0, 0, 0);


            try
            {

                //signoPad._stPad.SignatureSaveAsFileEx(path, resolution, width, height, System.Drawing.Imaging.ImageFormat.Tiff, penWidth, penColor, options);

                //ResultByte = DBImageManager.ImageFromFileToByte(signoPad.signaturePath);
                ResultByte = Helper.ConvertImageToByteArray(signoPad.Result);

                MessageBox.Show(_owner, "Unterschrift erfolgreich gespeichert", "Gespeichert", MessageBoxButton.OK, MessageBoxImage.Information);
                //signoPad.CloseConnection();
                this.Close();

            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                //signoPad.CloseConnection();
                this.Close();
            }

        }

        private async void GetSignatureImage()
        {
            //System.Drawing.Bitmap bitmap = SQL.ReadSingleBitmap("");

            //// display image
            //ImageView imageView = new ImageView();
            //imageView.BackgroundImage = Helper.ConvertBitmapToImage(bitmap);
            //imageView.ShowDialog();
        }




        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            signoPad.CloseConnection();            
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            //signoPad.CloseConnection();
            this.Close();
        }


        public enum DisclaimerType
        {
            Parken,
            McFit,
            Gruen,
            Rot,
            Rueck
        }
    }
}
