using signotec.STPadLibNet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using static ZKuP.signoPad;

namespace ZKuP
{
    public class signoPad
    {
        public enum PadModel
        {
            Sigma,
            Zeta,
            Omega,
            Gamma,
            Delta,
            Alpha
        }


        //private static STPadLibControl _stPad = new STPadLibControl();
        private static STPadLibControl sTPad = new STPadLibControl();
        public static STPadLibControl _stPad
        {
            get { return sTPad; }
            set { sTPad = value; }
        }

        private static bool _signatureConfirmed = false;
        public static bool SignatureConfirmed
        {
            get { return _signatureConfirmed; }
            set { _signatureConfirmed = value; }
        }

        public static System.Drawing.Bitmap Result { get; set; }
        //public static string signaturePath { get; private set; }

        private static SignPad[] _signPads = null;
        private static DisplayTarget _storeIdSigning = DisplayTarget.NewStandardStore;
        private static DisplayTarget _storeIdOverlay = DisplayTarget.NewStandardStore;

        private static int _buttonCancelId = -1;
        private static int _buttonRetryId = -1;
        private static int _buttonConfirmId = -1;

        static string _disclaimer = "";


        private static bool GetDevices()
        {
            bool isDeviceAvailable = false;

            try
            {
                //sTPad = new STPadLibControl();
                // get number of connected devices
                int deviceCount = _stPad.DeviceGetCount();

                // erase all entries
                //ListOfDevices.Items.Clear();

                // build list
                if (deviceCount <= 0)
                {   // no devices detected
                    _signPads = null;
                    System.Windows.MessageBox.Show("Kein Unterschriftenpad gefunden!\nMeldung 0x02", "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                    isDeviceAvailable = false;
                }
                else
                {   // build device list
                    _signPads = new SignPad[deviceCount];
                    for (int i = 0; i < deviceCount; i++)
                    {
                        _signPads[i] = new SignPad(_stPad, i);
                        //ListOfDevices.Items.Add(String.Format("Device {0}", i + 1));
                    }
                    //ListOfDevices.IsEnabled = true;

                    // select first element of list
                    //if (deviceCount > 0)
                    //    0 = 0;
                    _stPad.SensorHotSpotPressed += new SensorHotSpotPressedEventHandler(STPad_SensorHotSpotPressed);
                    _stPad.ControlMirrorDisplay = MirrorMode.Signature;

                    isDeviceAvailable = true;
                }

                return isDeviceAvailable;
            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                throw;
            }

        }

        private static void SayThankYou()
        {
            System.Drawing.Bitmap bitmap = null;

            bitmap = Helper.LoadBitmapFromResource("Resources/PadDanke.png");

            //Debug.WriteLine(_stPad.);
            _stPad.DisplaySetImage(0, 0, bitmap);
        }




        public static bool ShowDisclaimerParken(string Name = "", string Firma = "", string Kennzeichen = "", bool showDisclaimer = true)
        {
            SignatureConfirmed = false;
            // display disclaimer and two buttons "Cancel" and "Confirm"
            if (!GetDevices())
                return false;
            
            try
            {
                //_stPad.DeviceOpen(_signPads.Length - 1, true);
                _stPad.DeviceOpen(0, true);

                _stPad.DisplaySetStandbyImage(Helper.LoadBitmapFromResource("Resources/db-logo.png"));
                // clear all hot spots
                ClearHotSpots();

                // clear signature window
                _stPad.SensorClearSignRect();

                
                // erase LCD and background buffer
                _stPad.DisplayErase();

                var nCapabilities = _stPad.DeviceGetCapabilities(0);
                if (nCapabilities != null && nCapabilities.HasBacklight)
                    _stPad.DisplaySetBacklight(BacklightMode.On);

                // set font
                float fontSize = 0;
                switch (_signPads[0].PadModel)
                {
                    case PadModel.Sigma:
                    case PadModel.Zeta:
                        fontSize = 20;
                        break;
                    case PadModel.Omega:
                        fontSize = 40;
                        break;
                    case PadModel.Gamma:
                        fontSize = 45;
                        break;
                    case PadModel.Delta:
                    case PadModel.Alpha:
                        fontSize = 60;
                        break;
                }
                _stPad.DisplaySetFont(new System.Drawing.Font("Arial", fontSize));

                switch (_signPads[0].PadModel)
                {
                    case PadModel.Sigma:
                    case PadModel.Zeta:
                        // do all the following drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // load button bitmaps and set hot spots
                        // "Cancel" button
                        System.Drawing.Bitmap button = Helper.LoadBitmapFromResource("Resources/Cancel BW.png");
                        int x = 20;
                        int y = _stPad.DisplayHeight - button.Height - 7;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonCancelId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // "Confirm" button
                        button = Helper.LoadBitmapFromResource("Resources/OK BW.png");
                        x = _stPad.DisplayWidth - button.Width - 20;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        if (showDisclaimer) // display disclaimer
                        {
                            if (Firma != "" && Name != "" && Kennzeichen != "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit den Erhalt eines Parkschein durch den Pförtner und das ich über die Parkordnung, sowie deren Folgen bei Missachtung, ausreichend informiert wurde\n\nFirma / Besucher:   {Firma}\nVerantwortlicher:   {Name}\nKennzeichen:   {Kennzeichen}");
                            else if (Firma != "" && Name != "" && Kennzeichen == "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit den Erhalt eines Parkschein durch den Pförtner und das ich über die Parkordnung, sowie deren Folgen bei Missachtung, ausreichend informiert wurde\n\nFirma / Besucher:   {Firma}\nVerantwortlicher:   {Name}");
                            else
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit den Erhalt eines Parkschein durch den Pförtner und das ich über die Parkordnung, sowie deren Folgen bei Missachtung, ausreichend informiert wurde\n\nFirma / Besucher:   {Firma}");
                        }
                        else
                        {
                            if (Firma != "" && Name != "" && Kennzeichen != "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Firma / Besucher:   {Firma}\nVerantwortlicher:   {Name}\nKennzeichen:   {Kennzeichen}");
                            else if (Firma != "" && Name != "" && Kennzeichen == "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Firma / Besucher:   {Firma}\nVerantwortlicher:   {Name}");
                            else
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Firma / Besucher:   {Firma}");
                        }


                        break;
                    case PadModel.Omega:
                    case PadModel.Gamma:
                    case PadModel.Delta:
                        if (_signPads[0].FastConnection)
                            // fast connection: do all drawing operations in the overlay buffer
                            _storeIdOverlay = _stPad.DisplaySetTarget(DisplayTarget.OverlayBuffer);
                        else
                            // do all the following drawing operations in the permanent memory
                            _storeIdOverlay = _stPad.DisplaySetTarget(_storeIdOverlay);

                        // load button bitmaps and set hot spots for toolbar
                        // "Cancel" button
                        button = Helper.LoadBitmapFromResource("Resources/Cancel RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 24;
                                break;
                            default:
                                x = 45;
                                break;
                        }
                        y = _stPad.DisplayHeight - button.Height - 14;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonCancelId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // "Confirm" button
                        button = Helper.LoadBitmapFromResource("Resources/OK RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 234;
                                break;
                            case PadModel.Gamma:
                                x = 315;
                                break;
                            case PadModel.Delta:
                                x = 555;
                                break;
                        }
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // Scroll buttons
                        button = Helper.LoadBitmapFromResource("Resources/Scroll RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 444;
                                break;
                            case PadModel.Gamma:
                                x = 585;
                                break;
                            case PadModel.Delta:
                                x = 1065;
                                break;
                        }
                        _stPad.DisplaySetImage(x, y, button);
                        _stPad.SensorAddScrollHotSpot(x, y, 66, 66, ScrollOption.ScrollDown);
                        x += 104;
                        _stPad.SensorAddScrollHotSpot(x, y, 66, 66, ScrollOption.ScrollUp);

                        if (!_signPads[0].FastConnection)
                        {
                            // do all the following drawing operations in the overlay buffer
                            _stPad.DisplaySetTarget(DisplayTarget.OverlayBuffer);

                            // copy stored image to overlay buffer
                            _stPad.DisplaySetImageFromStore(_storeIdOverlay);
                        }

                        // do all the following drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // draw disclaimer
                        x = 10;
                        y = 10;
                        int size = _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 80, signotec.STPadLibNet.TextAlignment.Left, _disclaimer);

                        // use font size of the disclaimer text
                        if (size != (int)fontSize)
                            _stPad.DisplaySetFont(new System.Drawing.Font("Arial", size, System.Drawing.FontStyle.Regular));

                        // set scroll text
                        y = _stPad.DisplayHeight - 80;
                        _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 100, signotec.STPadLibNet.TextAlignment.Left, "Congratulations! If you can read this text you have found the scroll buttons!");
                        if (_signPads[0].PadModel == PadModel.Delta)
                        {
                            y += _stPad.DisplayHeight;
                            _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 200, 0, "Doesn't the Delta have an impressive large image buffer?");
                            const int x2 = 320;
                            while ((y + _stPad.DisplayHeight) < _stPad.DisplayTargetHeight)
                            {
                                y += _stPad.DisplayHeight;
                                _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 200, 0, String.Format("You've reached line {0} of {1}!", y, _stPad.DisplayTargetHeight));

                                if ((y + _stPad.DisplayHeight) < _stPad.DisplayTargetHeight)
                                {
                                    y += _stPad.DisplayHeight;
                                    _stPad.DisplaySetTextInRect(x2, y, _stPad.DisplayWidth - 2 * x2, 200, signotec.STPadLibNet.TextAlignment.CenterCenteredVertically, "Click me, I'm a scrollable button!");
                                    _stPad.SensorAddScrollHotSpot(x2, y, _stPad.DisplayWidth - 2 * x2, 200, ScrollOption.Scrollable);
                                }
                            }
                        }

                        // set end text
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                y = _stPad.DisplayTargetHeight - 120;
                                break;
                            case PadModel.Gamma:
                                y = _stPad.DisplayTargetHeight - 140;
                                break;
                            case PadModel.Delta:
                                y = _stPad.DisplayTargetHeight - 160;
                                break;
                        }
                        _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 60, signotec.STPadLibNet.TextAlignment.Left, "You have scrolled to the end of this text!");

                        // set overlay rect
                        y = _stPad.DisplayHeight - 80;
                        _stPad.DisplaySetOverlayRect(0, y, _stPad.DisplayWidth, 80);
                        break;
                }

                // do all drawing operations on the LCD
                _stPad.DisplaySetTarget(DisplayTarget.ForegroundBuffer);

                // draw buffered image
                _stPad.DisplaySetImageFromStore(DisplayTarget.BackgroundBuffer);

                // set complete buffer for scrolling
                _stPad.SensorSetScrollArea(0, 0, 0, 0);

                if (_signPads[0].SupportsPenScrolling)
                    // enable pen scrolling
                    _stPad.SensorSetPenScrollingEnabled(true);

                //ButtonRetry.IsEnabled = false;
            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                //throw;
                return false;
            }
            finally
            {
                //this.Cursor = Cursors.Arrow;
            }
            return true;
        }

        public static bool ShowDisclaimerMcFit(string Name = "", string Abteilung = "", string Handynr = "", bool showDisclaimer = true)
        {
            SignatureConfirmed = false;
            // display disclaimer and two buttons "Cancel" and "Confirm"
            if (!GetDevices())
                return false;

            try
            {
                //_stPad.DeviceOpen(_signPads.Length - 1, true);
                _stPad.DeviceOpen(0, true);

                _stPad.DisplaySetStandbyImage(Helper.LoadBitmapFromResource("Resources/db-logo.png"));
                // clear all hot spots
                ClearHotSpots();

                // clear signature window
                _stPad.SensorClearSignRect();


                // erase LCD and background buffer
                _stPad.DisplayErase();

                var nCapabilities = _stPad.DeviceGetCapabilities(0);
                if (nCapabilities != null && nCapabilities.HasBacklight)
                    _stPad.DisplaySetBacklight(BacklightMode.On);

                // set font
                float fontSize = 0;
                switch (_signPads[0].PadModel)
                {
                    case PadModel.Sigma:
                    case PadModel.Zeta:
                        fontSize = 20;
                        break;
                    case PadModel.Omega:
                        fontSize = 40;
                        break;
                    case PadModel.Gamma:
                        fontSize = 45;
                        break;
                    case PadModel.Delta:
                    case PadModel.Alpha:
                        fontSize = 60;
                        break;
                }
                _stPad.DisplaySetFont(new System.Drawing.Font("Arial", fontSize));

                switch (_signPads[0].PadModel)
                {
                    case PadModel.Sigma:
                    case PadModel.Zeta:
                        // do all the following drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // load button bitmaps and set hot spots
                        // "Cancel" button
                        System.Drawing.Bitmap button = Helper.LoadBitmapFromResource("Resources/Cancel BW.png");
                        int x = 20;
                        int y = _stPad.DisplayHeight - button.Height - 7;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonCancelId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // "Confirm" button
                        button = Helper.LoadBitmapFromResource("Resources/OK BW.png");
                        x = _stPad.DisplayWidth - button.Width - 20;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        if (showDisclaimer) // display disclaimer
                        {
                            if (Abteilung != "" && Name != "" && Handynr != "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit dass ich die Mitgliedskarte inkl. Nutzungsbedingungen von McFit erhalten habe\n\nAbteilung:   {Abteilung}\nName:   {Name}\nHandynr:   {Handynr}");
                            else if (Abteilung != "" && Name != "" && Handynr == "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit dass ich die Mitgliedskarte inkl. Nutzungsbedingungen von McFit erhalten habe\n\nAbteilung:   {Abteilung}\nName:   {Name}");
                            else
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit dass ich die Mitgliedskarte inkl. Nutzungsbedingungen von McFit erhalten habe\n\nAbteilung:   {Abteilung}");
                        }
                        else
                        {
                            if (Abteilung != "" && Name != "" && Handynr != "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Abteilung:   {Abteilung}\nName:   {Name}\nHandynr:   {Handynr}");
                            else if (Abteilung != "" && Name != "" && Handynr == "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Abteilung:   {Abteilung}\nName:   {Name}");
                            else
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Abteilung:   {Abteilung}");
                        }


                        break;
                    case PadModel.Omega:
                    case PadModel.Gamma:
                    case PadModel.Delta:
                        if (_signPads[0].FastConnection)
                            // fast connection: do all drawing operations in the overlay buffer
                            _storeIdOverlay = _stPad.DisplaySetTarget(DisplayTarget.OverlayBuffer);
                        else
                            // do all the following drawing operations in the permanent memory
                            _storeIdOverlay = _stPad.DisplaySetTarget(_storeIdOverlay);

                        // load button bitmaps and set hot spots for toolbar
                        // "Cancel" button
                        button = Helper.LoadBitmapFromResource("Resources/Cancel RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 24;
                                break;
                            default:
                                x = 45;
                                break;
                        }
                        y = _stPad.DisplayHeight - button.Height - 14;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonCancelId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // "Confirm" button
                        button = Helper.LoadBitmapFromResource("Resources/OK RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 234;
                                break;
                            case PadModel.Gamma:
                                x = 315;
                                break;
                            case PadModel.Delta:
                                x = 555;
                                break;
                        }
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // Scroll buttons
                        button = Helper.LoadBitmapFromResource("Resources/Scroll RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 444;
                                break;
                            case PadModel.Gamma:
                                x = 585;
                                break;
                            case PadModel.Delta:
                                x = 1065;
                                break;
                        }
                        _stPad.DisplaySetImage(x, y, button);
                        _stPad.SensorAddScrollHotSpot(x, y, 66, 66, ScrollOption.ScrollDown);
                        x += 104;
                        _stPad.SensorAddScrollHotSpot(x, y, 66, 66, ScrollOption.ScrollUp);

                        if (!_signPads[0].FastConnection)
                        {
                            // do all the following drawing operations in the overlay buffer
                            _stPad.DisplaySetTarget(DisplayTarget.OverlayBuffer);

                            // copy stored image to overlay buffer
                            _stPad.DisplaySetImageFromStore(_storeIdOverlay);
                        }

                        // do all the following drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // draw disclaimer
                        x = 10;
                        y = 10;
                        int size = _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 80, signotec.STPadLibNet.TextAlignment.Left, _disclaimer);

                        // use font size of the disclaimer text
                        if (size != (int)fontSize)
                            _stPad.DisplaySetFont(new System.Drawing.Font("Arial", size, System.Drawing.FontStyle.Regular));

                        // set scroll text
                        y = _stPad.DisplayHeight - 80;
                        _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 100, signotec.STPadLibNet.TextAlignment.Left, "Congratulations! If you can read this text you have found the scroll buttons!");
                        if (_signPads[0].PadModel == PadModel.Delta)
                        {
                            y += _stPad.DisplayHeight;
                            _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 200, 0, "Doesn't the Delta have an impressive large image buffer?");
                            const int x2 = 320;
                            while ((y + _stPad.DisplayHeight) < _stPad.DisplayTargetHeight)
                            {
                                y += _stPad.DisplayHeight;
                                _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 200, 0, String.Format("You've reached line {0} of {1}!", y, _stPad.DisplayTargetHeight));

                                if ((y + _stPad.DisplayHeight) < _stPad.DisplayTargetHeight)
                                {
                                    y += _stPad.DisplayHeight;
                                    _stPad.DisplaySetTextInRect(x2, y, _stPad.DisplayWidth - 2 * x2, 200, signotec.STPadLibNet.TextAlignment.CenterCenteredVertically, "Click me, I'm a scrollable button!");
                                    _stPad.SensorAddScrollHotSpot(x2, y, _stPad.DisplayWidth - 2 * x2, 200, ScrollOption.Scrollable);
                                }
                            }
                        }

                        // set end text
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                y = _stPad.DisplayTargetHeight - 120;
                                break;
                            case PadModel.Gamma:
                                y = _stPad.DisplayTargetHeight - 140;
                                break;
                            case PadModel.Delta:
                                y = _stPad.DisplayTargetHeight - 160;
                                break;
                        }
                        _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 60, signotec.STPadLibNet.TextAlignment.Left, "You have scrolled to the end of this text!");

                        // set overlay rect
                        y = _stPad.DisplayHeight - 80;
                        _stPad.DisplaySetOverlayRect(0, y, _stPad.DisplayWidth, 80);
                        break;
                }

                // do all drawing operations on the LCD
                _stPad.DisplaySetTarget(DisplayTarget.ForegroundBuffer);

                // draw buffered image
                _stPad.DisplaySetImageFromStore(DisplayTarget.BackgroundBuffer);

                // set complete buffer for scrolling
                _stPad.SensorSetScrollArea(0, 0, 0, 0);

                if (_signPads[0].SupportsPenScrolling)
                    // enable pen scrolling
                    _stPad.SensorSetPenScrollingEnabled(true);

                //ButtonRetry.IsEnabled = false;
            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                //throw;
                return false;
            }
            finally
            {
                //this.Cursor = Cursors.Arrow;
            }
            return true;
        }

        public static bool ShowDisclaimerRot(string Name = "", string Handynr = "", bool showDisclaimer = true)
        {
            SignatureConfirmed = false;
            // display disclaimer and two buttons "Cancel" and "Confirm"
            if (!GetDevices())
                return false;

            try
            {
                //_stPad.DeviceOpen(_signPads.Length - 1, true);
                _stPad.DeviceOpen(0, true);

                _stPad.DisplaySetStandbyImage(Helper.LoadBitmapFromResource("Resources/db-logo.png"));
                // clear all hot spots
                ClearHotSpots();

                // clear signature window
                _stPad.SensorClearSignRect();


                // erase LCD and background buffer
                _stPad.DisplayErase();

                var nCapabilities = _stPad.DeviceGetCapabilities(0);
                if (nCapabilities != null && nCapabilities.HasBacklight)
                    _stPad.DisplaySetBacklight(BacklightMode.On);

                // set font
                float fontSize = 0;
                switch (_signPads[0].PadModel)
                {
                    case PadModel.Sigma:
                    case PadModel.Zeta:
                        fontSize = 20;
                        break;
                    case PadModel.Omega:
                        fontSize = 40;
                        break;
                    case PadModel.Gamma:
                        fontSize = 45;
                        break;
                    case PadModel.Delta:
                    case PadModel.Alpha:
                        fontSize = 60;
                        break;
                }
                _stPad.DisplaySetFont(new System.Drawing.Font("Arial", fontSize));

                switch (_signPads[0].PadModel)
                {
                    case PadModel.Sigma:
                    case PadModel.Zeta:
                        // do all the following drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // load button bitmaps and set hot spots
                        // "Cancel" button
                        System.Drawing.Bitmap button = Helper.LoadBitmapFromResource("Resources/Cancel BW.png");
                        int x = 20;
                        int y = _stPad.DisplayHeight - button.Height - 7;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonCancelId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // "Confirm" button
                        button = Helper.LoadBitmapFromResource("Resources/OK BW.png");
                        x = _stPad.DisplayWidth - button.Width - 20;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        if (showDisclaimer) // display disclaimer
                        {
                            if (Name != "" && Handynr != "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit den Erhalt des Sicherheitsflyer und einer roten Gastkarte vom Pförtner\n\nBenutzer:   {Name}\nHandynr:   {Handynr}");
                            else if (Name != "" && Handynr == "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit den Erhalt des Sicherheitsflyer und einer roten Gastkarte vom Pförtner\n\nBenutzer:   {Name}");
                            else
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit den Erhalt des Sicherheitsflyer und einer roten Gastkarte vom Pförtner\n\nBenutzer:   {Name}");
                        }
                        else
                        {
                            if (Name != "" && Handynr != "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Benutzer:   {Name}\nHandynr:   {Handynr}");
                            else if (Name != "" && Handynr == "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Benutzer:   {Name}");
                            else
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Benutzer:   {Name}");
                        }


                        break;
                    case PadModel.Omega:
                    case PadModel.Gamma:
                    case PadModel.Delta:
                        if (_signPads[0].FastConnection)
                            // fast connection: do all drawing operations in the overlay buffer
                            _storeIdOverlay = _stPad.DisplaySetTarget(DisplayTarget.OverlayBuffer);
                        else
                            // do all the following drawing operations in the permanent memory
                            _storeIdOverlay = _stPad.DisplaySetTarget(_storeIdOverlay);

                        // load button bitmaps and set hot spots for toolbar
                        // "Cancel" button
                        button = Helper.LoadBitmapFromResource("Resources/Cancel RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 24;
                                break;
                            default:
                                x = 45;
                                break;
                        }
                        y = _stPad.DisplayHeight - button.Height - 14;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonCancelId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // "Confirm" button
                        button = Helper.LoadBitmapFromResource("Resources/OK RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 234;
                                break;
                            case PadModel.Gamma:
                                x = 315;
                                break;
                            case PadModel.Delta:
                                x = 555;
                                break;
                        }
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // Scroll buttons
                        button = Helper.LoadBitmapFromResource("Resources/Scroll RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 444;
                                break;
                            case PadModel.Gamma:
                                x = 585;
                                break;
                            case PadModel.Delta:
                                x = 1065;
                                break;
                        }
                        _stPad.DisplaySetImage(x, y, button);
                        _stPad.SensorAddScrollHotSpot(x, y, 66, 66, ScrollOption.ScrollDown);
                        x += 104;
                        _stPad.SensorAddScrollHotSpot(x, y, 66, 66, ScrollOption.ScrollUp);

                        if (!_signPads[0].FastConnection)
                        {
                            // do all the following drawing operations in the overlay buffer
                            _stPad.DisplaySetTarget(DisplayTarget.OverlayBuffer);

                            // copy stored image to overlay buffer
                            _stPad.DisplaySetImageFromStore(_storeIdOverlay);
                        }

                        // do all the following drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // draw disclaimer
                        x = 10;
                        y = 10;
                        int size = _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 80, signotec.STPadLibNet.TextAlignment.Left, _disclaimer);

                        // use font size of the disclaimer text
                        if (size != (int)fontSize)
                            _stPad.DisplaySetFont(new System.Drawing.Font("Arial", size, System.Drawing.FontStyle.Regular));

                        // set scroll text
                        y = _stPad.DisplayHeight - 80;
                        _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 100, signotec.STPadLibNet.TextAlignment.Left, "Congratulations! If you can read this text you have found the scroll buttons!");
                        if (_signPads[0].PadModel == PadModel.Delta)
                        {
                            y += _stPad.DisplayHeight;
                            _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 200, 0, "Doesn't the Delta have an impressive large image buffer?");
                            const int x2 = 320;
                            while ((y + _stPad.DisplayHeight) < _stPad.DisplayTargetHeight)
                            {
                                y += _stPad.DisplayHeight;
                                _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 200, 0, String.Format("You've reached line {0} of {1}!", y, _stPad.DisplayTargetHeight));

                                if ((y + _stPad.DisplayHeight) < _stPad.DisplayTargetHeight)
                                {
                                    y += _stPad.DisplayHeight;
                                    _stPad.DisplaySetTextInRect(x2, y, _stPad.DisplayWidth - 2 * x2, 200, signotec.STPadLibNet.TextAlignment.CenterCenteredVertically, "Click me, I'm a scrollable button!");
                                    _stPad.SensorAddScrollHotSpot(x2, y, _stPad.DisplayWidth - 2 * x2, 200, ScrollOption.Scrollable);
                                }
                            }
                        }

                        // set end text
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                y = _stPad.DisplayTargetHeight - 120;
                                break;
                            case PadModel.Gamma:
                                y = _stPad.DisplayTargetHeight - 140;
                                break;
                            case PadModel.Delta:
                                y = _stPad.DisplayTargetHeight - 160;
                                break;
                        }
                        _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 60, signotec.STPadLibNet.TextAlignment.Left, "You have scrolled to the end of this text!");

                        // set overlay rect
                        y = _stPad.DisplayHeight - 80;
                        _stPad.DisplaySetOverlayRect(0, y, _stPad.DisplayWidth, 80);
                        break;
                }

                // do all drawing operations on the LCD
                _stPad.DisplaySetTarget(DisplayTarget.ForegroundBuffer);

                // draw buffered image
                _stPad.DisplaySetImageFromStore(DisplayTarget.BackgroundBuffer);

                // set complete buffer for scrolling
                _stPad.SensorSetScrollArea(0, 0, 0, 0);

                if (_signPads[0].SupportsPenScrolling)
                    // enable pen scrolling
                    _stPad.SensorSetPenScrollingEnabled(true);

                //ButtonRetry.IsEnabled = false;
            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                //throw;
                return false;
            }
            finally
            {
                //this.Cursor = Cursors.Arrow;
            }
            return true;
        }

        public static bool ShowDisclaimerGruen(string Name = "", string Handynr = "", bool showDisclaimer = true)
        {
            SignatureConfirmed = false;
            // display disclaimer and two buttons "Cancel" and "Confirm"
            if (!GetDevices())
                return false;

            try
            {
                //_stPad.DeviceOpen(_signPads.Length - 1, true);
                _stPad.DeviceOpen(0, true);

                _stPad.DisplaySetStandbyImage(Helper.LoadBitmapFromResource("Resources/db-logo.png"));
                // clear all hot spots
                ClearHotSpots();

                // clear signature window
                _stPad.SensorClearSignRect();


                // erase LCD and background buffer
                _stPad.DisplayErase();

                var nCapabilities = _stPad.DeviceGetCapabilities(0);
                if (nCapabilities != null && nCapabilities.HasBacklight)
                    _stPad.DisplaySetBacklight(BacklightMode.On);

                // set font
                float fontSize = 0;
                switch (_signPads[0].PadModel)
                {
                    case PadModel.Sigma:
                    case PadModel.Zeta:
                        fontSize = 20;
                        break;
                    case PadModel.Omega:
                        fontSize = 40;
                        break;
                    case PadModel.Gamma:
                        fontSize = 45;
                        break;
                    case PadModel.Delta:
                    case PadModel.Alpha:
                        fontSize = 60;
                        break;
                }
                _stPad.DisplaySetFont(new System.Drawing.Font("Arial", fontSize));

                switch (_signPads[0].PadModel)
                {
                    case PadModel.Sigma:
                    case PadModel.Zeta:
                        // do all the following drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // load button bitmaps and set hot spots
                        // "Cancel" button
                        System.Drawing.Bitmap button = Helper.LoadBitmapFromResource("Resources/Cancel BW.png");
                        int x = 20;
                        int y = _stPad.DisplayHeight - button.Height - 7;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonCancelId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // "Confirm" button
                        button = Helper.LoadBitmapFromResource("Resources/OK BW.png");
                        x = _stPad.DisplayWidth - button.Width - 20;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        if (showDisclaimer) // display disclaimer
                        {
                            if (Name != "" && Handynr != "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit dass ich eine Einweisung vom Fremdfirmenkoordinator erhalten habe\n\nBenutzer:   {Name}\nHandynr:   {Handynr}");
                            else if (Name != "" && Handynr == "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit dass ich eine Einweisung vom Fremdfirmenkoordinator erhalten habe\n\nBenutzer:   {Name}");
                            else
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit dass ich eine Einweisung vom Fremdfirmenkoordinator erhalten habe\n\nBenutzer:   {Name}");
                        }
                        else
                        {
                            if (Name != "" && Handynr != "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Benutzer:   {Name}\nHandynr:   {Handynr}");
                            else if (Name != "" && Handynr == "")
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Benutzer:   {Name}");
                            else
                                _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Benutzer:   {Name}");
                        }


                        break;
                    case PadModel.Omega:
                    case PadModel.Gamma:
                    case PadModel.Delta:
                        if (_signPads[0].FastConnection)
                            // fast connection: do all drawing operations in the overlay buffer
                            _storeIdOverlay = _stPad.DisplaySetTarget(DisplayTarget.OverlayBuffer);
                        else
                            // do all the following drawing operations in the permanent memory
                            _storeIdOverlay = _stPad.DisplaySetTarget(_storeIdOverlay);

                        // load button bitmaps and set hot spots for toolbar
                        // "Cancel" button
                        button = Helper.LoadBitmapFromResource("Resources/Cancel RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 24;
                                break;
                            default:
                                x = 45;
                                break;
                        }
                        y = _stPad.DisplayHeight - button.Height - 14;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonCancelId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // "Confirm" button
                        button = Helper.LoadBitmapFromResource("Resources/OK RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 234;
                                break;
                            case PadModel.Gamma:
                                x = 315;
                                break;
                            case PadModel.Delta:
                                x = 555;
                                break;
                        }
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // Scroll buttons
                        button = Helper.LoadBitmapFromResource("Resources/Scroll RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 444;
                                break;
                            case PadModel.Gamma:
                                x = 585;
                                break;
                            case PadModel.Delta:
                                x = 1065;
                                break;
                        }
                        _stPad.DisplaySetImage(x, y, button);
                        _stPad.SensorAddScrollHotSpot(x, y, 66, 66, ScrollOption.ScrollDown);
                        x += 104;
                        _stPad.SensorAddScrollHotSpot(x, y, 66, 66, ScrollOption.ScrollUp);

                        if (!_signPads[0].FastConnection)
                        {
                            // do all the following drawing operations in the overlay buffer
                            _stPad.DisplaySetTarget(DisplayTarget.OverlayBuffer);

                            // copy stored image to overlay buffer
                            _stPad.DisplaySetImageFromStore(_storeIdOverlay);
                        }

                        // do all the following drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // draw disclaimer
                        x = 10;
                        y = 10;
                        int size = _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 80, signotec.STPadLibNet.TextAlignment.Left, _disclaimer);

                        // use font size of the disclaimer text
                        if (size != (int)fontSize)
                            _stPad.DisplaySetFont(new System.Drawing.Font("Arial", size, System.Drawing.FontStyle.Regular));

                        // set scroll text
                        y = _stPad.DisplayHeight - 80;
                        _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 100, signotec.STPadLibNet.TextAlignment.Left, "Congratulations! If you can read this text you have found the scroll buttons!");
                        if (_signPads[0].PadModel == PadModel.Delta)
                        {
                            y += _stPad.DisplayHeight;
                            _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 200, 0, "Doesn't the Delta have an impressive large image buffer?");
                            const int x2 = 320;
                            while ((y + _stPad.DisplayHeight) < _stPad.DisplayTargetHeight)
                            {
                                y += _stPad.DisplayHeight;
                                _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 200, 0, String.Format("You've reached line {0} of {1}!", y, _stPad.DisplayTargetHeight));

                                if ((y + _stPad.DisplayHeight) < _stPad.DisplayTargetHeight)
                                {
                                    y += _stPad.DisplayHeight;
                                    _stPad.DisplaySetTextInRect(x2, y, _stPad.DisplayWidth - 2 * x2, 200, signotec.STPadLibNet.TextAlignment.CenterCenteredVertically, "Click me, I'm a scrollable button!");
                                    _stPad.SensorAddScrollHotSpot(x2, y, _stPad.DisplayWidth - 2 * x2, 200, ScrollOption.Scrollable);
                                }
                            }
                        }

                        // set end text
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                y = _stPad.DisplayTargetHeight - 120;
                                break;
                            case PadModel.Gamma:
                                y = _stPad.DisplayTargetHeight - 140;
                                break;
                            case PadModel.Delta:
                                y = _stPad.DisplayTargetHeight - 160;
                                break;
                        }
                        _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 60, signotec.STPadLibNet.TextAlignment.Left, "You have scrolled to the end of this text!");

                        // set overlay rect
                        y = _stPad.DisplayHeight - 80;
                        _stPad.DisplaySetOverlayRect(0, y, _stPad.DisplayWidth, 80);
                        break;
                }

                // do all drawing operations on the LCD
                _stPad.DisplaySetTarget(DisplayTarget.ForegroundBuffer);

                // draw buffered image
                _stPad.DisplaySetImageFromStore(DisplayTarget.BackgroundBuffer);

                // set complete buffer for scrolling
                _stPad.SensorSetScrollArea(0, 0, 0, 0);

                if (_signPads[0].SupportsPenScrolling)
                    // enable pen scrolling
                    _stPad.SensorSetPenScrollingEnabled(true);

                //ButtonRetry.IsEnabled = false;
            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                //throw;
                return false;
            }
            finally
            {
                //this.Cursor = Cursors.Arrow;
            }
            return true;
        }

        public static bool ShowDisclaimerRueck(string Rueckgebender = "", bool showDisclaimer = true)
        {
            SignatureConfirmed = false;
            // display disclaimer and two buttons "Cancel" and "Confirm"
            if (!GetDevices())
                return false;

            try
            {
                //_stPad.DeviceOpen(_signPads.Length - 1, true);
                _stPad.DeviceOpen(0, true);

                _stPad.DisplaySetStandbyImage(Helper.LoadBitmapFromResource("Resources/db-logo.png"));
                // clear all hot spots
                ClearHotSpots();

                // clear signature window
                _stPad.SensorClearSignRect();


                // erase LCD and background buffer
                _stPad.DisplayErase();

                var nCapabilities = _stPad.DeviceGetCapabilities(0);
                if (nCapabilities != null && nCapabilities.HasBacklight)
                    _stPad.DisplaySetBacklight(BacklightMode.On);

                // set font
                float fontSize = 0;
                switch (_signPads[0].PadModel)
                {
                    case PadModel.Sigma:
                    case PadModel.Zeta:
                        fontSize = 20;
                        break;
                    case PadModel.Omega:
                        fontSize = 40;
                        break;
                    case PadModel.Gamma:
                        fontSize = 45;
                        break;
                    case PadModel.Delta:
                    case PadModel.Alpha:
                        fontSize = 60;
                        break;
                }
                _stPad.DisplaySetFont(new System.Drawing.Font("Arial", fontSize));

                switch (_signPads[0].PadModel)
                {
                    case PadModel.Sigma:
                    case PadModel.Zeta:
                        // do all the following drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // load button bitmaps and set hot spots
                        // "Cancel" button
                        System.Drawing.Bitmap button = Helper.LoadBitmapFromResource("Resources/Cancel BW.png");
                        int x = 20;
                        int y = _stPad.DisplayHeight - button.Height - 7;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonCancelId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // "Confirm" button
                        button = Helper.LoadBitmapFromResource("Resources/OK BW.png");
                        x = _stPad.DisplayWidth - button.Width - 20;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);


                        if (Rueckgebender != "")
                            _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit dass ich alle an die Firma/Besucher ausgegebenen Karten vollständig zurückgebe\n\nBenutzer:   {Rueckgebender}");
                        else
                        {
                            string Rueckgeber = "";
                            //MessageBox.Show("Rückgebende Person muss angegeben werden", "Fehlende Daten", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            XMessageBoxWithReturn messageBox = new XMessageBoxWithReturn("Fehlende Daten", "Rückgebende Person muss angegeben werden", System.Windows.MessageBoxImage.Exclamation, false, "Rückgebender", "Rückgebender muss angegeben werden!");
                            
                            if (messageBox.ShowDialog() == true)
                            {
                                Rueckgeber = messageBox.Result;
                            }

                            _stPad.DisplaySetTextInRect(10, 10, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 60, signotec.STPadLibNet.TextAlignment.Left, $"Ich bestätige hiermit dass ich alle an die Firma/Besucher ausgegebenen Karten vollständig zurückgebe\n\nBenutzer:   {Rueckgeber}");
                        }

                        break;
                    case PadModel.Omega:
                    case PadModel.Gamma:
                    case PadModel.Delta:
                        if (_signPads[0].FastConnection)
                            // fast connection: do all drawing operations in the overlay buffer
                            _storeIdOverlay = _stPad.DisplaySetTarget(DisplayTarget.OverlayBuffer);
                        else
                            // do all the following drawing operations in the permanent memory
                            _storeIdOverlay = _stPad.DisplaySetTarget(_storeIdOverlay);

                        // load button bitmaps and set hot spots for toolbar
                        // "Cancel" button
                        button = Helper.LoadBitmapFromResource("Resources/Cancel RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 24;
                                break;
                            default:
                                x = 45;
                                break;
                        }
                        y = _stPad.DisplayHeight - button.Height - 14;
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonCancelId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // "Confirm" button
                        button = Helper.LoadBitmapFromResource("Resources/OK RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 234;
                                break;
                            case PadModel.Gamma:
                                x = 315;
                                break;
                            case PadModel.Delta:
                                x = 555;
                                break;
                        }
                        _stPad.DisplaySetImage(x, y, button);
                        _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, button.Width, button.Height);

                        // Scroll buttons
                        button = Helper.LoadBitmapFromResource("Resources/Scroll RGB.png");
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                x = 444;
                                break;
                            case PadModel.Gamma:
                                x = 585;
                                break;
                            case PadModel.Delta:
                                x = 1065;
                                break;
                        }
                        _stPad.DisplaySetImage(x, y, button);
                        _stPad.SensorAddScrollHotSpot(x, y, 66, 66, ScrollOption.ScrollDown);
                        x += 104;
                        _stPad.SensorAddScrollHotSpot(x, y, 66, 66, ScrollOption.ScrollUp);

                        if (!_signPads[0].FastConnection)
                        {
                            // do all the following drawing operations in the overlay buffer
                            _stPad.DisplaySetTarget(DisplayTarget.OverlayBuffer);

                            // copy stored image to overlay buffer
                            _stPad.DisplaySetImageFromStore(_storeIdOverlay);
                        }

                        // do all the following drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // draw disclaimer
                        x = 10;
                        y = 10;
                        int size = _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, _stPad.DisplayHeight - 80, signotec.STPadLibNet.TextAlignment.Left, _disclaimer);

                        // use font size of the disclaimer text
                        if (size != (int)fontSize)
                            _stPad.DisplaySetFont(new System.Drawing.Font("Arial", size, System.Drawing.FontStyle.Regular));

                        // set scroll text
                        y = _stPad.DisplayHeight - 80;
                        _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 100, signotec.STPadLibNet.TextAlignment.Left, "Congratulations! If you can read this text you have found the scroll buttons!");
                        if (_signPads[0].PadModel == PadModel.Delta)
                        {
                            y += _stPad.DisplayHeight;
                            _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 200, 0, "Doesn't the Delta have an impressive large image buffer?");
                            const int x2 = 320;
                            while ((y + _stPad.DisplayHeight) < _stPad.DisplayTargetHeight)
                            {
                                y += _stPad.DisplayHeight;
                                _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 200, 0, String.Format("You've reached line {0} of {1}!", y, _stPad.DisplayTargetHeight));

                                if ((y + _stPad.DisplayHeight) < _stPad.DisplayTargetHeight)
                                {
                                    y += _stPad.DisplayHeight;
                                    _stPad.DisplaySetTextInRect(x2, y, _stPad.DisplayWidth - 2 * x2, 200, signotec.STPadLibNet.TextAlignment.CenterCenteredVertically, "Click me, I'm a scrollable button!");
                                    _stPad.SensorAddScrollHotSpot(x2, y, _stPad.DisplayWidth - 2 * x2, 200, ScrollOption.Scrollable);
                                }
                            }
                        }

                        // set end text
                        switch (_signPads[0].PadModel)
                        {
                            case PadModel.Omega:
                                y = _stPad.DisplayTargetHeight - 120;
                                break;
                            case PadModel.Gamma:
                                y = _stPad.DisplayTargetHeight - 140;
                                break;
                            case PadModel.Delta:
                                y = _stPad.DisplayTargetHeight - 160;
                                break;
                        }
                        _stPad.DisplaySetTextInRect(x, y, _stPad.DisplayWidth - 20, 60, signotec.STPadLibNet.TextAlignment.Left, "You have scrolled to the end of this text!");

                        // set overlay rect
                        y = _stPad.DisplayHeight - 80;
                        _stPad.DisplaySetOverlayRect(0, y, _stPad.DisplayWidth, 80);
                        break;
                }

                // do all drawing operations on the LCD
                _stPad.DisplaySetTarget(DisplayTarget.ForegroundBuffer);

                // draw buffered image
                _stPad.DisplaySetImageFromStore(DisplayTarget.BackgroundBuffer);

                // set complete buffer for scrolling
                _stPad.SensorSetScrollArea(0, 0, 0, 0);

                if (_signPads[0].SupportsPenScrolling)
                    // enable pen scrolling
                    _stPad.SensorSetPenScrollingEnabled(true);

                //ButtonRetry.IsEnabled = false;
            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                //throw;
                return false;
            }
            finally
            {
                //this.Cursor = Cursors.Arrow;
            }
            return true;
        }




        private void CancelProcess()
        {
            try
            {
                if (_stPad.SignatureState)
                    // cancel capturing (this clears the LCD, too)
                    _stPad.SignatureCancel();
                else
                {
                    // disable pen scrolling
                    _stPad.SensorSetPenScrollingEnabled(false);

                    // erase LCD
                    _stPad.DisplayErase();
                }

                // clear all hot spots
                ClearHotSpots();
            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                //throw;
                return;
            }
            finally
            {
                //this.Cursor = Cursors.Arrow;
            }

        }



        private static void STPad_SensorHotSpotPressed(object sender, SensorHotSpotPressedEventArgs e)
        {            
            SensorHotSpotPressedEventHandler handler = new SensorHotSpotPressedEventHandler(SensorHotSpotPressed);
            System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(handler, new object[] { sender, e });
        }

        private static void SensorHotSpotPressed(object sender, SensorHotSpotPressedEventArgs e)
        {
            if (e.hotSpotId == _buttonCancelId) System.Diagnostics.Debug.WriteLine("test");
            //ButtonStartCancel_Click(this, new System.Windows.RoutedEventArgs());
            else if (e.hotSpotId == _buttonRetryId)
                ButtonRetry_Click(sender, new System.Windows.RoutedEventArgs());
            else if (e.hotSpotId == _buttonConfirmId)
                ButtonConfirm_Click(sender, new System.Windows.RoutedEventArgs());
            else
                MessageBox.Show(String.Format("Hot Spot {0} clicked.", e.hotSpotId + 1));
        }






        public static void CloseConnection()
        {
            try
            {
                // erase display
                _stPad.DisplayErase();

                // clear hot spots
                ClearHotSpots();

                var nCapabilities = _stPad.DeviceGetCapabilities(0);
                if (nCapabilities != null && nCapabilities.HasBacklight)
                    _stPad.DisplaySetBacklight(BacklightMode.Off);

                _stPad.DeviceClose(0);

                //_signPads = null;
                //_stPad = null;
            }
            catch (Exception)
            {
            }
        }

        private static void ButtonRetry_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (_stPad.SignatureState == true)
                    _stPad.SignatureRetry();
                else
                    StartDefaultCapturing();
            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                //throw;
            }
        }

        private static void ButtonConfirm_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (_stPad.SignatureState == false)
                // accept disclaimer and start capturing
                StartDefaultCapturing();
            else
            {
                ConfirmCapturing();

                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
                {
                Result = _stPad.SignatureSaveAsStreamEx(resolution, width, height, penWidth, penColor, options);

                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("Unterschriftenerfassung noch nicht gestartet"))
                    {
                        MessageBox.Show("Der Benutzer hat die Unterschrift ohne Eingabe bestätigt\nUnterschrift erneut einholen!", "Keine Unterschrift!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }


                    Result = null;
                }
                //signaturePath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + $"\\signature{new Random().Next(9999)}.tif";
                
                //signoPad._stPad.SignatureSaveAsFileEx(signaturePath, resolution, width, height, System.Drawing.Imaging.ImageFormat.Tiff, penWidth, penColor, options);

                SayThankYou();
            }
        }

        private static bool StartDefaultCapturing()
        {
            try
            {
                // disable pen scrolling
                _stPad.SensorSetPenScrollingEnabled(false);

                // erase display
                _stPad.DisplayErase();

                // clear hot spots
                ClearHotSpots();

                if (_signPads[0].HasDisplay)
                {
                    if (_signPads[0].FastConnection)
                    {   // "fast" pad or connection
                        // do all drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);
                    }
                    else
                    {   // "slow" pad or connection: do all drawing operations in the permanent memory
                        // make sure that always the second permanent memory is used
                        if (_storeIdOverlay < 0)
                            _storeIdOverlay = _stPad.DisplaySetTarget(_storeIdOverlay);
                        // set permanent memory as target
                        _storeIdSigning = _stPad.DisplaySetTarget(_storeIdSigning);
                    }

                    // draw the bitmaps
                    System.Drawing.Bitmap bitmap = null;
                    switch (_signPads[0].PadModel)
                    {
                        case PadModel.Sigma:
                            bitmap = Helper.LoadBitmapFromResource("Resources/Capture Sigma.png");
                            break;
                    }
                    _stPad.DisplaySetImage(0, 0, bitmap);

                    if (!_signPads[0].FastConnection)
                    {
                        // do all drawing operations in the background buffer
                        _stPad.DisplaySetTarget(DisplayTarget.BackgroundBuffer);

                        // draw stored image
                        _stPad.DisplaySetImageFromStore(_storeIdSigning);
                    }

                    if (_signPads[0].PadModel == PadModel.Alpha)
                        // draw disclaimer
                        _stPad.DisplaySetTextInRect(50, 250, _stPad.DisplayWidth - 100, 300, signotec.STPadLibNet.TextAlignment.Left, "With my signature, I certify that I'm excited about the signotec LCD Signature Pad and the signotec Pad Capture Control. This demo application has blown me away and I can't wait to integrate all these great features in my own application.");

                    // do all drawing operations on the LCD directly
                    _stPad.DisplaySetTarget(DisplayTarget.ForegroundBuffer);

                    // draw buffered image
                    _stPad.DisplaySetImageFromStore(DisplayTarget.BackgroundBuffer);

                    // set default signature window
                    int x = 0;
                    int y = 0;
                    int width = 0;
                    int height = 0;
                    switch (_signPads[0].PadModel)
                    {
                        case PadModel.Sigma:
                        case PadModel.Zeta:
                            y = 50;
                            break;
                        case PadModel.Omega:
                        case PadModel.Gamma:
                        case PadModel.Delta:
                            y = 100;
                            break;
                        case PadModel.Alpha:
                            x = 90;
                            y = 600;
                            width = 590;
                            height = 370;
                            break;
                    }
                    _stPad.SensorSetSignRect(x, y, width, height);

                    // add default hotspots
                    switch (_signPads[0].PadModel)
                    {
                        case PadModel.Sigma:
                        case PadModel.Zeta:
                            x = 12;
                            y = 9;
                            width = 85;
                            height = 33;
                            break;
                        case PadModel.Omega:
                        case PadModel.Gamma:
                            x = 24;
                            y = 18;
                            width = 170;
                            height = 66;
                            break;
                        case PadModel.Delta:
                            x = 150;
                            y = 18;
                            width = 170;
                            height = 66;
                            break;
                        case PadModel.Alpha:
                            x = 30;
                            y = 30;
                            width = 80;
                            height = 80;
                            break;
                    }
                    _buttonCancelId = _stPad.SensorAddHotSpot(x, y, width, height);

                    switch (_signPads[0].PadModel)
                    {
                        case PadModel.Sigma:
                        case PadModel.Zeta:
                            x = 117;
                            break;
                        case PadModel.Omega:
                            x = 234;
                            break;
                        case PadModel.Gamma:
                            x = 315;
                            break;
                        case PadModel.Delta:
                            x = 555;
                            break;
                        case PadModel.Alpha:
                            x = 344;
                            break;
                    }
                    _buttonRetryId = _stPad.SensorAddHotSpot(x, y, width, height);

                    switch (_signPads[0].PadModel)
                    {
                        case PadModel.Sigma:
                        case PadModel.Zeta:
                            x = 222;
                            break;
                        case PadModel.Omega:
                            x = 444;
                            break;
                        case PadModel.Gamma:
                            x = 605;
                            break;
                        case PadModel.Delta:
                            x = 960;
                            break;
                        case PadModel.Alpha:
                            x = 658;
                            break;
                    }
                    _buttonConfirmId = _stPad.SensorAddHotSpot(x, y, width, height);
                }

                // start capturing
                _stPad.SignatureStart();

            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                //throw;
                return false;
            }
            finally
            {
            }
            return true;
        }

        private static void ConfirmCapturing()
        {
            try
            {
                // confirm capturing
                _stPad.SignatureConfirm();

                SignatureConfirmed = true;
                // clear hot spots
                ClearHotSpots();
            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                MessageBox.Show(exc.Message);
                //throw;
            }
        }




        #region helper methods

        private static void ClearHotSpots()
        {
            try
            {
                _stPad.SensorClearHotSpots();
            }
            catch (STPadException exc)
            {
                Log.WriteLog(exc.ToString());
                if (exc.ErrorCode != -22)
                    throw exc;
            }
            //_buttonCancelId = -1;
            //_buttonRetryId = -1;
            //_buttonConfirmId = -1;
        }

        private System.Windows.Media.Imaging.BitmapImage GetBitmapFromResource(System.Drawing.Bitmap bitmap, System.Drawing.Imaging.ImageFormat imageFormat)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bitmap.Save(ms, imageFormat);
            System.Windows.Media.Imaging.BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = ms;
            bitmapImage.EndInit();
            return bitmapImage;
        }

        #endregion

    }





    public class SignPad
    {
        private STPadLibControl _stPad = null;
        private int _index = -1;
        private int _padType = 0;
        private int _connectionType = 0;
        private string _serial = "";
        private int _fwMajor = 0;
        private int _fwMinor = 0;

        public SignPad()
        {
        }

        public SignPad(STPadLibControl stPad, int index)
        {
            _stPad = stPad;
            _index = index;

            // get serial and type of selected device
            _stPad.DeviceGetInfo(out _serial, out _padType, index);

            // get connection type
            _connectionType = _stPad.DeviceGetConnectionType(index);

            // get firmware version
            string version = _stPad.DeviceGetVersion(index);
            string[] versionArray = version.Split('.');
            if ((versionArray != null) && (versionArray.Length > 1))
            {
                _fwMajor = Int32.Parse(versionArray[0]);
                _fwMinor = Int32.Parse(versionArray[1]);
            }
        }

        public int PadType
        {
            get { return _padType; }
        }

        public PadModel PadModel
        {
            get
            {
                switch (PadType)
                {
                    case 1:
                    case 2:
                        return PadModel.Sigma;
                    case 5:
                    case 6:
                        return PadModel.Zeta;
                    case 11:
                    case 12:
                        return PadModel.Omega;
                    case 15:
                    case 16:
                        return PadModel.Gamma;
                    case 21:
                    case 22:
                    case 23:
                        return PadModel.Delta;
                    case 31:
                    case 32:
                    case 33:
                        return PadModel.Alpha;
                    default:
                        throw new Exception("This pad type is not supported by this application!");
                };
            }
        }

        public string PadName
        {
            get
            {
                switch (_padType)
                {
                    case 1:
                        return "Sigma USB";
                    case 2:
                        return "Sigma Serial";
                    case 5:
                        return "Zeta USB";
                    case 6:
                        return "Zeta Serial";
                    case 11:
                        return "Omega USB";
                    case 12:
                        return "Omega Serial";
                    case 15:
                        return "Gamma USB";
                    case 16:
                        return "Gamma Serial";
                    case 21:
                        return "Delta USB";
                    case 22:
                        return "Delta Serial";
                    case 23:
                        return "Delta IP";
                    case 31:
                        return "Alpha USB";
                    case 32:
                        return "Alpha Serial";
                    case 33:
                        return "Alpha IP";
                    default:
                        return "Unkown pad type " + _padType;
                }
            }
        }

        public string ConnectionName
        {
            get
            {
                switch (_connectionType)
                {
                    case 0:
                        return "HID";
                    case 1:
                        return "USB";
                    case 2:
                        return String.Format("COM{0}", _stPad.DeviceGetComPort(_index));
                    case 3:
                        return _stPad.DeviceGetIPAddress(_index);
                    default:
                        return "Unkown connection type " + _connectionType;
                }
            }
        }

        public bool USB
        {
            get { return (_connectionType == 1); }
        }

        public string Serial
        {
            get { return _serial; }
        }

        public bool FastConnection
        {
            get { return (USB || (PadModel == PadModel.Sigma)); }
        }

        public string Firmware
        {
            get { return String.Format("{0}.{1}", _fwMajor, _fwMinor); }
        }

        public bool HasDisplay
        {
            get
            {
                if (_stPad == null)
                    return false;
                else
                    return _stPad.DeviceGetCapabilities(_index).HasDisplay;
            }
        }

        public bool SupportsPenScrolling
        {
            get
            {
                if (_stPad == null)
                    return false;
                else
                    return _stPad.DeviceGetCapabilities(_index).SupportsPenScrolling;
            }
        }
    }
}
