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


namespace Param
{
    public class Customizer : MercatorUi.ICustomizers.IFormLoadCustomizer, MercatorUi.ICustomizers.IStringUpdater, MercatorUi.ICustomizers.ISqlCommandUpdater
    {

        public void FormLoadCustomize(Form WindowsForm)
        {
            if (WindowsForm is MercatorUi.Forms.Param.ParamCategoriesForm) // on est dans l'écran de paramétrage des catégories
            {
                MercatorUi.Forms.Param.ParamCategoriesForm paramCategoriesForm = (MercatorUi.Forms.Param.ParamCategoriesForm)WindowsForm;
                foreach (MercatorUi.GridPro.DataGridViewXPro grid in paramCategoriesForm.Grids)
                {
                    grid.Columns.Add("colLibLong", "Libellé long");
                    grid.Columns["colLibLong"].Width = 400;
                    grid.Columns["colLibLong"].DataPropertyName = "libLong";
                    grid.Columns["colLibLong"].SortMode = DataGridViewColumnSortMode.NotSortable;
                }
            }
        }

        public string StringUpdate(string StringToModify)
        {
            if (StringToModify.Contains("CAT_STCK"))
            {
                StringToModify = StringToModify.Replace("id,nom", "id,nom,libLong");
            }
            return StringToModify;
        }

        public void SqlCommandUpdate(System.Data.SqlClient.SqlCommand SqlCommandToModify, Form WindowsForm) // modification de la requête de sauvegarde des données
        {
            if (WindowsForm is MercatorUi.Forms.Param.ParamCategoriesForm) // on est bien dans l'écran de paramétrage des catégories
            {
                if (SqlCommandToModify.Parameters.Contains("@type"))
                {
                    if (!SqlCommandToModify.CommandText.Contains("libLong")) // cette modification ne doit être effectuée qu'une seule fois. Ensuite la requête modifiée est utilisée pour toutes les lignes de la grille ajoutées ou modifiées
                    {
                        SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace("nom=@nom", "nom=@nom,libLong=@libLong");
                        SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace("id,nom,type)", "id,nom,type,libLong)");
                        SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace(",@type)", ",@type,@libLong)");
                    }
                    MercatorUi.Forms.Param.ParamCategoriesForm paramCategoriesForm = (MercatorUi.Forms.Param.ParamCategoriesForm)WindowsForm;
                    string id = SqlCommandToModify.Parameters["@id"].Value.ToString(); // il nous faut connaître l'ID de la ligne afin de retrouver le libellé long correspondant à cette ligne
                    string type = SqlCommandToModify.Parameters["@type"].Value.ToString();
                    DataTable dt = (DataTable)paramCategoriesForm.Grids[(Int32.Parse(type) - 1)].DataSource; // derrière la grille, se trouve en fait une DataTable
                    DataRow[] foundRows = dt.Select(string.Format("id='{0}'", Api.UnquoteSql(id))); // recherche du libellé long sur base de l'ID.
                    if (foundRows.Length > 0) // en principe, cette condition sera toujours remplie
                        SqlCommandToModify.Parameters.AddWithValue("@libLong", foundRows[0]["libLong"]); // on ajoute le paramètre SQL @libLong avec la bonne valeur
                }
            }
        }
    }
}