using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Runtime.InteropServices;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media.Animation;
using SPC.Kiosk.Common;
using System.Threading.Tasks;
namespace SPC.Kiosk.Start
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class StartWindow : Window
    {
        public int ScreenNo { get; set; } = 0;
        public Size WindowSize { get; set; } = new Size(0, 0);
        public StartWindow()
        {
            StartInitialize();
        }
        public StartWindow(Size _size)
        {
            WindowSize = _size;
            StartInitialize();
        }
        public StartWindow(int _screenNo)
        {
            ScreenNo = _screenNo;
            StartInitialize();
        }

        public StartWindow(int _screenNo,Size _size)
        {
            ScreenNo = _screenNo;
            WindowSize = _size;
            StartInitialize();
        }
        private void StartInitialize()
        {
            InitializeComponent();
            this.Left = Screen.AllScreens[ScreenNo].WorkingArea.Left;
            this.Top = Screen.AllScreens[ScreenNo].WorkingArea.Top;
            this.Width = WindowSize.Width.Equals(0) ? Screen.AllScreens[ScreenNo].Bounds.Width : WindowSize.Width;
            this.Height = WindowSize.Height.Equals(0) ? Screen.AllScreens[ScreenNo].Bounds.Height : WindowSize.Height;
            this.Closed += StartWindow_Closed;
        }

        private void StartWindow_Closed(object sender, EventArgs e)
        {
            this.Loaded -= StartWindow_Loaded;
            this.DataContext = null;
            BindingOperations.ClearAllBindings(this);
            GC.SuppressFinalize(this);
        }

        ~StartWindow() { }
        private void StartWindow_Loaded(object sender, RoutedEventArgs e)
        {
            OpenBase();

        }
        private void BaseToTrade()
        {

            ItemList.DefaultPageNo = 0;
            var OpenAnimations = Animations.GetOpenAndCloseAnimation(this.BaseGrid, this.TradeGrid, OpenCloseAnimationType.GotoLeft, this.Width, 0.3);
            if (OpenAnimations != null)
            {
                OpenAnimations.Completed += OpenAnimations_Completed;
                OpenAnimations.Name = "TradeOpen";
                OpenAnimations.Begin();
            }
        }
        private void TradeToBase()
        {

            var OpenAnimations = Animations.GetOpenAndCloseAnimation(this.TradeGrid,this.BaseGrid, OpenCloseAnimationType.GotoRight, this.Width, 0.3);
            if (OpenAnimations != null)
            {
                OpenAnimations.Completed += OpenAnimations_Completed;
                OpenAnimations.Name = "BaseOpen";
                OpenAnimations.Begin();
            }

        }
        private void TradeToBasket()
        {
            
            var OpenAnimations = Animations.GetOpenAndCloseAnimation(this.TradeGrid,this.BasketGrid, OpenCloseAnimationType.GotoLeft, this.Width,0.3);
            if (OpenAnimations != null)
            {
                OpenAnimations.Completed += OpenAnimations_Completed;
                OpenAnimations.Name = "BasketOpen";
                OpenAnimations.Begin();
            }

        }
        private void BasketToTrade()
        {
            
            var OpenAnimations = Animations.GetOpenAndCloseAnimation(this.BasketGrid,this.TradeGrid, OpenCloseAnimationType.GotoRight, this.Width, 0.3);
            if (OpenAnimations != null)
            {
                OpenAnimations.Completed += OpenAnimations_Completed;
                OpenAnimations.Name = "TradeOpen";
                OpenAnimations.Begin();
            }

        }

        private void OpenBase()
        {
            this.TradeGrid.IsEnabled = false;
            this.BasketGrid.IsEnabled = false;
            this.TradeGrid.Visibility = Visibility.Hidden;
            this.BasketGrid.Visibility = Visibility.Hidden;
            var OpenAnimations = Animations.GetOpenAnimation(this.BaseGrid, OpenCloseAnimationType.GotoLeft, this.Width, 0.5);
            if (OpenAnimations != null)
            {
                OpenAnimations.Completed += OpenAnimations_Completed;
                OpenAnimations.Name = "BaseOpen";
                OpenAnimations.Begin();
            }
        }
        private void CloseBase()
        {
            var CloseAnimation = Animations.GetCloseAnimation(this.BasketGrid, OpenCloseAnimationType.GotoLeft, this.Width, 0.2);
            if (CloseAnimation != null)
            {
                CloseAnimation.Completed += CloseAnimation_Completed;
                CloseAnimation.Begin();
            }
        }
        private void OpenAnimations_Completed(object sender, EventArgs e)
        {
            var senderStoryboard = (sender as ClockGroup).Timeline as Storyboard;
            var getName = senderStoryboard.Name;
            switch (senderStoryboard.Name)
            {
                case "BaseOpen":
                    this.BaseGrid.IsEnabled = true;
                    break;
                case "TradeOpen":
                    this.TradeGrid.IsEnabled = true;
                    break;
                case "BasketOpen":
                    this.BasketGrid.IsEnabled = true;
                    break;

            }

        }
        private void CloseAnimation_Completed(object sender, EventArgs e)
        {
            this.Close();
        }
        private void CloseImage_Click(object sender, RoutedEventArgs e)
        {
            CloseBase();
        }
        private void TradeToBase_MouseClicked(object sender, RoutedEventArgs e)
        {
            TradeToBase();
        }
        private void TradeToBasket_MouseClicked(object sender, RoutedEventArgs e)
        {
            TradeToBasket();
        }
        private void BasketToTrade_MouseClicked(object sender, RoutedEventArgs e)
        {
            BasketToTrade();
        }
        private void BaseToTrade_MouseClicked(object sender, RoutedEventArgs e)
        {
            BaseToTrade();
        }
        private void ToggleButton_MouseClicked(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("ToggleButton Clicked");
        }

        private void ItemList_MouseClicked(object sender, RoutedEventArgs e)
        {
            var itemButton = e.OriginalSource as AnimationButton;
            Console.WriteLine("ClickItem:{0}",itemButton.NormalBrush);
        }
        private void TitleMedia3_MouseClicked(object sender, RoutedEventArgs e)
        {
            var target = sender as MediaPlayer;
            target.Restart();

        }

    }
}
