using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
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;
namespace SPC.Kiosk.Common
{
    /// <summary>
    /// IntroView.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class IntroView : UserControl
    {
        #region [ Members ]
        /// <summary>
        /// Intro Items
        /// </summary>
        public static readonly DependencyProperty IntroItemsProperty =
                 DependencyProperty.Register(nameof(IntroItems), typeof(ObservableCollection<M_IntroItems>), typeof(IntroView), new PropertyMetadata(null, new PropertyChangedCallback(OnIntroItemsPropertyChanged)));
        /// <summary>
        /// Click Command
        /// </summary>
        public static readonly DependencyProperty CommandProperty =
                 DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(IntroView), new UIPropertyMetadata(null));
        /// <summary>
        /// Intro ITems
        /// </summary>
        public ObservableCollection<M_IntroItems> IntroItems
        {
            get { return (ObservableCollection<M_IntroItems>)GetValue(IntroItemsProperty); }
            set { SetValue(IntroItemsProperty, value); }
        }
        /// <summary>
        /// Click Commad
        /// </summary>
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }
        #region [ RoutedEvent 'MouseClicked' ]

        /// <summary>
        /// MouseClicked Event
        /// </summary>
        public static readonly RoutedEvent MouseClickedEvent = EventManager.RegisterRoutedEvent(nameof(MouseClicked), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(IntroView));
        /// <summary>
        /// MouseClicked Routed Event Handler
        /// </summary>
        public event RoutedEventHandler MouseClicked
        {
            add { AddHandler(MouseClickedEvent, value); }
            remove { RemoveHandler(MouseClickedEvent, value); }
        }
        #endregion RoutedEvent 'MouseClicked'

        #endregion Members

        #region [  Ctor ]
        /// <summary>
        /// Ctor
        /// </summary>
        public IntroView()
        {
            InitializeComponent();
        }
        #endregion Ctor

        #region [ Methods ]
        private void RaiseMouseClickedEvent(object _sender)
        {
            //Command Excute 
            if (Command != null && Command.CanExecute(_sender))
            {
                Command.Execute(_sender);
            }
            RoutedEventArgs newEventArgs = new RoutedEventArgs(IntroView.MouseClickedEvent, _sender);
            RaiseEvent(newEventArgs);
        }
        private void AddStack(StackPanel _targetStack, M_IntroItems _stackContents)
        {
            try
            {
                if (_stackContents.StackContents is List<M_StackContents> stackContents)
                {
                    foreach (var aContents in stackContents)
                    {
                        var newName = string.Format("MediaRoll{0}_{1}", _stackContents.ItemsStack, stackContents.IndexOf(aContents));
                        var newMediaRoll = new MediaRoll
                        {
                            Width = _stackContents.StackWidth,
                            ListMedia = new ObservableCollection<M_MediaRollItem>(aContents.MediaRollItems)
                        };
                        newMediaRoll.MouseClicked += NewMediaRoll_MouseClicked;
                        _targetStack.Children.Add(newMediaRoll);
                        this.ReregisterName(newName, newMediaRoll);
                    }
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "AddStack()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }
        private void RemoveStack(StackPanel _targetStack, M_IntroItems _stackContents)
        {
            try
            {
                if (_stackContents.StackContents is List<M_StackContents> stackContents)
                {
                    foreach (var aContents in stackContents)
                    {
                        var oldName = string.Format("MediaRoll{0}_{1}", _stackContents.ItemsStack, stackContents.IndexOf(aContents));
                        var oldMediaRoll = this.FindName(oldName);
                        if (oldMediaRoll is MediaRoll removeMedia)
                        {
                            removeMedia.MouseClicked -= NewMediaRoll_MouseClicked;
                        }
                    }
                    _targetStack.Children.Clear();
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "RemoveStack()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }

        private void NewMediaRoll_MouseClicked(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is FrameworkElement getElement)
            {
                if (getElement.Tag is M_MediaRollItem selectItem)
                {
                    RaiseMouseClickedEvent(selectItem);
                }
            }
        }
        #endregion Methods

        #region [ Event Handlers ]
        private static void OnIntroItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                var target = d as IntroView;
                if (e.NewValue is ObservableCollection<M_IntroItems> introItems && introItems.Count > 0)
                {
                    if (e.OldValue is ObservableCollection<M_IntroItems> oldIntroItems)
                    {
                        if (introItems.Count.Equals(1))
                        {
                            target.RemoveStack(target.IntroMain, introItems.FirstOrDefault());

                        }
                        else if (introItems.Count.Equals(2))
                        {
                            foreach (var aIntroItem in introItems)
                            {
                                var oldStack = target.FindName(string.Format("IntroStack{0}", introItems.IndexOf(aIntroItem)));
                                if (oldStack is StackPanel removeStack)
                                {
                                    target.RemoveStack(removeStack, aIntroItem);
                                }

                            }
                            target.IntroMain.Children.Clear();
                        }
                    }
                    if (introItems.Count.Equals(1))
                    {
                        target.IntroMain.Orientation = Orientation.Vertical;
                        target.AddStack(target.IntroMain, introItems.FirstOrDefault());

                    }
                    else if (introItems.Count.Equals(2))
                    {
                        target.IntroMain.Orientation = Orientation.Horizontal;
                        foreach (var aIntroItem in introItems)
                        {
                            var newStackPanel = new StackPanel { Orientation = Orientation.Vertical };
                            target.AddStack(newStackPanel, aIntroItem);
                            target.IntroMain.Children.Add(newStackPanel);
                            target.ReregisterName(string.Format("IntroStack{0}", introItems.IndexOf(aIntroItem)), newStackPanel);
                        }
                    }
                }
                else
                {
                    if (e.OldValue is ObservableCollection<M_IntroItems> oldIntroItems)
                    {
                        if (oldIntroItems.Count.Equals(1))
                        {
                            target.RemoveStack(target.IntroMain, oldIntroItems.FirstOrDefault());

                        }
                        else if (oldIntroItems.Count.Equals(2))
                        {
                            foreach (var aIntroItem in oldIntroItems)
                            {
                                var oldStack = target.FindName(string.Format("IntroStack{0}", oldIntroItems.IndexOf(aIntroItem)));
                                if (oldStack is StackPanel removeStack)
                                {
                                    target.RemoveStack(removeStack, aIntroItem);
                                }

                            }
                            target.IntroMain.Children.Clear();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(d, "OnIntroItemsPropertyChanged()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }

        #endregion Event Handlers
    }
}
