using System;
using System.Collections.Generic;
using System.Text;
using MercatorApi;
using MercatorExtensions;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Runtime.InteropServices;

// <CompileWithRoslyn />

namespace HistCli
{
    public class Customizer : MercatorUi.ICustomizers.IFormLoadCustomizer, MercatorUi.ICustomizers.IFormClosedCustomizer
    {
        public void FormLoadCustomize(Form form)
        {
            MercatorUi.Forms.Hist.HistForm histForm = (MercatorUi.Forms.Hist.HistForm)form;
            histForm.dataGridView.CellMouseMove += DataGridView_CellMouseMove;
            histForm.Move += HistForm_Move;
            histForm.FormClosing += HistForm_FormClosing;
        }

        public void FormClosedCustomize(Form form)
        {
            MercatorUi.Forms.Hist.HistForm histForm = (MercatorUi.Forms.Hist.HistForm)form;
            histForm.dataGridView.CellMouseMove -= DataGridView_CellMouseMove;
            histForm.Move -= HistForm_Move;
            histForm.FormClosing -= HistForm_FormClosing;
        }

        void DataGridView_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            MercatorUi.GridPro.DataGridViewXPro grid = (MercatorUi.GridPro.DataGridViewXPro)sender;
            MercatorUi.Forms.Hist.HistForm histForm = (MercatorUi.Forms.Hist.HistForm)grid.FindForm();
            IntPtr activeHandle = MercatorWM.WMdll.GetActiveWindow();
            if ((activeHandle == histForm.Handle) || (activeHandle == MercatorUi.Wait.WaitStatic.CurrentHandle))
            {
                if ((e.RowIndex > -1) && (e.ColumnIndex <= 5) && (grid.Rows[e.RowIndex].Cells["piece"].Value != DBNull.Value))
                {
                    string id = grid.Rows[e.RowIndex].Cells["id"].Value.ToString();
                    string journal = grid.Rows[e.RowIndex].Cells["journal"].Value.ToString();
                    Int64 piece = Convert.ToInt64(grid.Rows[e.RowIndex].Cells["piece"].Value);
                    if (histForm.Tag?.ToString() != id + journal + piece)
                    {
                        Point p = new Point();
                        GetCursorPos(ref p);
                        StringBuilder sb = new StringBuilder("<p><strong>" + journal.TrimEnd() + " " + piece + "</strong></p>");
                        DataSet ds = Api.Zselect(MercatorUi.Globals.RepData, "select top 5 q,designatio from lignes_v (NOLOCK) where (id=@id) and (journal=@journal) and (piece=@piece) and (designatio<>'') order by recno \r\n select count(*) as nbre from lignes_v (NOLOCK) where (id=@id) and (journal=@journal) and (piece=@piece) and (designatio<>'')",
                            new MercatorSqlParam("@id", id, SqlDbType.Char),
                            new MercatorSqlParam("@journal", journal, SqlDbType.Char),
                            new MercatorSqlParam("@piece", piece));
                        if (ds != null)
                        {
                            foreach (DataRow dr in ds.Tables[0].Rows)
                            {
                                if (dr.Value<double>("q").CompareTo(0d, MercatorUi.Globals.N_DEC_Q) == 0)
                                    sb.Append(dr["designatio"].ToString().TrimEnd() + "<br />");
                                else
                                    sb.Append(dr.Value<double>("q").ToString("### ##0.00").TrimStart() + " " + dr["designatio"].ToString().TrimEnd() + "<br />");
                            }
                            if (ds.Tables[1].Rows[0].Value<int>("nbre") > 5)
                                sb.Append("...");
                        }
                        if (sb.EndsWith("<br />"))
                            sb.Length -= 6;
                        MercatorUi.Wait.WaitStatic.WaitWindowBaseThread(sb.ToString(), p, 3);
                        histForm.Tag = id + journal + piece;
                    }
                }
            }
        }

        [DllImport("user32.dll")]
        static extern bool GetCursorPos(ref Point lpPoint);

        void HistForm_Move(object sender, EventArgs e)
        {
            MercatorUi.Wait.WaitStatic.WaitClearBaseThread();
        }

        void HistForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MercatorUi.Wait.WaitStatic.WaitClearBaseThread();
        }
    }
}