using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace Sample
{
    public partial class SampleDlg : Form
    {
        private delegate void AddMessage(string strMsg);
        private DllFunc m_DLLFunc;

        public SampleDlg()
        {
            InitializeComponent();

            m_DLLFunc = new DllFunc();
            m_DLLFunc.myCallbackEvent += new DllFunc.callbackEventHandler(AddMsgCBStatus);

            CreatePrinterList();
        }
        public void CloseSampleDlg()
        {
            m_DLLFunc.CloseSamp();
        }

        private void btnDirctIOEx_Click(object sender, EventArgs e)
        {
            string msg = m_DLLFunc.DirectIOExSamp();
            txtDisp.Text = msg;
        }

        private void btnGetPrnCapability_Click(object sender, EventArgs e)
        {
            string msg = m_DLLFunc.GetPrnCapabilitySamp();
            txtDisp.Text = msg;
        }

        private void btnGetCounter_Click(object sender, EventArgs e)
        {
            string msg = m_DLLFunc.GetCounterSamp();
            txtDisp.Text = msg;
        }

        private void chkCallbackStatus_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCallbackStatus.Checked)
            {
                m_DLLFunc.CallbackSamp(true);
                lstCallbackStatus.Items.Clear();
                lstCallbackStatus.Enabled = true;
            }
            else
            {
                m_DLLFunc.CallbackSamp(false);
                lstCallbackStatus.Enabled = false;
            }
        }

        private void cboPrinter_SelectedIndexChanged(object sender, EventArgs e)
        {
            string printerName = "";

            chkCallbackStatus.Enabled = false;
            btnGetPrnCapability.Enabled = false;
            btnGetCounter.Enabled = false;
            btnDirctIOEx.Enabled = false;
            lstCallbackStatus.Items.Clear();
            chkCallbackStatus.Checked = false;

            printerName = cboPrinter.SelectedItem.ToString();

            // Open Printer
            if (m_DLLFunc.OpenSamp(printerName))
            {
                chkCallbackStatus.Enabled = true;
                btnGetPrnCapability.Enabled = true;
                btnGetCounter.Enabled = true;
                btnDirctIOEx.Enabled = true;
            }
        }

        private void CreatePrinterList()
        {
            for (int index = 0; index < PrinterSettings.InstalledPrinters.Count; index++)
            {
                String printerName = PrinterSettings.InstalledPrinters[index];

                if (printerName.StartsWith("SII"))
                {
                    cboPrinter.Items.Add(printerName);
                }
            }

            if (cboPrinter.Items.Count == 0)
            {
                MessageBox.Show("Don't find the printer driver");
            }
            else
            {
                cboPrinter.Enabled = true;
                cboPrinter.Sorted = true;
                cboPrinter.SelectedIndex = 0;
            }
        }

        //  Display Message ( lstCallbackStatus )
        private void AddMsgCBStatus(string msg)
        {
            if (lstCallbackStatus.InvokeRequired == true)
            {
                AddMessage d = new AddMessage(AddMsgCBStatus);
                object[] obj = { msg };
                Invoke(d, obj);
            }
            else
            {
                lstCallbackStatus.Items.Insert(0, msg);
            }
        }
    }
}
