using SanShared;
using Syncfusion.DocIO.DLS;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BerichtGen
{
    public  class Bericht
    {
        private List<Image> _listImages;
        /// <summary>
        /// Erstellt
        /// </summary>
        /// <param name="vorlage">Die zuverwendete Vorlagenname</param>
        /// <param name="documentname">Die name zum Speichern der Datei</param>
        /// <param name="savepath">Pfad zum Speichern</param>
        /// <param name="daten">Inhalt vom Dokument </param>
        /// <param name="erzeugeDoc">Ein doc datei soll erzeugt werden</param>
        /// <param name="erzeugePDF">Ein Pdf datei soll erzeugt werden</param>
        public void Erzeuge(string firma, string vorlage, string savepath, Hashtable daten,List<BilderObject> bilderObjects, bool erzeugeDOC = false , bool erzeugePDF = true)
        {
            _listImages = new List<Image>();
            foreach(BilderObject current in bilderObjects)
            {
                Image image = Image.FromFile(current.Path);
                _listImages.Add(ResizeImage(image, CalculateImageSizeForDocument(image.Height, image.Width)));
                image.Dispose();
            }

            WordDocument wordDocument = new WordDocument("./documents/JUME/liner_einbau.docx");

            string[] fieldnames = null;
            string[] fieldvalues = null;

            if(fieldnames == null || fieldvalues == null)
            {
                fieldnames = new string[daten.Count];
                fieldvalues = new string[daten.Count];
            }

            uint counter = 0;
            foreach(DictionaryEntry hashtable in daten)
            {
                fieldnames[counter] = hashtable.Key.ToString();
                if (hashtable.Value == null)
                {
                    fieldvalues[counter] = "";
                }
                else
                {
                    fieldvalues[counter] = hashtable.Value.ToString();
                }
                counter++;
            }
            wordDocument.MailMerge.Execute(fieldnames, fieldvalues);
            wordDocument.MailMerge.MergeImageField += new MergeImageFieldEventHandler(this.MailMerge_MergeImageField);

        }
        private readonly double _cmPixel = 0.393700787;
        private readonly int _dpi = 120;
        private readonly double _imgWidthCmMax = 8.0;

        private Size CalculateImageSizeForDocument(int height, int width)
        {
            double num = (double)height / (double)width;
            double num2 = this._imgWidthCmMax * num;
            int h = (int)(num2 * _cmPixel * (double)_dpi);
            int w = (int)(_imgWidthCmMax * _cmPixel * (double)_dpi);
            return new Size(w, h);
        }

        private Image ResizeImage(Image image, Size size)
        {
            int width = image.Width;
            int height = image.Height;
            float num = (float)size.Width / (float)width;
            float num2 = (float)size.Height / (float)height;
            float num3 = (num2 < num) ? num2 : num;
            int width2 = (int)((float)width * num3);
            int height2 = (int)((float)height * num3);
            Bitmap bitmap = new Bitmap(width2, height2);
            try
            {
                bitmap.SetResolution(120f, 120f);
                Graphics graphics = Graphics.FromImage(bitmap);
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                graphics.DrawImage(image, 0, 0, width2, height2);
                graphics.Dispose();

            }
            catch(Exception ex)
            {

            }
            finally
            {
                System.GC.Collect();
            }
            return bitmap;
        }

        private void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs args)
        {

        }



    }
}
