using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using MercatorApi;
using MercatorExtensions;
using MercatorUi;
using MercatorDatabase;
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("ordre", "Ordre");
                    grid.Columns["ordre"].Width = 40;
                    grid.Columns["ordre"].DataPropertyName = "ordre";
                    grid.Columns["ordre"].SortMode = DataGridViewColumnSortMode.NotSortable;
                }
            }
        }

        public string StringUpdate(string StringToModify)
        {
            if (StringToModify.Contains("CAT_GEN"))
                StringToModify = StringToModify.Replace("id,nom", "id,nom,ordre");
            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("ordre")) // 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,ordre=@ordre");
                        SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace("id,nom,type)", "id,nom,type,ordre)");
                        SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace(",@type)", ",@type,@ordre)");
                    }
                    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 l'ordre 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 de l'ordre sur base de l'ID.
                    if (foundRows.Length > 0) // en principe, cette condition sera toujours remplie
                        SqlCommandToModify.Parameters.AddWithValue("@ordre", (foundRows[0]["ordre"] == DBNull.Value ? 0 : Convert.ToInt32(foundRows[0]["ordre"]))); // on ajoute le paramètre SQL @ordre avec la bonne valeur
                }
            }
        }
    }
}