using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using SPC.Kiosk.Base; using SPC.Kiosk.Common; using SPC.Kiosk.Popup.Model; namespace SPC.Kiosk.Popup { /// /// MiniBasketItem.xaml에 대한 상호 작용 논리 /// public partial class HappyAppCoupons : UserControl { #region [ Members ] /// /// ItemData /// public static readonly DependencyProperty CouponDataProperty = DependencyProperty.Register(nameof(CouponData), typeof(List), typeof(HappyAppCoupons), new PropertyMetadata(null, new PropertyChangedCallback(OnCouponDataPropertyChanged))); /// /// Display Language /// public static readonly DependencyProperty DisplayLanguagePropery = DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(HappyAppCoupons), new PropertyMetadata(SupportLanguageType.ko, new PropertyChangedCallback(OnDisplayLanguageProperyChanged))); /// /// Vertical Scroll Offset /// public static readonly DependencyProperty VerticalOffsetProperty = DependencyProperty.Register(nameof(VerticalOffset), typeof(double), typeof(HappyAppCoupons), new PropertyMetadata(0d, new PropertyChangedCallback(OnVerticalOffsetPropertyChanged))); /// /// Command /// public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(HappyAppCoupons), new UIPropertyMetadata(null)); /// /// Command /// public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } /// /// Vertical Scroll Offset /// public double VerticalOffset { get { return (double)GetValue(VerticalOffsetProperty); } set { SetValue(VerticalOffsetProperty, value); } } /// /// ItemData /// public List CouponData { get { return (List)GetValue(CouponDataProperty); } set { SetValue(CouponDataProperty, value); } } /// /// Display Language /// public SupportLanguageType DisplayLanguage { get { return (SupportLanguageType)GetValue(DisplayLanguagePropery); } set { SetValue(DisplayLanguagePropery, value); } } #region [ RoutedEvent 'CouponSelect' ] /// /// MouseClicked Event /// public static readonly RoutedEvent CouponSelectEvent = EventManager.RegisterRoutedEvent(nameof(CouponSelect), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(HappyAppCoupons)); /// /// MouseClicked Routed Event Handler /// public event RoutedEventHandler CouponSelect { add { AddHandler(CouponSelectEvent, value); } remove { RemoveHandler(CouponSelectEvent, value); } } #endregion RoutedEvent 'SelectChange' private double mouseDownYposition = -1; private bool MoseDown = false; #endregion Members #region [ Ctor & Etc ] /// /// Ctor /// public HappyAppCoupons() { InitializeComponent(); CouponGrid.PreviewMouseDown += CouponGrid_PreviewMouseDown; CouponGrid.PreviewMouseUp += CouponGrid_PreviewMouseUp; CouponGrid.PreviewMouseMove += CouponGrid_PreviewMouseMove; CouponGrid.MouseLeave += CouponGrid_MouseLeave; } #endregion Ctor & Etc #region [ Methods ] /// /// Raise MouseClicked Event /// private void RaiseCouponSelectEvent(object _sender) { if (Command != null && Command.CanExecute(_sender)) { Command.Execute(_sender); } RoutedEventArgs newEventArgs = new RoutedEventArgs(HappyAppCoupons.CouponSelectEvent, _sender); RaiseEvent(newEventArgs); } private void SetCouponStack(List newValue) { foreach (var aGrid in CouponStack.Children) { if (aGrid is Grid aCoupons) { foreach (var aStack in aCoupons.Children) { if (aStack is StackPanel aItems) { foreach (var aItem in aItems.Children) { if (aItem is HappyAppCoupon aCoupon) { aCoupon.MouseClicked -= ACoupon_MouseClicked; } } aItems.Children.Clear(); } } aCoupons.Children.Clear(); } } CouponStack.Children.Clear(); if (newValue.Count.Equals(0)) return; var index = 0; Grid lineGrid = null; StackPanel lineStack = null; foreach (var aCouponDetail in newValue) { var newCoupon = new HappyAppCoupon { ItemData = aCouponDetail, DisplayLanguage = this.DisplayLanguage }; if ((index % 3).Equals(0)) { lineGrid = new Grid { VerticalAlignment = VerticalAlignment.Top, HorizontalAlignment = HorizontalAlignment.Left, Width = newCoupon.Width * 3, Height = newCoupon.Height, }; lineStack = new StackPanel { VerticalAlignment = VerticalAlignment.Top, HorizontalAlignment = HorizontalAlignment.Left, Orientation = Orientation.Horizontal, }; } newCoupon.MouseClicked += ACoupon_MouseClicked; newCoupon.Name = string.Format("HappyAppCoupon{0}", index); this.ReregisterName(newCoupon.Name, newCoupon); lineStack.Children.Add(newCoupon); index++; if ((index % 3).Equals(0)) { lineGrid.Children.Add(lineStack); CouponStack.Children.Add(lineGrid); } } if (!(index % 3).Equals(0)) { lineGrid.Children.Add(lineStack); CouponStack.Children.Add(lineGrid); } } #endregion Methods #region [ Event Handlers ] private void ACoupon_MouseClicked(object sender, RoutedEventArgs e) { if (sender is HappyAppCoupon selectCoupon) { foreach (var aGrid in CouponStack.Children) { if (aGrid is Grid aCoupons) { foreach (var aStack in aCoupons.Children) { if (aStack is StackPanel aItems) { foreach (var aItem in aItems.Children) { if (aItem is HappyAppCoupon aCoupon) { if (aCoupon.Name.Equals(selectCoupon.Name)) { aCoupon.ItemData.IsUsed = !aCoupon.ItemData.IsUsed; selectCoupon.Switch = aCoupon.ItemData.IsUsed; } else { aCoupon.Switch = false; aCoupon.ItemData.IsUsed = false; } } } } } } } } RaiseCouponSelectEvent(e.OriginalSource); } private static void OnVerticalOffsetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue != e.OldValue) { var target = d as HappyAppCoupons; target.MainScroll.ScrollToVerticalOffset((double)e.NewValue); } } private static void OnCouponDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue is List newValue) { var target = d as HappyAppCoupons; target.SetCouponStack(newValue); } } private static void OnDisplayLanguageProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue is SupportLanguageType newValue) { var target = d as HappyAppCoupons; foreach (var aGrid in target.CouponStack.Children) { if (aGrid is Grid aCoupons) { foreach (var aStack in aCoupons.Children) { if (aStack is StackPanel aItems) { foreach (var aItem in aItems.Children) { if (aItem is HappyAppCoupon aCoupon) { aCoupon.DisplayLanguage = newValue; } } } } } } } } private void CouponGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e) { var requstGrid = sender as Grid; mouseDownYposition = e.GetPosition(sender as Grid).Y; MoseDown = true; } private void CouponGrid_PreviewMouseUp(object sender, MouseButtonEventArgs e) { MoseDown = false; } private void CouponGrid_PreviewMouseMove(object sender, MouseEventArgs e) { if (!MoseDown) return; var requstGrid = sender as Grid; var curMouseYposition = e.GetPosition(requstGrid).Y; VerticalOffset = VerticalOffset + (mouseDownYposition - curMouseYposition) * 2; mouseDownYposition = curMouseYposition; } private void CouponGrid_MouseLeave(object sender, MouseEventArgs e) { MoseDown = false; } #endregion Event Handlers } }