using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WIA; //WIA Interop- Typen einbetten -> False!
using System.IO;
using System.Windows.Forms;

namespace Foto_Scanner_New
{
    /// <summary>
    /// Utils and default valus to connect and set setting for the scanner and to save images temporarly.
    /// </summary>
    class ScanUtils
    {
        // -Default values------------------------------------------------------------
        public const string FILE_PATH_SCANNED_IMAGE = "/tmp/tmp_scan" + ".png";
        // ---------------------------------------------------------------------------
        
        
        // -Methods-------------------------------------------------------------------

        /// <summary>
        /// Set scanner settings
        /// </summary>
        /// <param name="scannnerItem">Selected scanner</param>
        /// <param name="scanResolutionDPI">Resolution in DPI</param>
        /// <param name="scanStartLeftPixel">Pixel x- position start</param>
        /// <param name="scanStartTopPixel">Pixel y- position start</param>
        /// <param name="scanWidthPixels">Pixel x- position end</param>
        /// <param name="scanHeightPixels">Pixel y- position end</param>
        /// <param name="brightnessPercents">Brightness in pertentage</param>
        /// <param name="contrastPercents">Contrast in pertentage</param>
        public static void SetSannerSettings(IItem scannnerItem, int scanResolutionDPI, int scanStartLeftPixel, int scanStartTopPixel,
	            int scanWidthPixels, int scanHeightPixels, int brightnessPercents, int contrastPercents, int MODE, double _hoehe, double _breite)
	    {
            const string WIA_HORIZONTAL_SCAN_START_PIXEL = "6149";
            const string WIA_VERTICAL_SCAN_START_PIXEL = "6150";
            const string WIA_HORIZONTAL_SCAN_SIZE_PIXELS = "6151";
            const string WIA_VERTICAL_SCAN_SIZE_PIXELS = "6152";
            const string WIA_SCAN_BRIGHTNESS_PERCENTS = "6154";
            const string WIA_SCAN_CONTRAST_PERCENTS = "6155";

            switch (MODE)
            {
               
                case 0 :
	                const string WIA_HORIZONTAL_SCAN_RESOLUTION_DPI = "6147";
	                const string WIA_VERTICAL_SCAN_RESOLUTION_DPI = "6148";

                    //const string WIA_HORIZONTAL_SCAN_RESOLUTION_DPI = "2400";
                    //const string WIA_VERTICAL_SCAN_RESOLUTION_DPI = "2400";

                    //const string WIA_HORIZONTAL_SCAN_START_PIXEL = "6149";
                    //const string WIA_VERTICAL_SCAN_START_PIXEL = "6150";
                    //const string WIA_HORIZONTAL_SCAN_SIZE_PIXELS = "6151";
                    //const string WIA_VERTICAL_SCAN_SIZE_PIXELS = "6152";
                    //const string WIA_SCAN_BRIGHTNESS_PERCENTS = "6154";
                    //const string WIA_SCAN_CONTRAST_PERCENTS = "6155";
	               
                    SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
	                SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
	                SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_START_PIXEL, scanStartLeftPixel);
	                SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_START_PIXEL, scanStartTopPixel);
	                SetWIAProperty(scannnerItem.Properties, WIA_SCAN_BRIGHTNESS_PERCENTS, brightnessPercents);
	                SetWIAProperty(scannnerItem.Properties, WIA_SCAN_CONTRAST_PERCENTS, contrastPercents);

                    break;

                
                //case 2:
                //    SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
                //    SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
                //    break;


                case 3 :
                   
                    SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
	                SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
                    SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_START_PIXEL, scanStartLeftPixel);
	                SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_START_PIXEL, scanStartTopPixel);
                  
                    // Use A4 with respect to dpi
                    ////dpi * 118,9 cm / 2,54
                    //// 29,7 cm x 21 cm

                    double v = scanResolutionDPI * 29.7 / 2.54;
                    double h = scanResolutionDPI * 21 / 2.54;

                    SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_SIZE_PIXELS, h);
                    SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_SIZE_PIXELS, v);
                   
                    break;

                case 4 :

                    SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
                    SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
                    SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_START_PIXEL, scanStartLeftPixel);
                    SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_START_PIXEL, scanStartTopPixel);

                    // Use A4 with respect to dpi
                    ////dpi * 118,9 cm / 2,54
                    //// 29,7 cm x 21 cm

                    double vv = scanResolutionDPI * _breite / 2.54;
                    double hh = scanResolutionDPI * _hoehe / 2.54;

                    SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_SIZE_PIXELS, hh);
                    SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_SIZE_PIXELS, vv);

                    break;

            }


	    }

        /// <summary>
        /// Sets the specified WIA setting values
        /// </summary>
        /// <param name="properties">IWiaPropertyStorage interface</param>
        /// <param name="propName">Property name</param>
        /// <param name="propValue">Property value</param>
        private static void SetWIAProperty(IProperties properties, object propName, object propValue)
	    {
	        Property prop = properties.get_Item(ref propName);
	        prop.set_Value(ref propValue);
	    }

        /// <summary>
        /// Converts image to PNG and saves the image to the specifies folder. Return true if file was saved
        /// </summary>
        /// <param name="image">Scanned ImageFile </param>
        /// <param name="fileName">Path where images will be saved (temporary)</param>
        public static bool SaveImageToPNGFile(ImageFile image, string fileName)
	    {
            ImageProcess imgProcess = new ImageProcess();
	        object convertFilter = "Convert";
	        string convertFilterID = imgProcess.FilterInfos.get_Item(ref convertFilter).FilterID;
	        imgProcess.Filters.Add(convertFilterID, 0);
	        SetWIAProperty(imgProcess.Filters[imgProcess.Filters.Count].Properties, "FormatID", WIA.FormatID.wiaFormatPNG);

            if (File.Exists(fileName))
            {
                try
                {
                    File.Delete(fileName);
                    image.SaveFile(fileName);
                    
                    return true;
                }
                catch (Exception e)
                {
                    //MessageBox.Show("Can't save scanned image.\nDetails: "+e.Message+"", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    throw new Exception();
                    return false;
                }
            } else {
                image = imgProcess.Apply(image);
	            image.SaveFile(fileName);
                
                return true;
            }

	    }   
 
    }
}
