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("from baremes", ",dbo.GET_PRIX_TI(ar_ref) as s_prix_ti from baremes");
            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) // uniquement pour les remises clients
                {
                    // dans la grille des lignes

                    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 += GescomAllowancesForm_AfterInsertItem;
                    gescomAllowancesForm.Grid.CellValueChanged += 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 -= GescomAllowancesForm_AfterInsertItem;
                    gescomAllowancesForm.Grid.CellValueChanged -= Grid_CellValueChanged;
                }
            }
        }

        // après sélection d'un article, compléter la cellule s_prix_ti. Ce code est aussi exécuté lors de l'import.
        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"];
        }

        // si pour une quelconque raison, l'id article est remis à blanc, alors remettre zéro dans 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;
        }
    }
}