///Auther: Surajit Patra
///Form2.cs
///surajitpatra@gmail.com, Mob: 8007369459

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 System.Threading;
using System.IO.Ports;

namespace RFIDAuthSystem
{
    public partial class Form2 : Form
    {
        string InputDataBook = String.Empty;
        string InputDataEmpId = String.Empty;
        string InputData = String.Empty;
        bool flag = false;
        delegate void SetTextCallback(Form f, Control ctrl, string text);

        public Form2()
        {
            InitializeComponent();

            string[] ports = SerialPort.GetPortNames();

            foreach (string port in ports)
            {
                cmbComSelect.Items.Add(port);
            }
        }

        public static void SetText(Form form, Control ctrl, string text)
        {
            // InvokeRequired required compares the thread ID of the 
            // calling thread to the thread ID of the creating thread. 
            // If these threads are different, it returns true. 
            if (ctrl.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                form.Invoke(d, new object[] { form, ctrl, text });
            }
            else
            {
                ctrl.Text = text;
            }
        }

        private void cmbComSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (port.IsOpen) port.Close();
            port.PortName = cmbComSelect.SelectedItem.ToString();

            try
            {
                port.Open();
            }
            catch
            {
                MessageBox.Show("Serial port " + port.PortName + " cannot be opened!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cmbComSelect.SelectedText = "";
            }
        }


        void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(2000);
            InputData = port.ReadExisting();
            if (InputData != String.Empty)
            {
                if (InputData.Contains("$B-"))
                {
                    string[] words = InputData.Split('_');                   
                    SetText(this, txt_bookID, String.Join(String.Empty, words[0]));
                    SetText(this, txt_Name, String.Join(String.Empty, words[1]));
                    SetText(this, txt_Author, String.Join(String.Empty, words[2]));                    
                    pic_book.Image = Image.FromFile(@"D:\Techathon\RFIDRockers\Pic\QT4.jpg");
                    SetText(this, label1, "Please swipe your Employee Card - Now");                    
                }
                else if (InputData != "" && txt_bookID.Text != "")
                {
                    InputDataEmpId = InputData;                    
                    SetText(this, txt_EmployeeID, String.Join(String.Empty, InputData));
                }
                else
                {
                    label1.Text = "Please swipe the Book first";
                }
            }

            if (txt_bookID.Text != "" && txt_EmployeeID.Text != "")
            {
                var instance = new DataBase();
                bool status = false;
                status = instance.CheckBookIssued(txt_bookID.Text);
                if (!status)
                {
                    bool result = instance.Insert(txt_bookID.Text, txt_Name.Text, txt_Author.Text, txt_EmployeeID.Text);
                    if (result)
                    {                        
                        SetText(this, label1, "Successfully: Book Issued To Employee ID Number: '" + txt_EmployeeID.Text + "'");
                    }
                    else
                    {
                        SetText(this, label1, "Failed");                        
                    }
                }
                else
                {
                    bool result = instance.Delete(txt_EmployeeID.Text);
                    if (result)
                    {
                        SetText(this, label1, "Successfully: Return Book By Employee ID Number: '" + txt_EmployeeID.Text + "'");                        
                    }
                    else
                    {
                        SetText(this, label1, "Failed");
                    }
                }
            }
        }
    }
}
