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) //  We zitten in het scherm voor de parameterinstelling van de categorieën
            {
                MercatorUi.Forms.Param.ParamCategoriesForm paramCategoriesForm = (MercatorUi.Forms.Param.ParamCategoriesForm)WindowsForm;
                foreach (MercatorUi.GridPro.DataGridViewXPro grid in paramCategoriesForm.Grids)
                {
                    grid.Columns.Add("colLibLong", "Omschrijving");
                    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) // Wijziging van de opdracht voor gegevensopslag
        {
            if (WindowsForm is MercatorUi.Forms.Param.ParamCategoriesForm) // We zitten wel degelijk in het scherm voor de parameterinstelling van de categorieën
            {
                if (SqlCommandToModify.Parameters.Contains("@type"))
                {
                    if (!SqlCommandToModify.CommandText.Contains("libLong")) // Deze wijziging moet slechts eenmaal worden uitgevoerd. Daarna wordt de gewijzigde opdracht gebruikt voor alle toegevoegde of gewijzigde rijen van het rooster
                    {
                        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(); //  We moeten de ID van de rij kennen om de omschrijving te vinden die bij deze rij hoort.
                    string type = SqlCommandToModify.Parameters["@type"].Value.ToString();
                    DataTable dt = (DataTable)paramCategoriesForm.Grids[(Int32.Parse(type) - 1)].DataSource; // Achter het rooster bevindt zich in feite een DataTable
                    DataRow[] foundRows = dt.Select(string.Format("id='{0}'", Api.UnquoteSql(id))); // De omschrijving zoeken op basis van de ID
                    if (foundRows.Length > 0) // In principe zal altijd voldaan zijn aan deze voorwaarde
                        SqlCommandToModify.Parameters.AddWithValue("@libLong", foundRows[0]["libLong"]); // We voegen de SQL-parameter @libLong met de juiste waarde toe
                }
            }
        }
    }
}