using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using MercatorApi;
using MercatorUi;
using MercatorExtensions;
using MercatorDatabase;

namespace Billing
{
    public class Customizer : MercatorUi.ICustomizers.IBillingEngineCreated, MercatorUi.ICustomizers.IBillingEngineClosed
    {

        public void BillingEngineCreated(MercatorUi.Engine.Gescom.BillingEngine billingEngine)
        {
            billingEngine.BillingFormLoaded += billingEngine_BillingFormLoaded;
            billingEngine.BeforeDeleteLine += BillingEngine_BeforeDeleteLine;
        }

        public void BillingEngineClosed(MercatorUi.Engine.Gescom.BillingEngine billingEngine)
        {
            billingEngine.BillingFormLoaded -= billingEngine_BillingFormLoaded;
            billingEngine.BeforeDeleteLine -= BillingEngine_BeforeDeleteLine;
        }

        void billingEngine_BillingFormLoaded(object sender, EventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            if (!billingEngine.ReadOnly)
            {
                billingEngine.BillingForm.FormClosed += billingEngineBillingForm_FormClosed;
                MercatorUi.GridPro.DataGridViewXPro grid = ((MercatorUi.Forms.Billing.BillingObjects.LinesEditor)billingEngine.BillingForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Billing.BillingObjects.LinesEditor))[0]).Grid;
                grid.CellEnter += grid_CellEnter;
            }
        }

        void billingEngineBillingForm_FormClosed(object sender, EventArgs e)
        {
            MercatorUi.Forms.Billing.BillingForm billingForm = (MercatorUi.Forms.Billing.BillingForm)sender;
            if (!billingForm.BillingEngine.ReadOnly)
            {
                billingForm.FormClosed -= billingEngineBillingForm_FormClosed;
                MercatorUi.GridPro.DataGridViewXPro grid = ((MercatorUi.Forms.Billing.BillingObjects.LinesEditor)billingForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Billing.BillingObjects.LinesEditor))[0]).Grid;
                grid.CellEnter -= grid_CellEnter;
            }
        }

        //Het wijzigen van het veld ID_ARTICLE vermijden 
        void grid_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            MercatorUi.GridPro.DataGridViewXPro grid = (MercatorUi.GridPro.DataGridViewXPro)sender;
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1) && (grid.Columns[e.ColumnIndex].Name == "id_article"))
                grid.Columns[e.ColumnIndex].ReadOnly = (grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value is string) && !string.IsNullOrWhiteSpace(grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
        }

        // Tussenkomen in het verwijderen van de lijn en een negatieve artikellijn toevoegen
        void BillingEngine_BeforeDeleteLine(object sender, MercatorUi.Engine.Gescom.BillingEngine.BeforeDeleteLineEventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            if (e.LignesVRecord.ID_ARTICLE != "")
            {
                var lv_new = new MercatorDatabase.LIGNES_V(billingEngine.LIGNES.NewRow());
                Api.DataRowMerge(lv_new.DataRow, e.DataRow);
                lv_new.DL_ID = Api.Ident();
                lv_new.QorQ_UNITE = -lv_new.QorQ_UNITE;
                int i = Api.DataRowIndexInDataTable(e.DataRow);
                if (i == billingEngine.LIGNES.Rows.Count - 1)
                    billingEngine.LIGNES.Rows.Add(lv_new.DataRow);
                else
                    billingEngine.LIGNES.Rows.InsertAt(lv_new.DataRow, i + 1);
                billingEngine.UpdateAmounts();
                e.Cancel = true; // het verwijderen van de lijn voorkomen
            }
        }
    }
}