using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RogerRud.RUD;

namespace RUD2v2Example
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// RUD-2v2.0 
        /// </summary>
        private RUD2Reader MyReader = new RUD2Reader();

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Close Form1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonExit_Click(object sender, EventArgs e)
        {
            //Close
            this.Close();   
         
        }

        /// <summary>
        /// Get Roger RUD Readers( Usb - Hid ) library version
        /// </summary>
        private void buttonGetVersion_Click(object sender, EventArgs e)
        {
            
            textBoxGetVersion.Text = RUD2Reader.getLibraryVersion(); //library Version >= 1.1.0.2
        }

        /// <summary>
        /// Get list of connected Rud2v2.0 readers
        /// </summary>
        private void buttonDeviceList_Click(object sender, EventArgs e)
        {
            List<String[]> allRud4 = new List<string[]>();

            allRud4 = MyReader.FindAll();           //Get info 

            deviceList.RowCount = 0;

            foreach (String[] number in allRud4)
            {
                //Add to DataGridView
                String[] row = new String[] { deviceList.RowCount.ToString(), number[0], number[1], number[2] };         //serial number, description, port
                deviceList.Rows.Add(row);

            }

            deviceList.AutoSize = true;
        }

        /// <summary>
        /// Connection / disconnection to selected RUD-2 reader
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void usbButtonConnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (usbButtonConnect.Text.Equals("Disconnect") == false)                                //Connected
                {
                    //try connect using selected readers serial number
                    if (MyReader.connect(deviceList.CurrentRow.Cells["Serial"].Value.ToString()))
                    {
                        usbConnectedDevice(true);       //Update information 

                    }

                }
                else
                {
                    //try disconnect
                    if (MyReader.disconnect())
                    {
                        usbConnectedDevice(false);      //Update information 

                    }
                }
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Change Form1 property when connection change
        /// </summary>
        /// <param name="connectState">New connection state</param>
        private void usbConnectedDevice(Boolean connectState)
        {
            buttonDeviceList.Enabled = !connectState;               //Block when connect 
            deviceList.Enabled = !connectState;                     //Block when connect 
            buttonReadProxy.Enabled = connectState;                 //Block when disconnect


            if (connectState)
            {
                usbButtonConnect.Text = "Disconnect";
               
            }
            else
            {
                usbButtonConnect.Text = "Connect with the selected device";
                
            }

        }
     
        /// <summary>
        /// Try get card number from RUD-4 v 1.0 reader
        /// </summary>
        private void buttonReadProxy_Click(object sender, EventArgs e)
        {
            try
            {
                textBoxReadProxy.Text = MyReader.readProxy();    //
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }

        private void buttonReadMifare_Click(object sender, EventArgs e)
        {
            try
            {
                textBoxReadMifare.Text = MyReader.readMifareBuff();    //
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           

        }

        private void buttonReadEM_Click(object sender, EventArgs e)
        {
            try
            {
                textBoxReadEM.Text = MyReader.readEMBuff();    //
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonBuzzerON_Click(object sender, EventArgs e)
        {
            try
            {
                MyReader.buzzerON();    //
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonBuzzerOff_Click(object sender, EventArgs e)
        {
            try
            {
                MyReader.buzzerOff();    //
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void buttonRealyON_Click(object sender, EventArgs e)
        {
            try
            {
                MyReader.RelayON();
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonRealyOff_Click(object sender, EventArgs e)
        {
            try
            {
                MyReader.RelayOff();    //
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonLedRedOn_Click(object sender, EventArgs e)
        {
            try
            {
                MyReader.lenRedON();
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonLedRedOff_Click(object sender, EventArgs e)
        {
            try
            {
                MyReader.lenRedOff();
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonLedGreenOn_Click(object sender, EventArgs e)
        {
            try
            {
                MyReader.lenGreenON();
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonLedGreenOff_Click(object sender, EventArgs e)
        {
            try
            {
                MyReader.lenGreenOff();
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}
