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

namespace BrowseStock
{
    public class Customizer : MercatorUi.ICustomizers.IStringUpdater, MercatorUi.ICustomizers.IFormGridCustomizer, MercatorUi.ICustomizers.IFormClosedCustomizer
    {

        public string StringUpdate(string stringToModify)
        {
            string id = Api.StrExtract(stringToModify, "<ID>", "</ID>");
            if (id.StartsWith("LAST_MVTS_"))
                stringToModify = stringToModify.Replace(", date", ", date, pu").Replace(", p.date", ", p.date, l.pu");
            return stringToModify;
        }

        public void FormGridCustomize(Form form)
        {
            MercatorUi.Forms.Sig.SigBrowseForm sigBrowseForm = (MercatorUi.Forms.Sig.SigBrowseForm)form;
            sigBrowseForm.LastMvts += sigBrowseForm_LastMvts;
        }

        void sigBrowseForm_LastMvts(object sender, MercatorUi.Forms.Sig.SigBrowseForm.LastMvtsEventArgs e)
        {
            MercatorUi.Forms.Sig.SigBrowseForm sigBrowseForm = (MercatorUi.Forms.Sig.SigBrowseForm)sender;
            MercatorUi.GridPro.DataGridViewMultiColsRowsColumn colLastMvts = (MercatorUi.GridPro.DataGridViewMultiColsRowsColumn)sigBrowseForm.dataGridView.Columns["LastMvts"];
            // Extra subkolom toevoegen aan de laatste kolom LastMvts
            if (colLastMvts.Columns.Count == 2)
                colLastMvts.Columns.Add(new MercatorUi.GridPro.DataGridViewMultiColsRowsColumn.Column(StringAlignment.Far, 60));

            e.RowInList["LastMvts"] = string.Join("\n", e.RowsLastMvts.Select(p =>
                Convert.ToDateTime(p["date"]).ToShortDateString()
                    + "\t" + Api.Transform(Convert.ToDouble(p["q"]), Globals.PictQ)
                    + "\t" + Api.Transform(Convert.ToDouble(p["pu"]), "### ##0.00")
                ));
        }

        public void FormClosedCustomize(Form form)
        {
            MercatorUi.Forms.Sig.SigBrowseForm sigBrowseForm = (MercatorUi.Forms.Sig.SigBrowseForm)form;
            sigBrowseForm.LastMvts -= sigBrowseForm_LastMvts;
        }
    }
}