using System;
using System.Collections.Generic;
using System.Text;
using MercatorApi;
using System.Windows.Forms;
using System.Drawing;

namespace HistCli
{
     public class Customizer : MercatorUi.ICustomizers. IStringUpdater ,
                              MercatorUi.ICustomizers. IFormGridCustomizer ,
                              MercatorUi.ICustomizers. IFormLoadCustomizer ,
                              MercatorUi.ICustomizers. IFormClosedCustomizer
    {
         /*
          * Cette première méthode est l'implémentation de l'interface IStringUpdater.
          * Elle permet de modifier directement la requête SQL que Mercator se propose d'exécuter par défaut.
          * Comme cette requête est du type select pieds_v.id,journal,piece,reference,type,date,heure,tot_bas_dv,tot_bas_fb,tot_ttc_dv,tot_ttc_fb,n_dec,id_dev,imprime,cubic as flag_appl,niveau_bo,c_nom,devises.nom as dev_nom from pieds_v (NOLOCK),cli (NOLOCK),devises (NOLOCK) where (pieds_v.id_cli=cli.c_id) and (pieds_v.id_dev=devises.id) and (journal='VEN  ')  and (piece>=20060000) and (piece<=20069999) and (date>=cast('01/10/2006' as datetime)) and (date<=cast('31/10/2006' as datetime)) order by piece desc
          * on ajoute simplement ,echeance après date
          * Il est bien entendu possible d'apporter des modifications plus complexes, qui par exemple ajoutent des tables dans la clause from
          */

         public string StringUpdate( string StringToModify)
        {
             return StringToModify.Replace( "date," , "date,echeance," );
        }

         /*
          * Cette seconde méthode est l'implémentation de l'interface IFormGridCustomizer
          * Elle ajoute la colonne échéance au datagridview
          */

         public void FormGridCustomize( Form WindowsForm)
        {
            MercatorUi.Forms.Hist. HistForm HistForm = (MercatorUi.Forms.Hist. HistForm )WindowsForm;
            HistForm.dataGridView.Columns.Add( "echeance" , "Echéance" );
            HistForm.dataGridView.Columns[ "echeance" ].Width = 68;
            HistForm.dataGridView.Columns[ "echeance" ].DataPropertyName = "echeance" ;
            HistForm.dataGridView.Columns[ "echeance" ].DefaultCellStyle.Format = Api .Iif(MercatorUi. Globals .DateMDY, "MM/dd/yyyy" , "dd/MM/yyyy" );    
        }

         /*
          * Cette troisième méthode est l'implémentation de l'interface IFormLoadCustomizer.
          * Comme cette méthode reçoit en paramètre un Form (fenêtre de base - car on peut utiliser cette interface pour divers écrans),
          * il faut d'abord effectuer un cast Form -> HistForm
          * Ensuite, on écrit un code qui sera exécuté à la fin de l'évènement Load de HistForm.
          * Ce code ajoute un évènement CellFormatting sur le dataGridView, dans lequel on met en gras toutes les dates d'échéance inférieures à la date du jour.
          */

         public void FormLoadCustomize( Form WindowsForm)
        {
            MercatorUi.Forms.Hist. HistForm HistForm = (MercatorUi.Forms.Hist. HistForm )WindowsForm;
            HistForm.dataGridView.CellFormatting += new DataGridViewCellFormattingEventHandler (dataGridView_CellFormatting);
        }

         public void FormClosedCustomize( Form WindowsForm)
        {
            MercatorUi.Forms.Hist. HistForm HistForm = (MercatorUi.Forms.Hist. HistForm )WindowsForm;
            HistForm.dataGridView.CellFormatting -= new DataGridViewCellFormattingEventHandler (dataGridView_CellFormatting);
        }

         void dataGridView_CellFormatting( object sender, DataGridViewCellFormattingEventArgs e)
        {
             DataGridView dataGridView = ( DataGridView )sender;
             if ((dataGridView.Columns[e.ColumnIndex].Name == "echeance" ) && (dataGridView.Rows[e.RowIndex].Cells[ "echeance" ].Value != DBNull .Value))
            {
                 if (System. Convert .ToDateTime(e.Value) < DateTime .Now)
                    e.CellStyle.Font = new Font (e.CellStyle.Font, FontStyle .Bold);
            }
        }
    }
}