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

namespace Billing
{
    public class Customizer : MercatorUi.ICustomizers.IBillingEngineCreated, MercatorUi.ICustomizers.IBillingEngineClosed
    {
        public void BillingEngineCreated(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
        {
            BillingEngine.BillingFormLoaded += new EventHandler(BillingEngine_BillingFormLoaded);      
        }

        public void BillingEngineClosed(MercatorUi.Engine.Gescom.BillingEngine BillingEngine)
        {
            BillingEngine.BillingFormLoaded -= new EventHandler(BillingEngine_BillingFormLoaded);
        }

        void BillingEngine_BillingFormLoaded(object sender, EventArgs e)
        {
            MercatorUi.Engine.Gescom.BillingEngine billingEngine = (MercatorUi.Engine.Gescom.BillingEngine)sender;
            if (billingEngine.BillingForm.LinesEditor != null)
            {
                MercatorUi.GridPro.DataGridViewHidableButtonXColumn colButtonMemo = new MercatorUi.GridPro.DataGridViewHidableButtonXColumn();
                colButtonMemo.Name = "ButtonMemo";
                colButtonMemo.HeaderText = "";
                colButtonMemo.Width = 50;
                colButtonMemo.BeforeCellPaint += new EventHandler<DevComponents.DotNetBar.Controls.BeforeCellPaintEventArgs>(colButtonMemo_BeforeCellPaint);
                billingEngine.BillingForm.LinesEditor.Grid.Columns.Add(colButtonMemo);
                billingEngine.BillingForm.LinesEditor.Grid.CellSuperClick += new MercatorUi.GridPro.CellSuperClickHandler(Grid_CellSuperClick);
            }
        }

        void colButtonMemo_BeforeCellPaint(object sender, DevComponents.DotNetBar.Controls.BeforeCellPaintEventArgs e)
        {
            if ((e.ColumnIndex > -1) && (e.RowIndex > -1))
            {
                MercatorUi.GridPro.DataGridViewHidableButtonXColumn colButtonMemo = (MercatorUi.GridPro.DataGridViewHidableButtonXColumn)sender;
                DataTable dt = (DataTable)colButtonMemo.DataGridView.DataSource;
                if (dt.Rows[e.RowIndex]["memo"].ToString() != "")
                    colButtonMemo.DataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Api.Iif_langue(Globals.Langue, "Merkz.", "Memo", "Memo", "Mémo");
                else
                    colButtonMemo.DataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Api.Iif_langue(Globals.Langue, "merkz.", "memo", "memo", "mémo");
            }
        }

        void Grid_CellSuperClick(object sender, DataGridViewCellEventArgs e)
        {
            MercatorUi.GridPro.DataGridViewXPro grid = (MercatorUi.GridPro.DataGridViewXPro)sender;
            if ((e.ColumnIndex > -1) && (e.RowIndex > -1) && (grid.Columns[e.ColumnIndex].Name == "ButtonMemo"))
            {
                DataTable dt = (DataTable)grid.DataSource;
                string memo = Dialogs.AskMemo(string.Format("Memo ligne {0} ?", e.RowIndex + 1), dt.Rows[e.RowIndex]["memo"].ToString(), grid.FindForm());
                if (!dt.Columns["memo"].ReadOnly && (memo != null)) // er-werd niet op 'Annuleren' geklikt
                    dt.Rows[e.RowIndex]["memo"] = memo.Trim();
            }
        }
    }
}