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

// <ReferenceInclude>"WidgetPosTouch.dll"</ReferenceInclude>

namespace Billing
{
    public class Customizer : MercatorUi.ICustomizers.IBillingEngineCreated
    {

        public void BillingEngineCreated(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;
            billingEngine.BillingFormLoaded -= new EventHandler(BillingEngine_BillingFormLoaded);
            foreach (Control c in billingEngine.BillingForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Billing.BillingObjects.TouchSelector)))
            {
                MercatorUi.Forms.Billing.BillingObjects.TouchSelector touchSelector = (MercatorUi.Forms.Billing.BillingObjects.TouchSelector)c;
                touchSelector.AddingButtons += new MercatorUi.Forms.Billing.BillingObjects.TouchSelector.AddingButtonsEventHandler(touchSelector_AddingButtons);
                touchSelector.ButtonTouchAdded += new MercatorUi.Forms.Billing.BillingObjects.TouchSelector.ButtonTouchAddedEventHandler(touchSelectorOrList_ButtonTouchAdded);
            }
            foreach (Control c in billingEngine.BillingForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Billing.BillingObjects.TouchList)))
            {
                MercatorUi.Forms.Billing.BillingObjects.TouchList touchList = (MercatorUi.Forms.Billing.BillingObjects.TouchList)c;
                touchList.ButtonTouchAdded += new MercatorUi.Forms.Billing.BillingObjects.TouchSelector.ButtonTouchAddedEventHandler(touchSelectorOrList_ButtonTouchAdded);
                touchList.AfterItemButtonClick += new MercatorUi.Forms.Billing.BillingObjects.TouchList.AfterItemButtonClickEventHandler(touchList_AfterItemButtonClick);
            }
            billingEngine.BillingForm.FormClosed += new FormClosedEventHandler(BillingForm_FormClosed);

            foreach (MercatorUi.Widgets.WidgetBaseControl widget in MercatorUi.Globals.ListWidgets)
            {
                if (widget is WidgetPosTouch.UserControlPosTouch)
                {
                    ((WidgetPosTouch.UserControlPosTouch)widget).RegisterBilling(billingEngine.BillingForm);
                    break;
                }
            }
        }

        void BillingForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MercatorUi.Forms.Billing.BillingForm billingForm = (MercatorUi.Forms.Billing.BillingForm)sender;
            foreach (Control c in billingForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Billing.BillingObjects.TouchSelector)))
            {
                MercatorUi.Forms.Billing.BillingObjects.TouchSelector touchSelector = (MercatorUi.Forms.Billing.BillingObjects.TouchSelector)c;
                touchSelector.AddingButtons -= new MercatorUi.Forms.Billing.BillingObjects.TouchSelector.AddingButtonsEventHandler(touchSelector_AddingButtons);
                touchSelector.ButtonTouchAdded -= new MercatorUi.Forms.Billing.BillingObjects.TouchSelector.ButtonTouchAddedEventHandler(touchSelectorOrList_ButtonTouchAdded);
            }
            foreach (Control c in billingForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Billing.BillingObjects.TouchList)))
            {
                MercatorUi.Forms.Billing.BillingObjects.TouchList touchList = (MercatorUi.Forms.Billing.BillingObjects.TouchList)c;
                touchList.ButtonTouchAdded -= new MercatorUi.Forms.Billing.BillingObjects.TouchSelector.ButtonTouchAddedEventHandler(touchSelectorOrList_ButtonTouchAdded);
                touchList.AfterItemButtonClick -= new MercatorUi.Forms.Billing.BillingObjects.TouchList.AfterItemButtonClickEventHandler(touchList_AfterItemButtonClick);
            }
            billingForm.FormClosed -= new FormClosedEventHandler(BillingForm_FormClosed);
        }

        void touchSelector_AddingButtons(object sender, MercatorUi.Forms.Billing.BillingObjects.TouchSelector.AddingButtonsEventArgs e)
        {
            //Dialogs.Stop(e.ReqSql.ToString());
            e.ReqSql = e.ReqSql.Replace("s_id as id,", "s_id as id,s_prix_ti,s_cle4,s_cle1,s_qv_3,s_qdispo,");
            e.ReqSql = e.ReqSql.Replace("order by dbo.XDESIGN_F(s_id)", "order by s_cle1");
        }

        void touchSelectorOrList_ButtonTouchAdded(object sender, MercatorUi.Forms.Billing.BillingObjects.TouchSelector.ButtonTouchAddedEventArgs e)
        {
            if (e.ButtonTouch.Niveau == MercatorUi.Forms.Billing.BillingObjects.TouchSelector.TouchSelectorNiveau.ART)
            {
                DataSet ds = Api.Zselect(MercatorUi.Globals.RepData, string.Format("select dbo.XDESIGN_{0}(s_id) as design,s_prix_ti from STOCK (NOLOCK) where s_id=@s_id", MercatorUi.Globals.Langue), new MercatorSqlParam("@s_id", e.ButtonTouch.Name, SqlDbType.Char, 10));
                if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                {
                    Label labelDesign = new Label();
                    labelDesign.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    labelDesign.Text = ds.Tables[0].Rows[0]["design"].ToString().TrimEnd();
                    labelDesign.AutoSize = true;
                    labelDesign.BackColor = System.Drawing.Color.Transparent;
                    labelDesign.Location = new System.Drawing.Point(5, 3);
                    e.ButtonTouch.Controls.Add(labelDesign);

                    Label labelPrix = new Label();
                    labelPrix.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    labelPrix.Text = Api.Transform(Convert.ToDouble(ds.Tables[0].Rows[0]["s_prix_ti"]), "### ##0.00") + " €";
                    labelPrix.AutoSize = false;
                    labelPrix.Size = new System.Drawing.Size(100, 14);
                    labelPrix.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
                    labelPrix.BackColor = System.Drawing.Color.Transparent;
                    labelPrix.Location = new System.Drawing.Point(5, 95);
                    e.ButtonTouch.Controls.Add(labelPrix);
                }
            }
            else if (e.ButtonTouch.Text != "") // sur Rayons / Familles -> on regarde si l'image existe dans les fichiers SQL / Autres
            {
                Label labelNom = null;
                byte[] bytes = Api.SqlFileToBytes("<Other\\" + e.ButtonTouch.Text + ".png");
                if ((bytes != null) && (bytes.Length > 0))
                {
                    e.ButtonTouch.Image = Api.BytesToImage(bytes);
                    labelNom = new Label();
                    labelNom.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    labelNom.Text = e.ButtonTouch.Text;
                    labelNom.AutoSize = true;
                    labelNom.BackColor = System.Drawing.Color.Transparent;
                    labelNom.Location = new System.Drawing.Point(5, 2);
                    e.ButtonTouch.Controls.Add(labelNom);
                    e.ButtonTouch.Text = "";
                }

                if (e.ButtonTouch.Niveau == MercatorUi.Forms.Billing.BillingObjects.TouchSelector.TouchSelectorNiveau.FAMILLE)
                {
                    e.ButtonTouch.BackColor = System.Drawing.Color.IndianRed;
                    e.ButtonTouch.ColorTable = DevComponents.DotNetBar.eButtonColor.Flat;
                    if (labelNom != null)
                        labelNom.ForeColor = System.Drawing.Color.White;
                }
                else if (e.ButtonTouch.Niveau == MercatorUi.Forms.Billing.BillingObjects.TouchSelector.TouchSelectorNiveau.SSFAMILLE)
                {
                    e.ButtonTouch.BackColor = System.Drawing.Color.DarkOrange;
                    e.ButtonTouch.ColorTable = DevComponents.DotNetBar.eButtonColor.Flat;
                    if (labelNom != null)
                        labelNom.ForeColor = System.Drawing.Color.White;
                }
            }
        }

        void touchList_AfterItemButtonClick(object sender, MercatorUi.Forms.Billing.BillingObjects.TouchList.AfterItemButtonClickEventArgs e)
        {
            // on vient d'ajouter un article dans l'onglet qui contient l'objet de modes de paiements -> le mettre à jour
            MercatorUi.Forms.Billing.BillingObjects.TouchList touchList = (MercatorUi.Forms.Billing.BillingObjects.TouchList)sender;
            MercatorUi.Forms.Billing.BillingForm billingForm = (MercatorUi.Forms.Billing.BillingForm)touchList.Form;
            billingForm.UpdatePayments();
        }
    }
}