using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MercatorApi;
using MercatorUi;
using System.Windows.Forms;

namespace Gescom
{
    public class Customizer : MercatorUi.ICustomizers.IStringUpdater, MercatorUi.ICustomizers.IFormLoadCustomizer, MercatorUi.ICustomizers.IFormClosedCustomizer
    {

        public string StringUpdate(string StringToModify)
        {
            string id = Api.StrExtract(StringToModify, "<ID>", "</ID>");
            if (id == "ALLOWANCES_V")
                return StringToModify.Replace("baremes.*", "baremes.*,dbo.GET_PRIX_TI(ar_ref) as s_prix_ti");
            else
                return StringToModify;
        }

        public void FormLoadCustomize(Form WindowsForm)
        {
            if (WindowsForm is MercatorUi.Forms.Gescom.GescomAllowancesForm)
            {
                MercatorUi.Forms.Gescom.GescomAllowancesForm gescomAllowancesForm = (MercatorUi.Forms.Gescom.GescomAllowancesForm)WindowsForm;
                if (gescomAllowancesForm.TypeVA == MercatorUi.Engine.Gescom.Billing.TypeVAEnum.V) // uniek voor de klantenkortingen
                {
                    // in het rooster van de lijnen

                    MercatorUi.GridPro.DataGridViewDoubleTextBoxColumn prix_ti = new MercatorUi.GridPro.DataGridViewDoubleTextBoxColumn();
                    prix_ti.Name = "prix_ti";
                    prix_ti.HeaderText = "Prix Régulier";
                    prix_ti.Width = 85;
                    prix_ti.DataPropertyName = "s_prix_ti";
                    prix_ti.Increment = 0;
                    prix_ti.DisplayFormat = "##0.00";
                    prix_ti.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    prix_ti.ReadOnly = true;
                    prix_ti.DefaultCellStyle.ForeColor = System.Drawing.Color.Gray;
                    gescomAllowancesForm.Grid.Columns.Insert(2, prix_ti);

                    gescomAllowancesForm.AfterInsertItem += new MercatorUi.Forms.Gescom.GescomAllowancesForm.AfterInsertItemEventHandler(gescomAllowancesForm_AfterInsertItem);
                    gescomAllowancesForm.Grid.CellValueChanged += new DataGridViewCellEventHandler(grid_CellValueChanged);
                }
            }
        }

        public void FormClosedCustomize(Form WindowsForm)
        {
            if (WindowsForm is MercatorUi.Forms.Gescom.GescomAllowancesForm)
            {
                MercatorUi.Forms.Gescom.GescomAllowancesForm gescomAllowancesForm = (MercatorUi.Forms.Gescom.GescomAllowancesForm)WindowsForm;
                if (gescomAllowancesForm.TypeVA == MercatorUi.Engine.Gescom.Billing.TypeVAEnum.V) // uniquement pour les remises clients
                {
                    gescomAllowancesForm.AfterInsertItem -= new MercatorUi.Forms.Gescom.GescomAllowancesForm.AfterInsertItemEventHandler(gescomAllowancesForm_AfterInsertItem);
                    gescomAllowancesForm.Grid.CellValueChanged -= new DataGridViewCellEventHandler(grid_CellValueChanged);
                }
            }
        }

        // na selectie van een artikel de cel s_prix_ti vervolledigen. Deze code wordt ook uitgevoerd bij het importeren.
        void gescomAllowancesForm_AfterInsertItem(object sender, MercatorUi.Forms.Gescom.GescomAllowancesForm.AfterInsertItemEventArgs e)
        {
            MercatorUi.Forms.Gescom.GescomAllowancesForm gescomAllowancesForm = (MercatorUi.Forms.Gescom.GescomAllowancesForm)sender;
            e.DataRowLignes["s_prix_ti"] = e.DataRowStock["s_prix_ti"];
        }

        // Indien er voor elke reden het artikel leeg gelaten is, plaatsen we nul in s_prix_ti
        void grid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            MercatorUi.GridPro.DataGridViewXPro grid = (MercatorUi.GridPro.DataGridViewXPro)sender;
            if ((e.ColumnIndex > -1) && (e.RowIndex > -1) && (grid.Columns[e.ColumnIndex].Name == "ar_ref") && ((grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == DBNull.Value) ||  (grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "")))
                grid.Rows[e.RowIndex].Cells["prix_ti"].Value = 0;
        }
    }
}