using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows;
using Telerik.Windows.Controls;
using System.Windows.Media.Imaging;

namespace SLApp
{
	public partial class MainPage : UserControl
	{
		public MainPage()
		{
			InitializeComponent();
		}

		private void radUpload_FileUploaded(object sender, FileUploadedEventArgs e)
		{
			RadUploadSelectedFile uploadedFile = e.SelectedFile;
			// CustomData is a Dictionary property that stores the new file name in value of a key.
			// This key is set in the RadUploadHandler.ashx.cs
			string newFileName = e.HandlerData.CustomData["NewFileName"].ToString();

			if (this.radUpload.CurrentSession.FileNameUploadItem.ContainsKey(uploadedFile.Name))
			{
				RadUploadItem item = this.radUpload.CurrentSession.FileNameUploadItem[uploadedFile.Name];
				if (item != null)
				{
					// Retrieve the TextBlock that will hold new file name
					FrameworkElement element = GetCustomTagControl(item, "NewFileName");
					if (element != null)
					{
						TextBlock textBlock = element as TextBlock;
						if (textBlock != null)
						{
							textBlock.Text = newFileName;
							textBlock.Visibility = Visibility.Visible;
						}
					}
				}
			}
		}

		private static FrameworkElement GetCustomTagControl(DependencyObject control, string name)
		{
			for (int i = 0; i < VisualTreeHelper.GetChildrenCount(control); i++)
			{
				DependencyObject child = VisualTreeHelper.GetChild(control, i);
				FrameworkElement element = child as FrameworkElement;

				if (element != null)
				{
					if (0 == string.Compare(element.Name, name, StringComparison.InvariantCultureIgnoreCase))
					{
						return element;
					}
				}
				element = GetCustomTagControl(child, name);
				if (element != null)
				{
					return element;
				}
			}
			return null;
		}
	}
}
