using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using GenArt.Classes;
using GenArt.AST;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
 
namespace WpfApplication
{
    /// <summary>
    /// DNA Viewer - open and save-as a .DNA file generated by Roger Alsing's 
    /// http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/
    /// </summary>
    /// <remarks>
    /// FAQ
    /// http://rogeralsing.com/2008/12/09/genetic-programming-mona-lisa-faq/
    /// 
    /// Gallery
    /// http://rogeralsing.com/2008/12/11/genetic-gallery/
    /// </remarks>
    public partial class Window1 : Window
    {
        private DnaDrawing currentDrawing;
        private System.Windows.Point currentDrawingBottomRight = new System.Windows.Point(0,0);
        private int scale = 8;  // NOTE: this is hardcoded to make big 'saved images' for now...
        private System.Drawing.Rectangle bounds = new System.Drawing.Rectangle();
 
        public Window1()
        {
            InitializeComponent();
        }
 
        public void Open()
        {
            currentDrawing = Serializer.DeserializeDnaDrawing(FileUtil.GetOpenFileName(FileUtil.DnaExtension));
            if (currentDrawing != null)
            {
                // find MaxWidth and MaxHeight - extent of the canvas to draw
                foreach (var poly in currentDrawing.Polygons)
                {
                    foreach (var pnt in poly.Points)
                    {
                        if (pnt.X > currentDrawingBottomRight.X) currentDrawingBottomRight.X = pnt.X;
                        if (pnt.Y > currentDrawingBottomRight.Y) currentDrawingBottomRight.Y = pnt.Y;
                    }
                }
 
                bounds = new System.Drawing.Rectangle(0, 0, Convert.ToInt32(currentDrawingBottomRight.X), Convert.ToInt32(currentDrawingBottomRight.Y));
 
                using (
                var backBuffer = new System.Drawing.Bitmap(
                                        Convert.ToInt32(scale * bounds.Width),
                                        Convert.ToInt32(scale * bounds.Height),
                                        System.Drawing.Imaging.PixelFormat.Format24bppRgb))
                using (Graphics backGraphics = Graphics.FromImage(backBuffer))
                {
                    backGraphics.SmoothingMode = SmoothingMode.HighQuality;
                    Renderer.Render(currentDrawing, backGraphics, scale);   // Image
                    EvoImage.Source = loadBitmap(backBuffer);               // Image
 
                    WpfRenderer.Render(currentDrawing, EvoCanvas, scale);   // Canvas
                }
            }
        }
        /// <summary>
        /// Save a JPF of the image
        /// </summary>
        public void Save()
        {
            if (currentDrawing != null)
            {
                string imgExtension = "jpg files (*.jpg)|*.jpg|jpeg files (*.jpeg)|*.jpeg|All files (*.*)|*.*";
                var fn = FileUtil.GetSaveFileName(imgExtension);
 
                if (fn != null)
                {
                    //var scale = 8.0;
                    //var bounds = new System.Drawing.Rectangle(0, 0, 400, 300);
                    using (
                    var backBuffer = new System.Drawing.Bitmap(
                                            Convert.ToInt32(scale * bounds.Width),
                                            Convert.ToInt32(scale * bounds.Height),
                                            System.Drawing.Imaging.PixelFormat.Format24bppRgb))
                    using (Graphics backGraphics = Graphics.FromImage(backBuffer))
                    {
                        backGraphics.SmoothingMode = SmoothingMode.HighQuality;
                        Renderer.Render(currentDrawing, backGraphics, Convert.ToInt32(scale));
                        EvoImage.Source = loadBitmap(backBuffer);
                        backBuffer.Save(fn, System.Drawing.Imaging.ImageFormat.Jpeg);
 
                        StringBuilder sb = new StringBuilder();
                        XamlRenderer.Render(currentDrawing, ref sb, scale);     // XAML for silverlight
                        System.IO.File.WriteAllText(fn + ".xaml", sb.ToString());
 
 
                        sb = new StringBuilder();
                        SvgRenderer.Render(currentDrawing, ref sb, scale, bounds);     // SVG for Firefox - NEW!
                        System.IO.File.WriteAllText(fn + ".svg", sb.ToString());
                    }
                }
            }
        }
        public static BitmapSource loadBitmap(System.Drawing.Bitmap source)
        {
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        }
 
        #region Commands
        /// <summary>
        /// CloseCommand
        /// </summary>
        void CloseCommandExecuted(object target, ExecutedRoutedEventArgs e)
        {
            this.Close();
        }
        void CloseCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
 
        /// <summary>
        /// OpenCommand
        /// </summary>
        void OpenCommandExecuted(object target, ExecutedRoutedEventArgs e)
        {
            Open();
        }
        void OpenCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
 
        /// <summary>
        /// SaveCommand
        /// </summary>
        void SaveCommandExecuted(object target, ExecutedRoutedEventArgs e)
        {
            Save();
        }
        void SaveCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = (currentDrawing != null);
        }
        #endregion
    }
}