using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Globalization;

namespace BulkPrinting
{
    public partial class VoucherForm : Form
    {
        public VoucherForm()
        {
            InitializeComponent();
        }

        public int BatchID;
        public string BatchTitle;

        private void VoucherForm_Load(object sender, EventArgs e)
        {
            numBatchId.Maximum = int.MaxValue;
            numStartSeqNo.Maximum = int.MaxValue;
            numEndSeqNo.Maximum = int.MaxValue;
            if (BatchID > 0)
            {
                rdoVoucherRange.Show();
                lblBatch1.Show();
                lblBatch2.Show();
                numStartSeqNo.Show();
                numEndSeqNo.Show();
                numBatchId.Value = BatchID;
            }
            else {
                rdoVoucherRange.Hide();
                lblBatch1.Hide();
                lblBatch2.Hide();
                numStartSeqNo.Hide();
                numEndSeqNo.Hide();
            }
        }

        private void dgvVouchers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            using (var Command = Globals.DB.CreateCommand())
            {
                if (rdoPrintDate.Checked)
                {
                    Command.CommandText = "SELECT v.BatchId, v.Serial, v.SequenceNumber, v.ExpiryDate, v.EncryptedPIN FROM Voucher v LEFT JOIN Logs l on v.Id = l.VoucherId and l.EventType=@eventtype WHERE l.EventDate BETWEEN @startdate AND @enddate";
                    Command.Parameters.AddWithValue("@eventtype", VendorEvent.VendorEventType.PrintVoucher);
                    CultureInfo IVC = CultureInfo.InvariantCulture;
                    Command.Parameters.AddWithValue("@startdate", dtpStartDate.Value.ToString("yyyy-MM-dd 00:00:00", IVC));
                    Command.Parameters.AddWithValue("@enddate", dtpEndDate.Value.ToString("yyyy-MM-dd 23:59:59", IVC));
                }
                else if (rdoVoucherRange.Checked)
                {
                    Command.CommandText = "SELECT BatchId, Serial, SequenceNumber, ExpiryDate, EncryptedPIN FROM Voucher WHERE BatchId = @batchid AND SequenceNumber BETWEEN @minseqno AND @maxseqno";
                    Command.Parameters.AddWithValue("@batchid", numBatchId.Value);
                    Command.Parameters.AddWithValue("@minseqno", numStartSeqNo.Value);
                    Command.Parameters.AddWithValue("@maxseqno", numEndSeqNo.Value);
                }
                else if (rdoSerialNum.Checked)
                {
                    Command.CommandText = "SELECT BatchId, Serial, SequenceNumber, ExpiryDate, EncryptedPIN FROM Voucher WHERE Serial=@serial";
                    Command.Parameters.AddWithValue("@serial", txtSerial.Text);
                }

                dgvVouchers.Rows.Clear();
                dgvVouchers.Columns.Clear();
                dgvVouchers.Columns.Add("BatchId", "Batch ID");
                dgvVouchers.Columns.Add("Serial", "Serial Number");
                dgvVouchers.Columns.Add("SequenceNumber", "Sequence Number");
                dgvVouchers.Columns.Add("ExpiryDate", "Expiry Date");
                dgvVouchers.Columns.Add("PIN", "PIN");
                using (SQLiteDataReader SQLReader = Command.ExecuteReader())
                {
                    while (SQLReader.Read())
                    {
                        string VoucherPIN = Utility.TripleDESDecrypt(SQLReader.GetValue(4).ToString(), Globals.SessionVoucherKey).ToString();
                        dgvVouchers.Rows.Add(new object[] {
                            SQLReader.GetValue(0),
                            SQLReader.GetValue(1),
                            SQLReader.GetValue(2),
                            SQLReader.GetValue(3),
                            VoucherPIN
                        });
                    }
                }
            }
        }

        private void rdoSerialNum_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void numEndSeqNo_ValueChanged(object sender, EventArgs e)
        {

        }

        private void numStartSeqNo_ValueChanged(object sender, EventArgs e)
        {

        }

        private void lblBatch2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void lblBatch1_Click(object sender, EventArgs e)
        {

        }

        private void dtpEndDate_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dtpStartDate_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void rdoVoucherRange_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rdoPrintDate_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void lblBatchDetails_Click(object sender, EventArgs e)
        {

        }
    }
}
