using System;
using System.Collections.Generic;
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;
using System.Windows.Media.Animation;

namespace CPSC581___Tutorial_I
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int _animationDuration = 3;

        public MainWindow()
        {
            InitializeComponent();

            //Creating an event handler for the event that happens when one of the buttons is clicked
            wasdKB.OnWASDSelected += new EventHandler<WASDEventArgs>(wasdKB_OnWASDSelected);
            
        }

        /// <summary>
        /// Functino created for when 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void wasdKB_OnWASDSelected(object sender, WASDEventArgs e)
        {

            Storyboard sb = new Storyboard();
            switch (e.KeySelected)
            {
                case "W":

                    // Set the content of the label to be W clicked
                    eventLabel.Content = "Up Clicked!";
                    //Set the text color in the label to be Red
                    eventLabel.Foreground = Brushes.Red;

                    //Animation that handles movement, based on thickness
                    ThicknessAnimation tAnimation = new ThicknessAnimation();

                    //We want animation to go from current location, so we use margin
                    tAnimation.From = logoImage.Margin;

                    //We wnat the animation to goto a new location, up 50 pixels, so we adjust the top parameter
                    tAnimation.To = new Thickness(logoImage.Margin.Left,logoImage.Margin.Top-50,0,0);

                    //We want the animation to go for 3 seconds. So we now have an animation that moves the margin (or location) of an image up 50 pixels for 3 seconds
                    tAnimation.Duration = new Duration(TimeSpan.FromSeconds(3));

                    //Creating a bouncing animation effect
                    BounceEase bEasy = new BounceEase();

                    //Add 2 bounces to the animation, and set bounciness to 1
                    bEasy.Bounces = 2;
                    bEasy.Bounciness = 1;

                    //Let the animation ease into the bounces
                    bEasy.EasingMode = EasingMode.EaseIn;

                    //Set the animation effect to the bounce
                    tAnimation.EasingFunction = bEasy;

                    //To assign the animation to the image, we first set the target of the animation to the image
                    Storyboard.SetTarget(tAnimation, logoImage);

                    //Now that we've set what we're animating, we need to set what we're going to animate in the image
                    //To do this, we tell the animation to animate the margin property of an assigned image (which is logoImage)
                    Storyboard.SetTargetProperty(tAnimation, new PropertyPath("(Image.Margin)"));

                    //Now that we have setup the animation, we add it to the storyboard
                    sb.Children.Add(tAnimation);

                    break;
                case "A":

                    // Set the content of the label to be W clicked
                    eventLabel.Content = "Left Clicked!";
                    //Set the text color in the label to be Red
                    eventLabel.Foreground = Brushes.Green;

                    break;
                case "S":

                    // Set the content of the label to be W clicked
                    eventLabel.Content = "Down Clicked!";
                    //Set the text color in the label to be Red
                    eventLabel.Foreground = Brushes.Blue;


                    //Animation that handles movement, based on thickness
                    ThicknessAnimation tAnimation2 = new ThicknessAnimation();

                    //We want animation to go from current location, so we use margin
                    tAnimation2.From = logoImage.Margin;

                    //We wnat the animation to goto a new location, up 50 pixels, so we adjust the top parameter
                    tAnimation2.To = new Thickness(logoImage.Margin.Left, logoImage.Margin.Top + 50, 0, 0);

                    //We want the animation to go for 3 seconds. So we now have an animation that moves the margin (or location) of an image up 50 pixels for 3 seconds
                    tAnimation2.Duration = new Duration(TimeSpan.FromSeconds(3));

                    //Create an elastic animation effect
                    ElasticEase cEasy = new ElasticEase();

                    //Let the elastic animation have a springiness and 3 oscillations
                    cEasy.Springiness = 1;
                    cEasy.Oscillations = 3;

                    //Set the animation to ease in/out
                    cEasy.EasingMode = EasingMode.EaseInOut;

                    //Set the animation effect to the bounce
                    tAnimation2.EasingFunction = cEasy;

                    //To assign the animation to the image, we first set the target of the animation to the image
                    Storyboard.SetTarget(tAnimation2, logoImage);

                    //Now that we've set what we're animating, we need to set what we're going to animate in the image
                    //To do this, we tell the animation to animate the margin property of an assigned image (which is logoImage)
                    Storyboard.SetTargetProperty(tAnimation2, new PropertyPath("(Image.Margin)"));

                    //Now that we have setup the animation, we add it to the storyboard
                    sb.Children.Add(tAnimation2);

                    break;
                case "D":

                    // Set the content of the label to be W clicked
                    eventLabel.Content = "Right Clicked!";
                    //Set the text color in the label to be Red
                    eventLabel.Foreground = Brushes.Orange;

                    break;

                case "P":
                    // Set the content of the label to be W clicked
                    numberLabel.Content = e.TextBoxValue;
                    _animationDuration = e.TextBoxValue;
                    break;

            }

            sb.Begin();
        }

        /// <summary>
        /// Window Loaded method that contains an animation getting larger
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //This is similar to setting the source in XAML side, but first, we create a
            //Bitmap image source, which will eventually contain the actual image file
            BitmapImage bmiSource = new BitmapImage();
            bmiSource.BeginInit();

            //Create a uri source, which essentially assigns the bitmapmage image the correct image file
            //I've given it a relative path, but if you'd like to give it a fixed path, UriKind.Absolute is
            //what you would change
            bmiSource.UriSource = new Uri(@"/Resources/Images/iphone1.png", UriKind.Relative);
            bmiSource.EndInit();

            //Set the source of the logologoImage3 Image Control created in XAML to this image source we've just created
            logoImage3.Source = bmiSource;

            //Stretch the image uniformly inside the image control
            logoImage3.Stretch = Stretch.Uniform;
        }
    }
}
