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) // wijzigen van verzoek voor opslaan gegevens
        {
            if (WindowsForm is MercatorUi.Forms.Param.ParamCategoriesForm) // we zijn in het parametreringscherm van de categorieën
            {
                if (SqlCommandToModify.Parameters.Contains("@type"))
                {
                    if (!SqlCommandToModify.CommandText.Contains("ordre")) // deze wijziging hoeft maar één keer te worden doorgevoerd. Vervolgens wordt de gewijzigde query gebruikt voor alle rijen van het raster die zijn toegevoegd of gewijzigd
                    {
                        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(); // we hebben het lijn ID nodig om de volgorde te vinden die overeenkomt met deze lijn
                    string type = SqlCommandToModify.Parameters["@type"].Value.ToString();
                    DataTable dt = (DataTable)paramCategoriesForm.Grids[(Int32.Parse(type) - 1)].DataSource; // achter het raster is een DataTable
                    DataRow[] foundRows = dt.Select(string.Format("id='{0}'", Api.UnquoteSql(id))); // de volgorde zoeken op basis van het ID
                    if (foundRows.Length > 0) // in principe wordt deze voorwaarde altijd voldaan
                        SqlCommandToModify.Parameters.AddWithValue("@ordre", (foundRows[0]["ordre"] == DBNull.Value ? 0 : Convert.ToInt32(foundRows[0]["ordre"]))); // SQL-parameter @ordre toevoegen met de juiste waarde
                }
            }
        }
    }
}