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;

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

        public string PrintDescription;
        public DateTime BatchOrderDate;
        public int UnprintedVouchers;
        public int TotalVouchers;
        public string FaceValue;
        public string VoucherTypeName;
        public string NetworkName;
        public int BatchID;
        private Boolean CanReprintThisSession;

        private void PrintForm_Load(object sender, EventArgs e)
        {
            CanReprintThisSession =
                ((Globals.SessionMode == SessionModes.Online &&
                    Utility.CheckUserAccess(Utility.UserPermissions.CanReprintOnline)) ||
                (Globals.SessionMode == SessionModes.Offline &&
                    Utility.CheckUserAccess(Utility.UserPermissions.CanReprintOffline)));
            lblPrintDescription.Text = PrintDescription;
            lblBatchNumber.Text = BatchID.ToString();
            lblBatchOrderDate.Text = BatchOrderDate.Date.ToString("dd/MM/yyyy");
            lblUnprintedVouchers.Text = UnprintedVouchers.ToString();
            lblTotalVouchers.Text = TotalVouchers.ToString();
            lblFaceValue.Text = FaceValue;
            lblVoucherType.Text = VoucherTypeName;
            lblNetwork.Text = NetworkName;
            numNumVouchers.Maximum = UnprintedVouchers;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Globals.OpenBatches.Remove(BatchID);
            this.Close();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            decimal VoucherCount;

            if (numNumVouchers.Value < 1)
            {
                MessageBox.Show("Please select a valid number of vouchers to print", "Invalid print value", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (numNumVouchers.Value > UnprintedVouchers)
            {
                if (CanReprintThisSession)
                {
                    MessageBox.Show("You have selected more vouchers than are available to print. If you wish to reprint vouchers that have already been printed, please select the 'Print Voucher Range' option and type in the starting and ending voucher sequence numbers to print that range.", "Cannot print that many vouchers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                else
                {
                    MessageBox.Show("You have selected more vouchers than are available to print.", "Cannot print that many vouchers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }

            VoucherCount = numNumVouchers.Value;
            decimal TotalVoucherValue = VoucherCount * (decimal)Globals.DB.ExecuteScalar(
                "SELECT FaceValue FROM Batch WHERE Id = @id",
                new SQLiteParameter("@id", BatchID));

            if (Globals.SessionMode == SessionModes.Online)
            {
                if (!Utility.IsValueWithinRemainingUserLimit(UserLimits.UserLimitTypes.OnlinePrint, TotalVoucherValue))
                {
                    MessageBox.Show("You have selected more vouchers than your permissions allow you to print today.", "Cannot print that many vouchers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
            if (Globals.SessionMode == SessionModes.Offline)
            {
                if (!Utility.IsValueWithinRemainingUserLimit(UserLimits.UserLimitTypes.OfflinePrint, TotalVoucherValue))
                {
                    MessageBox.Show("You have selected more vouchers than your permissions allow you to print today.", "Cannot print that many vouchers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
            if (Globals.MaxPrinter.ChoosePrinter())
            {
                PrintAlignmentDialog printdialog = new PrintAlignmentDialog();
                printdialog.PrintDescription = PrintDescription;
                printdialog.BatchID = BatchID;
                printdialog.BatchOrderDate = BatchOrderDate;
                printdialog.FaceValue = FaceValue;
                printdialog.VoucherTypeName = VoucherTypeName;
                printdialog.NetworkName = NetworkName;
                printdialog.NumVouchers = (int)numNumVouchers.Value;
                printdialog.FirstSeqNo = null;
                printdialog.LastSeqNo = null;
                printdialog.Show();
                this.Close();
            }

        }

        private void lblPrintDescription_Click(object sender, EventArgs e)
        {

        }
    }
}
