using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Resources;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Media;

namespace WPPhotoExtras
{
    public partial class MainPage : PhoneApplicationPage
    {
        BitmapImage bitmap;
        WriteableBitmap wbitmap;
        string originalFilename;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            IDictionary<string, string> queryStrings = this.NavigationContext.QueryString;

            bitmap = new BitmapImage();

            //Ausgeführt, wenn aus den Photo Extras aufgerufen
            if (queryStrings.ContainsKey("token"))
            {

                MediaLibrary library = new MediaLibrary();
                Picture picture = library.GetPictureFromToken(queryStrings["token"]);
                originalFilename= picture.Name;
                bitmap.SetSource(picture.GetImage());
                wbitmap = new WriteableBitmap(bitmap);
            }
            //Aufruf von Start
            else
            {
                Uri uri = new Uri("/WPPhotoExtras;component/SampleImage.png", UriKind.Relative);
                StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
                bitmap.SetSource(resourceInfo.Stream);
                wbitmap = new WriteableBitmap(bitmap);
                originalFilename = "SampleImage.png";
            }
            imgMain.Source = wbitmap;

        }

        private void btnConvert_Click(object sender, RoutedEventArgs e)
        {
            for (int y = 0; y < wbitmap.PixelHeight; y++)
            {
                for (int x = 0; x < wbitmap.PixelWidth; x++)
                {
                    int pixelLocation = wbitmap.PixelWidth * y + x;
                    int pixel = wbitmap.Pixels[pixelLocation];
                    byte[] pixelBytes = BitConverter.GetBytes(pixel);
                    byte bwPixel = (byte)(.299 * pixelBytes[2] + .587 * pixelBytes[1] + .114 * pixelBytes[0]);
                    pixelBytes[0] = bwPixel;
                    pixelBytes[1] = bwPixel;
                    pixelBytes[2] = bwPixel;
                    wbitmap.Pixels[pixelLocation] = BitConverter.ToInt32(pixelBytes, 0);
                }
            }
            imgMain.Source = wbitmap;
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            MediaLibrary library = new MediaLibrary();
            string newFilename =
                System.IO.Path.GetFileNameWithoutExtension(originalFilename) + "-Greyscale.jpg";
            MemoryStream ms = new MemoryStream ();
            wbitmap.SaveJpeg(ms,wbitmap.PixelWidth,wbitmap.PixelWidth,0,98);
            ms.Flush();
            ms.Seek(0,SeekOrigin.Begin);
            library.SavePicture(newFilename, ms);
            ms.Close();
            ms.Dispose();

        }
    }
}