//
// Copyright (c) 2015, MindFusion LLC - Bulgaria.
//

using Microsoft.Win32;

using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;

using MindFusion.Reporting.Wpf.Export;


namespace MindFusion.Reporting.Wpf.Samples.CS.Exporting
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();

			myReport = Report.FromDictionary(new Uri("Report.xaml", UriKind.Relative), "myReport");

			myReport.Run();

			layout = myReport.Layout(new Size(8.5 * 96, 11 * 96),
				new Thickness(0.5 * 96), PageOrientation.Portrait);

			var renderTarget = new FixedDocumentRenderTarget();
			layout.RenderAllPages(renderTarget);

			viewer.Document = renderTarget.Document;
		}

		private void menuItemPng_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "PNG Files (*.png)|*.png|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				var renderTarget = new ImageRenderTarget()
				{
					IsMultipage = false,
					PageBackground = Brushes.White,
					PageBorderBrush = Brushes.Black,
				};
				layout.RenderAllPages(renderTarget);
				renderTarget.Save(dialog.FileName, new PngBitmapEncoder());
			}
		}

		private void menuItemTiff_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "TIFF Files (*.tif, *.tiff)|*.tif, *.tiff|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				var renderTarget = new ImageRenderTarget()
				{
					IsMultipage = true,
					PageBackground = Brushes.White,
					PageBorderBrush = Brushes.Black,
				};
				layout.RenderAllPages(renderTarget);
				renderTarget.Save(dialog.FileName, new TiffBitmapEncoder());
			}
		}

		private void menuItemGif_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "GIF Files (*.gif)|*.gif|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				var renderTarget = new ImageRenderTarget()
				{
					IsMultipage = false,
					PageBackground = Brushes.White,
					PageBorderBrush = Brushes.Black,
				};
				layout.RenderAllPages(renderTarget);
				renderTarget.Save(dialog.FileName, new GifBitmapEncoder());
			}
		}

		private void menuItemJpg_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "JPEG Files (*.jpg, *.jpeg)|*.jpg, *.jpeg|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				var renderTarget = new ImageRenderTarget()
				{
					IsMultipage = false,
					PageBackground = Brushes.White,
					PageBorderBrush = Brushes.Black,
				};
				layout.RenderAllPages(renderTarget);
				renderTarget.Save(dialog.FileName, new JpegBitmapEncoder());
			}
		}

		private void menuItemBmp_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "Bitmap Files (*.bmp)|*.bmp|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				var renderTarget = new ImageRenderTarget()
				{
					IsMultipage = false,
					PageBackground = Brushes.White,
					PageBorderBrush = Brushes.Black,
				};
				layout.RenderAllPages(renderTarget);
				renderTarget.Save(dialog.FileName, new BmpBitmapEncoder());
			}
		}

		private void menuItemWmp_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "WMP Files (*.wmp)|*.wmp|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				var renderTarget = new ImageRenderTarget()
				{
					IsMultipage = false,
					PageBackground = Brushes.White,
					PageBorderBrush = Brushes.Black,
				};
				layout.RenderAllPages(renderTarget);
				renderTarget.Save(dialog.FileName, new WmpBitmapEncoder());
			}
		}

		private void menuItemPdf_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "PDF Files (*.pdf)|*.pdf|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				PdfExporter exporter = new PdfExporter();
				exporter.Export(myReport, dialog.FileName);
			}
		}

		private void menuItemHtml_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "HTML Files (*.htm, *.html)|*.htm, *.html|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				var renderTarget = new HtmlRenderTarget(
					Path.GetDirectoryName(dialog.FileName), "")
					{
						RenderPages = true,
						RenderPageShadows = true,
					};
				layout.RenderAllPages(renderTarget);
				renderTarget.Save(dialog.FileName);
			}
		}

		private void menuItemMhtml_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "MHTML Files (*.mht)|*.mht|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				var renderTarget = new MhtmlRenderTarget()
				{
					RenderPages = true,
					RenderPageShadows = true,
				};
				layout.RenderAllPages(renderTarget);
				renderTarget.Save(dialog.FileName);
			}
		}

		private void menuItemXps_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "XPS Files (*.xps)|*.xps|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				var renderTarget = new FixedDocumentRenderTarget();
				layout.RenderAllPages(renderTarget);

				XpsDocument xpsDocument = new XpsDocument(dialog.FileName, FileAccess.ReadWrite);
				XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
				writer.Write(renderTarget.Document);
				xpsDocument.Close();
			}
		}

		private void menuItemExcel_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "Open Office XML Files (*.xlsx)|*.xlsx|All files (*.*)|*.*||";
			if (dialog.ShowDialog(this) == true)
			{
				ExcelExporter ex = new ExcelExporter();
				using (var stream = new FileStream(dialog.FileName, FileMode.Create, FileAccess.Write))
					ex.Export(myReport, stream);
			}
		}


		private Report myReport;
		private ReportLayout layout;
	}
}
