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

// <CompileWithRoslyn />

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("Quel représentant ?", true);
                if ((rep != null) && (rep.Id != "")) // Tous : 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 is MercatorUi.Forms.Gescom.GescomDashboardForm gescomDashboardForm)
                {
                    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(Form form)
        {
            if (form is MercatorUi.Forms.Gescom.GescomDashboardForm gescomDashboardForm)
            {
                if (gescomDashboardForm.Dt.Columns.Contains("nom_rep"))
                    gescomDashboardForm.Text += " - Représentant : " + gescomDashboardForm.Dt.Rows[0]["nom_rep"].ToString();
            }
        }
    }
}