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

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

        private Size desiredSize = new Size(80, 80);

        public void FormLoadCustomize(Form form)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)form;
            MercatorUi.MovableControls.MovablePictureBox pictureBox = (MercatorUi.MovableControls.MovablePictureBox)sigForm.MovableControls["1CD6B224F1"];
            pictureBox.PictureSelected += new EventHandler(pictureBox_PictureSelected);
        }

        public void FormClosedCustomize(Form form)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)form;
            MercatorUi.MovableControls.MovablePictureBox pictureBox = (MercatorUi.MovableControls.MovablePictureBox)sigForm.MovableControls["1CD6B224F1"];
            pictureBox.PictureSelected -= new EventHandler(pictureBox_PictureSelected);
        }

        private void pictureBox_PictureSelected(object sender, EventArgs e)
        {
            MercatorUi.MovableControls.MovablePictureBox pictureBox = (MercatorUi.MovableControls.MovablePictureBox)sender;
            if ((pictureBox.BinaryValue != null) && (pictureBox.BinaryValue is byte[]) && (((byte[])pictureBox.BinaryValue).Length > 0))
            {
                Image img = Api.BytesToImage((byte[])pictureBox.BinaryValue);
                if ((img.Width == desiredSize.Width) && (img.Height == desiredSize.Height))
                    return; // l'image est déjà à la bonne dimension
                Image img2 = _Divers.ResizeImage(img, desiredSize);
                pictureBox.BinaryValue = Api.ImageToBytes(img2);
                Dialogs.Stop(string.Format("L'image a été redimensionnée : {0}x{1} !", desiredSize.Width, desiredSize.Height));
            }
        }

    }
}