using System.Windows;
using System.Windows.Controls;
using Microsoft.Windows.Shell;
using System.Windows.Input;
using APE.model;

namespace APE.presentation {
	public partial class MainWindow : Window {
		FSClipboard clippy;

		/// <summary>
		/// In case this gets compiled for WinXP, default blue header color is 
		/// approximately "#0254e8". Neutral grey is though more recommended approach
		/// in case the header area is not draggable.
		/// </summary>
		public MainWindow() {
			InitializeComponent();
			var style = (Style)Resources["MinimalistStyle"];
			this.Style = style;
		}

		#region Event Handlers

		private void tabs_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) {
			var tabPanel = sender as TabPanel;
			if ((e.Key == Key.W && Keyboard.IsKeyDown(Key.LeftCtrl)) || e.Key == Key.F1) {
				var closedTab = tabPanel.SelectedItem;
				tabPanel.Items.Remove(closedTab);
				if (tabPanel.Items.Count == 0) {
					Application.Current.Shutdown();
				}
			} else if ((e.Key == Key.T && Keyboard.IsKeyDown(Key.LeftCtrl)) || e.Key == Key.F2) {
				var newTab = new presentation.Tab(Properties.Settings.Default.startupDirectory);
				newTab.IsSelected = true;
				tabPanel.Items.Add(newTab);
			} else if (e.Key == Key.F5) {
				refresh();
			}
		}

		private void OnClearClipboard(object sender, RoutedEventArgs e) {
			clippy.clear();
		}

		/// <summary>
		/// This method catches attached event defined on another class in the project. That is why we wait and attach the handler after Loaded method is fired.
		/// </summary>
		private void OnCopyMove(object sender, RoutedEventArgs e) {
			clippy = (e as List.CopyMoveEventArgs).clippy;
		}

		/// <summary>
		/// This event listener has to be set via AddHandler, because:
		/// 1. It is second time the event is caught and thus would not be handled without explicit command. The first handler should inject destinationNode into EventArgs. This second one carries out copying/moving itself.
		/// 2. It catches attached event defined on another class in the project. That is why we wait and attach the handler after Loaded method is fired.
		/// 
		/// </summary>
		private void OnPaste(object sender, RoutedEventArgs e) {
			AbstractNode destinationNode = (e as List.PasteEventArgs).DestinationNode;
			foreach (AbstractNode node in clippy.nodes) {
				if (clippy.operation == DragDropEffects.Copy) {
					node.accept(new FSCopyVisitor(destinationNode));
				} else if (clippy.operation == DragDropEffects.Move) {
					node.accept(new FSMoveVisitor(destinationNode));
				}
			}
			refresh();
		}

		private void OnRefresh(object sender, RoutedEventArgs e) {
			refresh();
		}


		private void APE_Loaded(object sender, RoutedEventArgs e) {
			AddHandler(List.CopyMoveEvent, new RoutedEventHandler(OnCopyMove));
			AddHandler(List.PasteEvent, new RoutedEventHandler(OnPaste), true);
			AddHandler(List.RefreshEvent, new RoutedEventHandler(OnRefresh));
			AddHandler(List.ClearClipboardEvent, new RoutedEventHandler(OnClearClipboard));
		}

		#endregion

		private void refresh() {
			((TabPanel)Template.FindName("tabs", this)).refresh();
		}
	}
}
