///Auther: Surajit Patra
///Form1.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.IO.Ports;
using System.Threading;

namespace RFIDAuthSystem
{
    public partial class Form1 : Form
    {
        string InputData = String.Empty;
        delegate void SetTextCallback(string text);

        public Form1()
        {
            InitializeComponent();
            string[] ports = SerialPort.GetPortNames();
            
            foreach (string port in ports)
            {
                cmbComSelect.Items.Add(port);
            }
        }

        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)
            {
                SetText(InputData);                
            }
        }

        private void SetText(string text)
        {
            txtSNumber.Text = "";
            if (this.txtSNumber.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else this.txtSNumber.Text += text;
            
            var instance = new DataBase();
            List<string>[] listOfItem = instance.Select(txtSNumber.Text);

            txtEName.Text = String.Join(String.Empty, listOfItem[1]);
            if (txtEName.Text != "")
            {                
                label1.Text = "Verified";
                string imagePath = String.Join(String.Empty, listOfItem[8]);
                //txtEName.Text = String.Join(String.Empty, listOfItem[1]);
                txtECode.Text = String.Join(String.Empty, listOfItem[2]);
                txtProject.Text = String.Join(String.Empty, listOfItem[3]);
                txtDesignation.Text = String.Join(String.Empty, listOfItem[4]);
                txtDescription.Text = String.Join(String.Empty, listOfItem[5]);
                txtMakeModel.Text = String.Join(String.Empty, listOfItem[6]);
                txtAssetCode.Text = String.Join(String.Empty, listOfItem[7]);
                pictureBox1.Image = Image.FromFile(imagePath);
            }
            else 
            {
                label1.Text = "Unauthorized Access !! Invalid Asset Code Detected";
                System.Media.SoundPlayer obj = new System.Media.SoundPlayer(@"D:\Techathon\RFIDRockers\Music\alert.wav");
                obj.Play();

                txtECode.Text = String.Empty;
                txtProject.Text = String.Empty;
                txtDesignation.Text = String.Empty;
                txtDescription.Text = String.Empty;
                txtMakeModel.Text = String.Empty;
                txtAssetCode.Text = String.Empty;
                pictureBox1.Image = null;
            }
        }        
    }
}
