using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace CarGallery
{
    public static class ImageProcessor
    {
        // in ye class e static hastesh vase inke ax ha ro resize konim
        // hamchenin betoonim Hash e file ro begirim
        // code hash amadas, khodam naneveshtam aksaresho vali ye tozihi midam dar morede chizayi ke khodam neveshtam


        // in method ax ro be soorate stream migire ke ye no e dadeyi hast 
        // vase inke betooni ax ro behesh bedi bayad az method e OpenRead az Class e File estefade koni ke to page e CreatePost hast
        //

        public static void Resize(Stream fromStream,int newWidth,int newHeight, string saveFilePathAndName)
        {
            var image = Image.FromStream(fromStream);

            // vaqti mikhaym thumbnail dorost konim nemishe hamintori khiari begim 200x200 kon
            // chon age masalan tasvir 1000x500 bashe va resize konish be 200x200 , tasvir keshide mishe va be ham mirize formesh
            // pas miaym ye harekati mizanim ke aspect ratio (nesbat e tool be arz) e tasvir be ham narize
           

            if (newWidth == 200) // aval bebinim asan mikhad thumbnail dorost kone ?! ya faqat mikhad resize kone
                // chon ma resize ro vase axe slideshow ham be kar bordim
            {
                if (image.Width > image.Height) // age andaze ofoqi az andaze amoodi bishtar bood
                    // andaze ofoqi ro 200 mikonim vali andaze amoodi bayad mohasebe beshe
                {
                    newWidth = 200;

                    // az in formool estefade mikonim :

                    newHeight = image.Height / (image.Width / 200); 

                    // ino ba ye mesal migam behtar motevajeh mishi.

                    // farz mikonim :

                    // ImageWidth = 1000;
                    // ImageHeight = 500;

                    // formoolemoon ine : newHeight = ImageHeight ÷ ( ImageWidth ÷ 200 )
                    // newheight = 1000 ÷ ( 1000 ÷ 200 )
                    // newHeight = 500 ÷ ( 5 )

                    // newHeight = 100;
                    // newWidth = 200;

                    // age bekhaym ye axe 1000x500 ro koochik konim ke bozorgtarin bodesh 200 bashe 
                    // va nesbat e tasviresh ham beham narize , bayad resizesh konim be 200x100

                    // hamoontor ke mibini 1000 ÷ 200 = 5 va 500 ÷ 100 = 5
                    
                    // darvaqe 5 bozorgtarin adadike ke age bodhaye tasvir ro behesh taqsim konim
                    // bozorgtarinesh mishe 200
                    
                }
                else if (image.Width < image.Height) // hamchenin age andaze ofoqi koochik tar az andaze amoodi bood bar axe qabli mishe
                {
                    newWidth = image.Width / (image.Height / 200);
                    newHeight = 200;
                }
                else // age andaze ofoqi va amoodie ax barabar bood ke che behtar ! joftesh mishe 200x200
                {
                    newHeight = 200;
                    newWidth = 200;
                }

            }

            // az inja be badesh dozdie :D

            var thumbnailBitmap = new Bitmap(newWidth, newHeight);

            var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
            thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

            var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
            thumbnailGraph.DrawImage(image, imageRectangle);

            thumbnailBitmap.Save(saveFilePathAndName, image.RawFormat);

            thumbnailGraph.Dispose();
            thumbnailBitmap.Dispose();
            image.Dispose();
        }


        // in method ye Hash az file mide (dar morede tozihe inke Hash chie behtare search koni age lazeme)
        // faqat dar in had begam ke har file ye hash e monhaser be fard dare
        // va hash ye code hastesh , ye reshte
        // az in reshte ma be onvane esme file estefade mikonim
        // baes mishe hichvaqt esme file tekrari nashe
        // va in ham dozdie :D
        public static string getFileHash(string file)
        {
            using (FileStream stream = File.OpenRead(file))
            {
                SHA256Managed sha = new SHA256Managed();
                byte[] hash = sha.ComputeHash(stream);
                return BitConverter.ToString(hash).Replace("-", String.Empty);
            }
        }
    }
}