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

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

        public string StringUpdate(string StringToModify)
        {
            string id = Api.StrExtract(StringToModify, "<ID>", "</ID>");
            if (id.StartsWith("DASHBOARD_V"))
            {
                MercatorUi._Dialogs.DialogAskComboRet rep = Dialogs.AskRep("Welke vertegenwoordiger?", true);
                if ((rep != null) && (rep.Id != "")) // Allemaal : rep.Id=""
                {
                    StringToModify = StringToModify.Replace(" from lignes_v", string.Format(",'{0}' as id_rep,'{1}' as nom_rep from lignes_v", Api.UnquoteSql(rep.Id), Api.UnquoteSql(rep.Lib)))
                                                   .Replace("rayons.id) where ", string.Format("rayons.id) where (pieds_v.id_rep='{0}') and ", Api.UnquoteSql(rep.Id)));
                }
            }
            else if (id == "DASHBOARD_NODE")
            {
                Form f = Globals.Main.WonTopForm();
                if ((f != null) && (f is MercatorUi.Forms.Gescom.GescomDashboardForm))
                {
                    MercatorUi.Forms.Gescom.GescomDashboardForm gescomDashboardForm = (MercatorUi.Forms.Gescom.GescomDashboardForm)f;
                    if (gescomDashboardForm.Dt.Columns.Contains("id_rep"))
                    {
                        string id_rep = gescomDashboardForm.Dt.Rows[0]["id_rep"].ToString();
                        StringToModify = StringToModify.Replace("(pieds_v.date>=@date_1)", string.Format("(pieds_v.id_rep='{0}') and (pieds_v.date>=@date_1)", Api.UnquoteSql(id_rep)));
                    }
                }
            }
            return StringToModify;
        }

        public void FormLoadCustomize(System.Windows.Forms.Form WindowsForm)
        {
            if (WindowsForm is MercatorUi.Forms.Gescom.GescomDashboardForm)
            {
                MercatorUi.Forms.Gescom.GescomDashboardForm gescomDashboardForm = (MercatorUi.Forms.Gescom.GescomDashboardForm)WindowsForm;
                if (gescomDashboardForm.Dt.Columns.Contains("nom_rep"))
                    gescomDashboardForm.Text += " - Vertegenwoordiger: " + gescomDashboardForm.Dt.Rows[0]["nom_rep"].ToString();
            }
        }

    }
}