using Amazon;
using Amazon.Rekognition;
using Amazon.Rekognition.Model;
using Amazon.Runtime;
using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;

namespace RekoAdule
{
	public partial class MainWindow : Window
    {
		//*******************************************************
		//Définir votre région et vos paramètres d'identification
		//*******************************************************
		private static readonly RegionEndpoint regAWS = RegionEndpoint.EUWest1;
		private static readonly string AWSAccessKey = "";
		private static readonly string AWSSecretKey = "";

		private static readonly BasicAWSCredentials AWSCred = new BasicAWSCredentials(AWSAccessKey, AWSSecretKey);

		private BitmapSource imageSource;
        private Stream stream = null;
        private string path = "";
        String strResultat = "";
        int orientationjpg = 0;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnImage_Click(object sender, RoutedEventArgs e)
        {
            Adulte();
            txtResultat.Text = strResultat;
        }

        public BitmapSource imageAffiche(Stream stream)
        {
            orientationjpg = 0;
            BitmapFrame bitmapFrame = BitmapFrame.Create(stream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
            BitmapMetadata bitmapMetadata = bitmapFrame.Metadata as BitmapMetadata;

            if ((bitmapMetadata != null) && (bitmapMetadata.ContainsQuery("System.Photo.Orientation")))
            {
                object o = bitmapMetadata.GetQuery("System.Photo.Orientation");

                if (o != null)
                {
                    switch ((ushort)o)
                    {
                        case 6:
                            orientationjpg = 90;
                            break;
                        case 3:
                            orientationjpg = 180;
                            break;
                        case 8:
                            orientationjpg = 270;
                            break;
                    }
                }
            }

            imageSource = new BitmapImage(new Uri(path));
            if (orientationjpg != 0)
            {
                TransformedBitmap tb = new TransformedBitmap();
                tb.BeginInit();
                tb.Source = imageSource;
                RotateTransform transform = new RotateTransform(orientationjpg);
                tb.Transform = transform;
                tb.EndInit();
                return tb;
            }
            else
            {
                return imageSource;
            }
        }
        public Amazon.Rekognition.Model.Image GetImageFromFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
			//Indiquer le répertoire contenant les photos à analyser
            openFileDialog.InitialDirectory = "C:\\";
            openFileDialog.ShowDialog();
            path = openFileDialog.FileName;

            //String photo = "c:\\Temp\\IMGP0581s.jpg";
            Amazon.Rekognition.Model.Image image = new Amazon.Rekognition.Model.Image();
            try
            {
                stream = openFileDialog.OpenFile();

                byte[] data = new byte[stream.Length];
                stream.Read(data, 0, (int)stream.Length);
                image.Bytes = new MemoryStream(data);
                stream.Position = 0;

                return image;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return null;
            }

        }

        public void Adulte()
        {
            strResultat = "";
            AmazonRekognitionClient rekCli = new AmazonRekognitionClient(AWSCred, AWSreg);

            DetectModerationLabelsRequest detectModerationRequest = new DetectModerationLabelsRequest()
            {
                Image = GetImageFromFile(),
                MinConfidence = 90
            };

            BitmapSource bitmap = imageAffiche(stream);
            try
            {
                int i = 0;
                DetectModerationLabelsResponse detectModerationResp = rekCli.DetectModerationLabels(detectModerationRequest);
                if (detectModerationResp.ModerationLabels.Count != 0)
                {
                    strResultat = $"Contenu suggestif détecté. ";
                    imgSource.Effect = new BlurEffect
                    {
                        Radius=20,
                        KernelType=KernelType.Gaussian,
                        RenderingBias=RenderingBias.Quality
                    };
                    imgSource.Source = bitmap;
                }
                else
                {
                    strResultat = $"Aucun contenu suggestif détecté\n";
                    imgSource.Effect = null;
                    imgSource.Source = bitmap;
                }

                strResultat += $"Modèle de détection {detectModerationResp.ModerationModelVersion.ToString()}\n\n";
                foreach (Amazon.Rekognition.Model.ModerationLabel lab in detectModerationResp.ModerationLabels)
                {
                    i++;   
                    strResultat += $"{lab.Name}\n";
                }
                
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            

        }

    }
}
