using System;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using agree;
using agree.Wpf.Util;
using glue.WpfUtil;

namespace WpfClient
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	///
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	class MainScroll : ScrollViewer
	{
		public MainScroll(Grammar g, IEnumerable<TfsEdge> ce, Orientation grid_orientation)
		{
			if (grid_orientation == Orientation.Horizontal)
				this.Content = new HorizontalTfsGrid(g, ce);
			else
				this.Content = new TwoColumnDefinitionExpandedGrid(g, ce);

			this.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
			this.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
			this.CanContentScroll = true;
		}
	};


	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	///
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	abstract class MainGrid : Grid
	{
		public MainGrid()
		{
			this.HorizontalAlignment = HorizontalAlignment.Left;
			this.VerticalAlignment = VerticalAlignment.Top;
			this.ShowGridLines = true;
		}
	};


	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	///
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	class HorizontalTfsGrid : MainGrid
	{
		Random rnd = new Random();

		public HorizontalTfsGrid(Grammar g, IEnumerable<TfsEdge> ce)
		{
			int j = 0;
			foreach (TfsEdge te in ce)
			{
				TfsControl tc = new TfsControl();
				tc._TfsEdge = te;
				tc.FontFamily = new FontFamily("Arial");
				tc.Margin = new Thickness(10);
				this.ColumnDefinitions.Add(new ColumnDefinition());
				tc.SetValue(Grid.ColumnProperty, j);
				this.Children.Add(tc);

				j++;

				//tc.Background = new SolidColorBrush(Color.FromArgb((byte)rnd.Next(), (byte)rnd.Next(), (byte)rnd.Next(), (byte)rnd.Next()));
			}

			this.ShowGridLines = true;
		}
	};


	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	///
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	class SingleTfsGrid : Grid
	{
		Random rnd = new Random();

		public SingleTfsGrid(Grammar g, TfsEdge ce)
		{
			TfsControl tc = new TfsControl();
			tc._TfsEdge = ce;
			tc.FontFamily = new FontFamily("Arial");
			tc.Margin = new Thickness(10);
			this.Children.Add(tc);
		}
	};


	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	///
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	class TwoColumnDefinitionExpandedGrid : MainGrid
	{
		public TwoColumnDefinitionExpandedGrid(Grammar g, IEnumerable<TfsEdge> ce)
		{
			this.Margin = new Thickness(20);

			this.ColumnDefinitions.Add(new ColumnDefinition());
			this.ColumnDefinitions.Add(new ColumnDefinition());
			this.ColumnDefinitions.Add(new ColumnDefinition());
			this.ColumnDefinitions.Add(new ColumnDefinition());
			this.RowDefinitions.Add(new RowDefinition());

			int j = 1;
			foreach (TfsEdge e in ce)
			{
				this.RowDefinitions.Add(new RowDefinition());

				TfsControl tc0 = new TfsControl();
				tc0._TfsEdge = e;
				tc0.FontFamily = new FontFamily("Arial");
				tc0.Margin = new Thickness(10);
				tc0.SetValue(Grid.RowProperty, j);
				tc0.SetValue(Grid.ColumnProperty, 1);

				TfsControl tc1 = new TfsControl();
				tc1._TfsEdge = e.Type.Expanded;
				tc1.FontFamily = new FontFamily("Arial");
				tc1.Margin = new Thickness(10);
				tc1.SetValue(Grid.ColumnProperty, 2);
				tc1.SetValue(Grid.RowProperty, j);

				this.Children.Add(tc0);
				this.Children.Add(tc1);

				j++;
			}

			this.RowDefinitions.Add(new RowDefinition());
			this.ShowGridLines = true;
		}
	};
}