using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorExtensions;
using MercatorUi;
using System.Reflection;
using System.Windows.Forms;
using POS.Devices;
using POS;
using Microsoft.PointOfService;

// <ReferenceInclude>"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PointOfService\v4.0_1.14.1.0__31bf3856ad364e35\Microsoft.PointOfService.dll"</ReferenceInclude>
// <ReferenceInclude>"POS.Devices.Opos_Constants.dll"</ReferenceInclude>
// <ReferenceInclude>"POS.Devices.OPOSCashDrawer.dll"</ReferenceInclude>

namespace Main
{
    public class Customizer : MercatorUi.ICustomizers.IExec
    {
        public void Main(MercatorUi.ICustomizers.ExecAction action)
        {
            switch (action)
            {
                case MercatorUi.ICustomizers.ExecAction.DossierOpen:
                    MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawer += MercatorUiFormsBillingBillingObjectsPayments_BeforeOpenCashDrawer;
                    break;
                case MercatorUi.ICustomizers.ExecAction.DossierClose:
                    MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawer -= MercatorUiFormsBillingBillingObjectsPayments_BeforeOpenCashDrawer;
                    break;
            }
        }

        void MercatorUiFormsBillingBillingObjectsPayments_BeforeOpenCashDrawer(MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawerEventArgs e)
        {
            string strLogicalName = Globals.ParamPos["PORT_TIR"];
            CashDrawer m_Drawer = null;
            try
            {
                //Create PosExplorer
                PosExplorer posExplorer = new PosExplorer();
                DeviceInfo deviceInfo = null;
                deviceInfo = posExplorer.GetDevice(DeviceType.CashDrawer, strLogicalName);
                m_Drawer = (CashDrawer)posExplorer.CreateInstance(deviceInfo);
                //Open the device
                //Use a Logical Device Name which has been set on the SetupPOS.
                m_Drawer.Open();
                //Get the exclusive control right for the opened device.
                //Then the device is disabled for other applications.
                m_Drawer.Claim(1000);
                //Enable the device.
                m_Drawer.DeviceEnabled = true;
                m_Drawer.OpenDrawer();
                e.Result = MercatorUi.Forms.Billing.BillingObjects.Payments.BeforeOpenCashDrawerResultEnum.Done;
            }
            catch (PosException ex)
            {
                MessageBox.Show(_Divers.Iif_langue(Globals.Langue, "Fehler beim Öffnen der Schublade : ", "Error opening drawer : ", "Fout bij openen lade: ", "Erreur ouverture tiroir caisse : ") + "\r\n\r\nBillingPayments_BeforeOpenCashDrawer() :\r\n" + ex.ToString());
                //Nothing can be used.
                return;
            }
            finally
            {
                //Finish using the device.
                if (m_Drawer != null)
                    m_Drawer.Close();
            }
        }
    }
}