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


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)
            {
                BindingList<MercatorUi._BaseClasses.MercatorComboItem> l = new BindingList<MercatorUi._BaseClasses.MercatorComboItem>();
                foreach(DataRow dr in billingEngine.LIGNES.Rows)
                {
                    string usage = dr["usage"].ToString();
                    if ((usage != "") && !itemExistsInList(l, usage))
                        l.Add(new MercatorUi._BaseClasses.MercatorComboItem(usage, usage));
                }
                DevComponents.DotNetBar.Controls.DataGridViewComboBoxExColumn comboCol = new DevComponents.DotNetBar.Controls.DataGridViewComboBoxExColumn();
                comboCol.Name = "usage";
                comboCol.HeaderText = "Usage";
                comboCol.DataPropertyName = "usage";
                comboCol.DisplayMember = "Description";
                comboCol.ValueMember = "Id";
                comboCol.DataSource = l;
                comboCol.Width = 100;
                comboCol.DropDownStyle = ComboBoxStyle.DropDownList;
                billingEngine.BillingForm.LinesEditor.Grid.Columns.Add(comboCol);
                billingEngine.BillingForm.LinesEditor.Grid.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(Grid_EditingControlShowing);
            }
        }

        void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            MercatorUi.GridPro.DataGridViewXPro grid = (MercatorUi.GridPro.DataGridViewXPro)sender;
            if ((e.Control is DevComponents.DotNetBar.Controls.ComboBoxEx) && (grid.Columns[grid.CurrentCell.ColumnIndex].Name == "usage"))
            {
                DevComponents.DotNetBar.Controls.DataGridViewComboBoxExColumn comboCol = (DevComponents.DotNetBar.Controls.DataGridViewComboBoxExColumn)grid.Columns[grid.CurrentCell.ColumnIndex];
                BindingList<MercatorUi._BaseClasses.MercatorComboItem> listOnCol = (BindingList<MercatorUi._BaseClasses.MercatorComboItem>)comboCol.DataSource;
                
                
                DevComponents.DotNetBar.Controls.ComboBoxEx combo = (DevComponents.DotNetBar.Controls.ComboBoxEx)e.Control;
                string id_article = grid.CurrentRow.Cells["id_article"].Value.ToString();
                List<MercatorUi._BaseClasses.MercatorComboItem> l = new List<MercatorUi._BaseClasses.MercatorComboItem>();
                if (id_article != "")
                {
                    string s_usages = MercatorController.xFunctions.xLookUpString("STOCK", "S_ID", id_article, "S_USAGES");
                    foreach (string s in s_usages.Split(new char[2] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        l.Add(new MercatorUi._BaseClasses.MercatorComboItem(s, s));
                        if (!itemExistsInList(listOnCol, s))
                            listOnCol.Add(new MercatorUi._BaseClasses.MercatorComboItem(s, s));
                    }
                    l.Add(new MercatorUi._BaseClasses.MercatorComboItem("", "")); // ajouter un élément vide dans le combo
                }
                combo.DataSource = l;
            }
        }

        private bool itemExistsInList(BindingList<MercatorUi._BaseClasses.MercatorComboItem> l, string s)
        {
            foreach (MercatorUi._BaseClasses.MercatorComboItem item in l)
            {
                if (item.Id == s)
                    return true;
            }
            return false;
        }
    }
}