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. IStringUpdater ,
                              MercatorUi.ICustomizers. IFormLoadCustomizer ,
                              MercatorUi.ICustomizers. IFormClosedCustomizer ,
                              MercatorUi.ICustomizers. ISqlCommandUpdater
    {

         public string StringUpdate( string StringToModify) // Wijziging van de opdracht voor het lezen van de gegevens
        {
             if (StringToModify.StartsWith( "select id,nom from reps order by nom" )) // We zijn in het parameterinstellingscherm van de vertegenwoordigers
                 return StringToModify.Replace( "select id,nom" , "select id,nom,email" );
             else
                 return StringToModify; // we zijn in een ander parameterinstellingscherm -> we veranderen niets
        }

         public void FormLoadCustomize( Form WindowsForm)
        {
             if (WindowsForm is MercatorUi.Forms.Param. ParamRepsForm ) // We zijn in het parameterinstellingscherm van de vertegenwoordigers
            {
                MercatorUi.Forms.Param. ParamRepsForm paramRepsForm = (MercatorUi.Forms.Param. ParamRepsForm )WindowsForm;

                 // Toevoeging van de kolom e-mail
                 DataGridViewTextBoxColumn colEmail = new DataGridViewTextBoxColumn ();
                colEmail.Name = "email" ;
                colEmail.HeaderText = "Email" ;
                colEmail.Width = 160;
                colEmail.DataPropertyName = "email" ;
                colEmail.MaxInputLength = 35;
                colEmail.SortMode = DataGridViewColumnSortMode .NotSortable;
                paramRepsForm.Grid.Columns.Add(colEmail);

                 // Toevoeging van de kolom voor de Outlook-knop
                MercatorUi.GridPro. DataGridViewHidableButtonXColumn btnCol = new MercatorUi.GridPro. DataGridViewHidableButtonXColumn ();
                btnCol.Name = "btnCol" ;
                btnCol.Image = _Divers .ImageFromResource( "mail" );
                btnCol.Width = 34;
                btnCol.HeaderText = "" ;
                paramRepsForm.Grid.Columns.Add(btnCol);

                 // Installatie van 2 events
                paramRepsForm.Grid.CellFormatting += new System.Windows.Forms. DataGridViewCellFormattingEventHandler (Grid_CellFormatting);
                paramRepsForm.Grid.CellSuperClick += new MercatorUi.GridPro. CellSuperClickHandler (Grid_CellSuperClick);

                 // De breedte verminderen van de kolom "Naam" die in elk geval te breed is
                paramRepsForm.Grid.Columns[0].Width = 160;
            }
        }

         public void FormClosedCustomize( Form WindowsForm)
        {
             if (WindowsForm is MercatorUi.Forms.Param. ParamRepsForm ) // We zijn in het parameterinstellingscherm van de vertegenwoordigers
            {
                MercatorUi.Forms.Param. ParamRepsForm paramRepsForm = (MercatorUi.Forms.Param. ParamRepsForm )WindowsForm;

                 // De 2 events uitschrijven
                paramRepsForm.Grid.CellFormatting -= new System.Windows.Forms. DataGridViewCellFormattingEventHandler (Grid_CellFormatting);
                paramRepsForm.Grid.CellSuperClick -= new MercatorUi.GridPro. CellSuperClickHandler (Grid_CellSuperClick);
            }
        }

         private void Grid_CellFormatting( object sender, DataGridViewCellFormattingEventArgs e)
        {
            MercatorUi.GridPro. DataGridViewXPro Grid = (MercatorUi.GridPro. DataGridViewXPro )sender;
             if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {
                 if (Grid.Columns[e.ColumnIndex].Name == "btnCol" ) // De cel die momenteel wordt geformatteerd, is duidelijk de knop. We kunnen de knop dus deactiveren wanneer er geen e-mailadres is
                {

                    MercatorUi.GridPro. DataGridViewHidableButtonXCell cellBtn = (MercatorUi.GridPro. DataGridViewHidableButtonXCell )Grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    cellBtn.Disable = (Grid.Rows[e.RowIndex].Cells[ "email" ].Value == DBNull .Value) || (Grid.Rows[e.RowIndex].Cells[ "email" ].Value == null ) || (Grid.Rows[e.RowIndex].Cells[ "email" ].Value.ToString().Trim() == "" );
                }

            }
        }

         private void Grid_CellSuperClick( object sender, DataGridViewCellEventArgs e)
        {
            MercatorUi.GridPro. DataGridViewXPro Grid = (MercatorUi.GridPro. DataGridViewXPro )sender;
             if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {
                 if (Grid.Columns[e.ColumnIndex].Name == "btnCol" ) // De cel waarop we hebben geklikt, is inderdaad de Outlook-knop
                {
                    MercatorUi.GridPro. DataGridViewHidableButtonXCell cellBtn = (MercatorUi.GridPro. DataGridViewHidableButtonXCell )Grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
                     if (!cellBtn.Disable) // Als de knop niet is gedeactiveerd, kunnen we Outlook opstarten
                        MercatorOutlook. OutlookStatic .SendMail( "" , Grid.Rows[e.RowIndex].Cells[ "nom" ].Value.ToString().Trim(), Grid.Rows[e.RowIndex].Cells[ "email" ].Value.ToString().Trim(), "" , null );

                }
            }
        }

         public void SqlCommandUpdate(System.Data.SqlClient. SqlCommand SqlCommandToModify, Form WindowsForm) // Wijziging van de opdracht voor gegevensopslag
        {
             if (WindowsForm is MercatorUi.Forms.Param. ParamRepsForm ) // We zijn wel degelijk in het parameterinstellingscherm van de vertegenwoordigers
            {
                 if (!SqlCommandToModify.CommandText.Contains( "email" )) // Deze wijziging moet slechts eenmaal worden uitgevoerd. Daarna wordt de gewijzigde opdracht gebruikt voor alle toegevoegde/gewijzigde rijen van het rooster
                {
                    SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace( "nom=@nom" , "nom=@nom,email=@email" );
                    SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace( "(id,nom)" , "(id,nom,email)" );
                    SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace( "(@id,@nom)" , "(@id,@nom,@email)" );
                }
                MercatorUi.Forms.Param. ParamRepsForm paramRepsForm = (MercatorUi.Forms.Param. ParamRepsForm )WindowsForm;
                 string id = SqlCommandToModify.Parameters[ "@id" ].Value.ToString(); // We moeten de ID van de rij kennen om het e-mailadres te vinden dat bij deze rij hoort.
                 DataTable dt = ( DataTable )paramRepsForm.Grid.DataSource; // Achter het rooster bevindt er zich in feite een DataTable
                 DataRow [] foundRows = dt.Select( string .Format( "id='{0}'" , Api .UnquoteSql(id))); // Het e-mailadres zoeken op basis van de ID. Via UnquoteSql kunnen eventuele quotes in de ID worden gekopieerd.
                 if (foundRows.Length > 0) // In principe zal steeds voldaan zijn aan deze voorwaarde.
                    SqlCommandToModify.Parameters.Add( "@email" , SqlDbType .Char).Value = foundRows[0][ "email" ].ToString(); // Men voegt de SQL-parameter @email in met de juiste waarde.
            }
        }

    }
}