using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

// At design time, open Solution Explorer and set the media
// file's "Copy to Output Directory" property to "Copy if Newer."

using System.Windows.Threading;

namespace ControlVideo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // A timer to display the video's location.
        private DispatcherTimer VideoTimer;

        // Create the timer and otherwise get ready.
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            VideoTimer = new DispatcherTimer();
            VideoTimer.Interval = TimeSpan.FromSeconds(0.1);
            VideoTimer.Tick += new EventHandler(timer_Tick);
            flutePlayer.Stop();
        }

        private void flutePlayer_MediaOpened(object sender, RoutedEventArgs e)
        {
            positionScrollBar.Minimum = 0;
            positionScrollBar.Maximum = flutePlayer.NaturalDuration.TimeSpan.TotalSeconds;
            positionScrollBar.Visibility = Visibility.Visible;
        }

        // Show the play position in the ScrollBar and TextBox.
        private void ShowPosition()
        {
            positionScrollBar.Value = flutePlayer.Position.TotalSeconds;
            positionTextBox.Text = flutePlayer.Position.TotalSeconds.ToString("0.0");
        }

        // Enable and disable appropriate buttons.
        private void EnableButtons(bool isPlaying)
        {
            if (isPlaying)
            {
                playButton.IsEnabled = false;
                pauseButton.IsEnabled = true;
                playButton.Opacity = 0.5;
                pauseButton.Opacity = 1.0;
            }
            else
            {
                playButton.IsEnabled = true;
                pauseButton.IsEnabled = false;
                playButton.Opacity = 1.0;
                pauseButton.Opacity = 0.5;
            }
            VideoTimer.IsEnabled = isPlaying;
        }

        private void playButton_Click(object sender, RoutedEventArgs e)
        {
            flutePlayer.Play();
            EnableButtons(true);
        }
        private void pauseButton_Click(object sender, RoutedEventArgs e)
        {
            flutePlayer.Pause();
            EnableButtons(false);
        }
        private void stopButton_Click(object sender, RoutedEventArgs e)
        {
            flutePlayer.Stop();
            EnableButtons(false);
            ShowPosition();
        }
        private void restartButton_Click(object sender, RoutedEventArgs e)
        {
            flutePlayer.Stop();
            flutePlayer.Play();
            EnableButtons(true);
        }
        private void fasterButton_Click(object sender, RoutedEventArgs e)
        {
            flutePlayer.SpeedRatio *= 1.5;
        }
        private void nextButton_Click(object sender, RoutedEventArgs e)
        {
            flutePlayer.Position += TimeSpan.FromSeconds(10);
            ShowPosition();
        }
        private void slowerButton_Click(object sender, RoutedEventArgs e)
        {
            flutePlayer.SpeedRatio /= 1.5;
        }
        private void previousButton_Click(object sender, RoutedEventArgs e)
        {
            flutePlayer.Position -= TimeSpan.FromSeconds(10);
            ShowPosition();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            ShowPosition();
        }

        private void setPositionButton_Click(object sender, RoutedEventArgs e)
        {
            TimeSpan timespan = TimeSpan.FromSeconds(double.Parse(positionTextBox.Text));
            flutePlayer.Position = timespan;
            ShowPosition();
        }
    }
}