using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScaBox30
{
    public partial class operatorDialog : Form
    {
        
        //Key press supporting items
        DateTime _lastKeystroke = new DateTime(0);
        List<char> _barcode = new List<char>(10);
        DateTime _date_down = new DateTime(0);
        DateTime timeDelta = new DateTime(0);
        int _PressedKey = 0;

        
        
        //private bool barcodeOnly;
        public string OPIDgave { get { return txtDialog.Text; } }

        public operatorDialog(bool barcodeOnly)
        {
            InitializeComponent();
            InitializeItems();
            InitializeButtons();
            if (barcodeOnly)
            {
                this.KeyPreview = true;
            }
            else
            {
                this.KeyPreview = false;
            }
            ActiveControl = this.txtDialog;
        }


        private void operatorDialog_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Key_Preview must be set to True on the form EWQ
            // check timing (keystrokes within 100 ms)
            TimeSpan elapsed = (DateTime.Now - _lastKeystroke);
            if (elapsed.TotalMilliseconds > 20)
            {
                _barcode.Clear();
                txtDialog.Text = "";
            }


            // record keystroke & timestamp
            _barcode.Add(e.KeyChar);
            _lastKeystroke = DateTime.Now;

            // process barcode
            if (e.KeyChar == 13 && _barcode.Count > 0)
            {
                string msg = new String(_barcode.ToArray());
                msg = msg.Replace("\r", "");
                if (msg != "")
                {
                    txtDialog.Text = msg;
                    //btnDialog.Focus();
                    button1.Focus();
                }
                _barcode.Clear();
            }
        }


        private void operatorDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            switch (txtDialog.Text.Length)
            {
                case 6:
                    e.Cancel = false;
                    break;
                case 0:
                    e.Cancel = true;
                    RJMessageBox.Show("El ID es obligatorio.", "ScanBox", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    break;

                default:
                    e.Cancel = true;
                    RJMessageBox.Show("El ID debe ser de 6 dígitos", "ScanBox", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    break;
            }

        }


        private void operatorDialog_KeyDown(object sender, KeyEventArgs e)
        {
            _PressedKey = e.KeyValue;
            _date_down = DateTime.Now;
            e.Handled = true;
        }


        private void operatorDialog_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == _PressedKey)
            {
                var timeDelta = DateTime.Now - _date_down;
                _PressedKey = e.KeyValue;
                if (timeDelta.TotalMilliseconds > 20)
                {
                    _barcode.Clear();
                    txtDialog.Text = "";
                }
            }
        }


        private void InitializeItems()
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.Padding = new Padding(Globals.borderSize);//Set border size
            this.btnClose.DialogResult = DialogResult.Cancel;
            this.button1.DialogResult = DialogResult.OK;
            this.labelCaption.Text = this.Text;
            this.panelTitleBar.BackColor = Globals.primaryColor;
            this.panelTitleBar.Padding = new Padding(0, 0, 0, Globals.borderSize);
            this.BackColor = Globals.primaryColor;

            this.labelMessage.Text = "Ingrese su ID";
            int xCenter = (this.panelButtons.Width - labelMessage.Width) / 2;
            int yCenter = (this.panelButtons.Height - labelMessage.Height) / 2;

            labelMessage.Location = new Point(xCenter, labelMessage.Location.Y);
        }

        private void InitializeButtons()
        {
            int xCenter = (this.panelButtons.Width - button1.Width) / 2;
            int yCenter = (this.panelButtons.Height - button1.Height) / 2;

            button1.Location = new Point(xCenter, yCenter);
            button1.Text = "Aceptar";
        }

        #region -> Drag Form
        [DllImport("user32.DLL", EntryPoint = "SendMessage")]
        private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam);
        [DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
        private extern static void ReleaseCapture();
        private void panelTitleBar_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, 0x112, 0xf012, 0);
        }
        #endregion

        
    }
}
