using com.superscene.ui;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace parkMonitor.view {
	/// <summary>强制滚动</summary>
	public class ScrollBehavior : BaseBehavior<ItemsControl> {
		public override void onAttached() {
			base.onAttached();
			if(element == null) {
				return;
			}
			//Debug.WriteLine("add:" + element.Foreground);
			element.PreviewMouseWheel += Element_PreviewMouseWheel;
		}

		private void Element_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) {
			DependencyObject border = VisualTreeHelper.GetChild(element, 0);
			ScrollViewer scrollViwer = null;
			if(border != null) {
				scrollViwer = VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
			}

			if(scrollViwer == null) {
				return;
			}
			int scrollGap = 50;
			if(e.Delta < 0) {
				//scrollViwer.LineDown();
				scrollViwer.ScrollToVerticalOffset(scrollViwer.VerticalOffset + scrollGap);
			} else if(e.Delta > 0) {
				//scrollViwer.LineUp();
				scrollViwer.ScrollToVerticalOffset(scrollViwer.VerticalOffset - scrollGap);
			}
		}

		public override void onDetaching() {
			base.onDetaching();
			if(element == null) {
				return;
			}
			//Debug.WriteLine("rm:" + element.Foreground);
			element.PreviewMouseWheel -= Element_PreviewMouseWheel;
		}
	}
}
