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

namespace Main
{
    public class Customizer : MercatorUi.ICustomizers.IExec
    {
        public void Main(MercatorUi.ICustomizers.ExecAction action)
        {
            if (action == MercatorUi.ICustomizers.ExecAction.DossierOpen)
            {
                Globals.Main.BaseFormCreating += Main_BaseFormCreating;
            }
            else if (action == MercatorUi.ICustomizers.ExecAction.DossierClose)
            {
                Globals.Main.BaseFormCreating -= Main_BaseFormCreating;
            }
        }

        void Main_BaseFormCreating(object sender, MercatorUi.Main.BaseFormCreatingEventArgs e)
        {
            if ((e.Form is MercatorUi.Forms.Accounting.AccountingBankVirementForm)
                && (Globals.CurrentUserRecord != null)
                && (Globals.CurrentUserRecord.NOM != "Guy")) // si l'utilisateur n'est pas Guy, on va mettre en place les évènements.  Sinon, on ne fait rien.
            {
                e.Form.Shown += Form_Shown;
            }
        }

        void Form_Shown(object sender, EventArgs e)
        {
            // Désactiver le bouton "Enregistrer"
            MercatorUi.Forms.Accounting.AccountingBankVirementForm accountingBankVirementForm = (MercatorUi.Forms.Accounting.AccountingBankVirementForm)sender;
            accountingBankVirementForm.Shown -= Form_Shown; // désinscrire l'évènement
            MercatorUi.BoutonsPro.BleuPro bleuPro1 = (MercatorUi.BoutonsPro.BleuPro)accountingBankVirementForm.Controls["bleuPro1"];
            MercatorUi.BoutonsPro.ButtonXPro buttonXProSave = (MercatorUi.BoutonsPro.ButtonXPro)bleuPro1.Controls["buttonXProSave"];
            buttonXProSave.Enabled = false;

            // Désactiver le sous-bouton "Fiche"
            MercatorUi.BoutonsPro.ButtonXPro buttonBrowsePieceliees = (MercatorUi.BoutonsPro.ButtonXPro)bleuPro1.Controls["buttonBrowsePieceliees"];
            buttonBrowsePieceliees.SubItems[0].Enabled = false;

            // Masquer la case à cocher "Compte propre"
            Control checkBoxProper = accountingBankVirementForm.Controls["checkBoxProper"];
            checkBoxProper.Visible = false;

            // Masquer le combo qui permet d'appeler un compte client, fournisseur, général
            Control comboAppelSig = accountingBankVirementForm.Controls["comboAppelSig"];
            comboAppelSig.Visible = false;

            // Mettre en readonly tous les contrôles
            foreach (Control c in accountingBankVirementForm.Controls)
            {
                c.SetReadOnlyOrDisable();
            }
            Dialogs.Stop("Vous n'avez pas l'autorisation de modifier les données de cet écran !");
        }

    }
}