using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorUi;
using System.Windows.Forms;

namespace SigStock
{
    public class Customizer : MercatorUi.ICustomizers.IStringUpdater, MercatorUi.ICustomizers.IFormLoadCustomizer, MercatorUi.ICustomizers.IFormClosedCustomizer
    {

        public string StringUpdate(string StringToModify)
        {
            string id = Api.StrExtract(StringToModify, "<ID>", "</ID>");
            if (id != "STOCK_INTERRO1") // Il ne faut agir que sur la requête de la grille 1 de StockInterro. Or, StringUpdate va voir passer toutes les requêtes de tous les objets et grille du signalétiques articles.
                return StringToModify; // Cette requête n'est pas celle de la grille 1 se StockInterro -> on ne la modifie pas
            else // attention : la requête varie en fonction des cases à cocher sélectionnée.
                return StringToModify.Replace(",pieds_v.date,", ",pieds_v.date,pieds_v.echeance,").Replace(",pieds_a.date,", ",pieds_a.date,pieds_a.echeance,").Replace(",lignes_d.date,", ",lignes_d.date,cast('01/01/1900' as datetime) as echeance,");
        }

        public void FormLoadCustomize(System.Windows.Forms.Form WindowsForm)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)WindowsForm;
            List<Control> l = sigForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Sig.SigObjects.StockInterro));
            if (l.Count > 0)
            {
             MercatorUi.Forms.Sig.SigObjects.StockInterro stockInterro = (MercatorUi.Forms.Sig.SigObjects.StockInterro)l[0];
             stockInterro.AfterColumnsCreated += new MercatorUi.Forms.Sig.SigObjects.StockInterro.AfterColumnsCreatedHandler(stockInterro_AfterColumnsCreated);
            }
        }
        public void FormClosedCustomize(System.Windows.Forms.Form WindowsForm)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)WindowsForm;
            List<Control> l = sigForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Sig.SigObjects.StockInterro));
            if (l.Count > 0)
            {
             MercatorUi.Forms.Sig.SigObjects.StockInterro stockInterro = (MercatorUi.Forms.Sig.SigObjects.StockInterro)l[0];
             stockInterro.AfterColumnsCreated -= new MercatorUi.Forms.Sig.SigObjects.StockInterro.AfterColumnsCreatedHandler(stockInterro_AfterColumnsCreated);
            }
        }
        private void stockInterro_AfterColumnsCreated(object sender, EventArgs e)
        {
            MercatorUi.GridPro.DataGridViewXPro grid = (MercatorUi.GridPro.DataGridViewXPro)sender;
            if (grid.Name == "grid1") // attention : StockInterro contient 2 grilles. Ici, on veut agir uniquement sur la grille 1. Ce même évènement va aussi voir passer la grille 2 (mais on ne la modifie pas)
            {
                grid.Columns.Add("echeance", _Divers.Iif_langue(Globals.Langue, "Due Date", "Vervald.", "Echéance"));
                grid.Columns["echeance"].Width = 68;
                grid.Columns["echeance"].DataPropertyName = "echeance";
                grid.Columns["echeance"].DefaultCellStyle.Format = Api.Iif(Globals.DateMDY, "MM/dd/yyyy", "dd/MM/yyyy");
            }
        }
    }
}