using System;
using System.Collections.Generic;
using System.Text;
using MercatorApi;
using MercatorUi;
using System.Linq;
using System.Drawing;

// <CompileWithRoslyn />

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

        public void FormLoadCustomize(System.Windows.Forms.Form form)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)form;
            MercatorUi.MovableControls.MovableSqlFileView movableSqlFileView = sigForm.MovableControls.Values.OfType<MercatorUi.MovableControls.MovableSqlFileView>().FirstOrDefault();
            if (movableSqlFileView != null)
                movableSqlFileView.UploadingFile += MovableSqlFileView_UploadingFile;
        }

        public void FormClosedCustomize(System.Windows.Forms.Form form)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)form;
            MercatorUi.MovableControls.MovableSqlFileView movableSqlFileView = sigForm.MovableControls.Values.OfType<MercatorUi.MovableControls.MovableSqlFileView>().FirstOrDefault();
            if (movableSqlFileView != null)
                movableSqlFileView.UploadingFile -= MovableSqlFileView_UploadingFile;
        }

        const int MAX_KB = 1024;
        const int MAX_WIDTH = 800;
        const int MAX_HEIGHT = 800;

        private void MovableSqlFileView_UploadingFile(object sender, MercatorUi._BaseClasses.SqlFileView.UploadingFileEventArgs e)
        {
            if (e.FileContent.Length / 1024 > MAX_KB)
            {
                if (e.FileName.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase)
                       || e.FileName.EndsWith(
".jpg", StringComparison.InvariantCultureIgnoreCase)
                       || e.FileName.EndsWith(
".png", StringComparison.InvariantCultureIgnoreCase))
                {
                    try
                    {
                        using (Bitmap bitmap = new Bitmap(e.Node.LocalFileName))
                        {
                            if ((bitmap.Width > MAX_WIDTH) || (bitmap.Height > MAX_HEIGHT))
                            {
                                var newImage = Api.ResizeImage(bitmap, new Size(MAX_WIDTH, MAX_HEIGHT));
                                
                                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
                                {
                                    if (e.FileName.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase) || e.FileName.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase))
                                        newImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                                    else
                                        newImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                                    e.FileContent = memoryStream.ToArray();
                                }
                            }
                        }
                        Dialogs.Stop($"L'image \"{e.FileName}\" a été redimensionnée en {MAX_WIDTH}x{MAX_HEIGHT} !");
                    }
                    catch (Exception ex)
                    {
                        Dialogs.Stop(ex.Message);
                        e.Cancel = true;
                        return;
                    }
                }
                else
                {
                    Dialogs.Stop($"Le fichier \"{e.FileName}\" a une taille supérieure à {MAX_KB} kb !");
                    e.Cancel = true;
                }
            }
        }
    }
}