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

namespace SigStock
{
    public class Customizer : MercatorUi.ICustomizers.IFormLoadCustomizer, MercatorUi.ICustomizers.IFormClosedCustomizer
    {

        public void FormLoadCustomize(System.Windows.Forms.Form WindowsForm)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)WindowsForm;
            List<Control> l = sigForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Sig.SigGrids.ArtFou));
            if (l.Count == 0)
                return;
            MercatorUi.Forms.Sig.SigGrids.ArtFou artFou = (MercatorUi.Forms.Sig.SigGrids.ArtFou)l[0];
            artFou.AfterColumnsCreated += new MercatorUi.MovableControls.MovableGrid.AfterColumnsCreatedHandler(Artfou_AfterColumnsCreated);
            artFou.Grid.CellFormatting += new DataGridViewCellFormattingEventHandler(Artfou_CellFormatting);
            artFou.Grid.CellValidated += new DataGridViewCellEventHandler(Artfou_CellValidated);
        }

        public void FormClosedCustomize(System.Windows.Forms.Form WindowsForm)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)WindowsForm;
            List<Control> l = sigForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Sig.SigGrids.ArtFou));
            if (l.Count == 0)
                return;
            MercatorUi.Forms.Sig.SigGrids.ArtFou artFou = (MercatorUi.Forms.Sig.SigGrids.ArtFou)l[0];
            artFou.AfterColumnsCreated -= new MercatorUi.MovableControls.MovableGrid.AfterColumnsCreatedHandler(Artfou_AfterColumnsCreated);
            artFou.Grid.CellFormatting -= new DataGridViewCellFormattingEventHandler(Artfou_CellFormatting);
            artFou.Grid.CellValidated -= new DataGridViewCellEventHandler(Artfou_CellValidated);
        }

        private void Artfou_AfterColumnsCreated(Object sender, EventArgs e) // De kolom "Nettoprijs" toevoegen
        {
            MercatorUi.Forms.Sig.SigGrids.ArtFou artFou = (MercatorUi.Forms.Sig.SigGrids.ArtFou)sender;
            artFou.Grid.Columns.Add("prix_net", _Divers.Iif_langue(Globals.Langue, "Net Pr.", "Nettoprijs", "Prix net"));
            artFou.Grid.Columns["prix_net"].Width = 85;
            artFou.Grid.Columns["prix_net"].ReadOnly = true;
            artFou.Grid.Columns["prix_net"].DefaultCellStyle.ForeColor = System.Drawing.Color.Gray;
            artFou.Grid.Columns["prix_net"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            // Deze kolom is bewust niet gelinkt aan een DataPropertyName.
            // Ze zal worden berekend via het event CellFormatting van het rooster.

        }

        private void Artfou_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView grid = (DataGridView)sender;
            if ((e.ColumnIndex > -1) && (e.RowIndex > -1) && (grid.Columns[e.ColumnIndex].Name == "prix_net") && grid.Columns.Contains("prix") && grid.Columns.Contains("remise") && grid.Columns.Contains("n_dec"))
                refreshPrixNetCell(grid, e.RowIndex, e);
        }

        private void refreshPrixNetCell(DataGridView grid, int rowIndex, DataGridViewCellFormattingEventArgs e)
        {
            double prix = System.Convert.ToDouble(grid.Rows[rowIndex].Cells["prix"].Value);
            double remise = System.Convert.ToDouble(grid.Rows[rowIndex].Cells["remise"].Value);
            double remise2 = 0;
            if (grid.Columns.Contains("remise2"))
                remise2 = System.Convert.ToDouble(grid.Rows[rowIndex].Cells["remise2"].Value);
            double remise3 = 0;
            if (grid.Columns.Contains("remise3"))
                remise3 = System.Convert.ToDouble(grid.Rows[rowIndex].Cells["remise3"].Value);
            double remise4 = 0;
            if (grid.Columns.Contains("remise4"))
                remise4 = System.Convert.ToDouble(grid.Rows[rowIndex].Cells["remise4"].Value);
            int n_dec = System.Convert.ToInt32(grid.Rows[rowIndex].Cells["n_dec"].Value);
            string mask = "# ### ### ##0" + Api.Iif(n_dec > 0, ".", "") + Api.Replicate("0", n_dec);
            string prix_net = string.Format("{0:" + mask + "}", Math.Round(prix * ((100 - remise) / 100) * ((100 - remise2) / 100) * ((100 - remise3) / 100) * ((100 - remise4) / 100), 2));
            if (e != null)
                e.Value = prix_net; // Men komt van Artfou_CellFormatting.
            else
                grid.Rows[rowIndex].Cells["prix_net"].Value = prix_net; // Men komt van Artfou_CellValidated.
        }

        private void Artfou_CellValidated(object sender, DataGridViewCellEventArgs e) // Als men de content wijzigt van prijs, korting ... -> moet men de nettoprijs op deze lijn aanpassen.
        {
            DataGridView grid = (DataGridView)sender;
            if ((e.ColumnIndex > -1) && (e.RowIndex > -1) && ((grid.Columns[e.ColumnIndex].Name == "prix") || (grid.Columns[e.ColumnIndex].Name == "remise") || (grid.Columns[e.ColumnIndex].Name == "remise2") || (grid.Columns[e.ColumnIndex].Name == "remise3") || (grid.Columns[e.ColumnIndex].Name == "remise4")))
                refreshPrixNetCell(grid, e.RowIndex, null);
        }


    }
}