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

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;


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

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

			brushes = myReport.Resources["PieBrushes"] as Brush[];

			dataset = new nwindDataSet();
			Tutorial5.nwindDataSetTableAdapters.ProductsTableAdapter adapter =
				new Tutorial5.nwindDataSetTableAdapters.ProductsTableAdapter();
			adapter.Fill(dataset.Products);
			Tutorial5.nwindDataSetTableAdapters.CheapProductsTableAdapter cheapProductsAdapter =
				new Tutorial5.nwindDataSetTableAdapters.CheapProductsTableAdapter();
			cheapProductsAdapter.Fill(dataset.CheapProducts);

			myReport.DataContext = dataset;
			myReport.Prerender += new EventHandler<PrerenderEventArgs>(myReport_Prerender);

			myReport.Run();

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

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

			documentViewer1.Document = renderTarget.Document;
		}

		private void myReport_Prerender(object sender, PrerenderEventArgs e)
		{
			if (e.Item is ItemContainer)
			{
				var row = e.Item.DataContext as nwindDataSet.ProductsRow;
				if (row != null)
				{
					if (row.UnitPrice < 10)
					{
						int i = 0;
						int index = -1;
						foreach (nwindDataSet.CheapProductsRow cheapRow in dataset.CheapProducts)
						{
							if (cheapRow.ProductName == row.ProductName)
							{
								index = i;
								break;
							}

							i++;
						}
						var border = e.RootVisual as Border;
						border.BorderBrush = new SolidColorBrush(Colors.Red);
						border.BorderThickness = new Thickness(1);
						border.Background = brushes[index];
					}
				}
			}
		}


		private nwindDataSet dataset;
		private Brush[] brushes;
	}
}
