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) // ajouter la colonne "Prix Net"
        {
            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;
             // Cette colonne est volontairement non liée à un DataPropertyName.
             // Elle sera calculée par l'évènement CellFormatting de la grille

        }

         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; // on vient de Artfou_CellFormatting
             else
                grid.Rows[rowIndex].Cells[ "prix_net" ].Value = prix_net; // on vient de Artfou_CellValidated
        }

         private void Artfou_CellValidated( object sender, DataGridViewCellEventArgs e) // si on change le contenu de prix, remise, ... -> il faut adapter le prix net sur cette ligne
        {
             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 );
        }


    }
}