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.IFormLoadCustomizer, MercatorUi.ICustomizers.IFormClosedCustomizer
    {
        public void FormLoadCustomize(System.Windows.Forms.Form WindowsForm)
        {
            MercatorUi.Forms.Billing.BillingForm billingForm = (MercatorUi.Forms.Billing.BillingForm)WindowsForm;
            if (billingForm.LinesEditor != null)
                billingForm.LinesEditor.BeforeCellValidated += billingFormLinesEditor_BeforeCellValidated;
        }

        public void FormClosedCustomize(System.Windows.Forms.Form WindowsForm)
        {
            MercatorUi.Forms.Billing.BillingForm billingForm = (MercatorUi.Forms.Billing.BillingForm)WindowsForm;
            if (billingForm.LinesEditor != null)
                billingForm.LinesEditor.BeforeCellValidated -= billingFormLinesEditor_BeforeCellValidated;
        }

        void billingFormLinesEditor_BeforeCellValidated(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            MercatorUi.Forms.Billing.BillingObjects.LinesEditor linesEditor = (MercatorUi.Forms.Billing.BillingObjects.LinesEditor)sender;
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1) && (linesEditor.Grid.Columns[e.ColumnIndex].Name == "id_article"))
            {
                string id_article = linesEditor.Grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().TrimEnd();
                if (id_article.EndsWith("%") && id_article.StartsWith("1*"))
                    linesEditor.Grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = id_article.Substring(2);
            }
        }
    }
}