using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;



namespace TrailerPlayer
{
    public partial class MediaPlayer : UserControl
    {

        public Uri Source
        {
            get { return (Uri)GetValue(SourceProperty); }
            set
            {
                SetValue(SourceProperty, value);
            }
        }
        /// <summary>
        /// Dependency property for XAML Databinding in TrailerView.xaml
        /// </summary>
        public readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(Uri), typeof(MediaPlayer), null);
        public ImageSource ThumbImage
        {
            get { return (BitmapImage)GetValue(ThumbImageProperty); }
            set
            {
                SetValue(ThumbImageProperty, value);
            }
        }
        public void Stop()
        {
            btnBack_Click(null, null);
        }
        /// <summary>
        /// Dependency property for XAML Databinding in TrailerView.xaml
        /// </summary>
        public readonly DependencyProperty ThumbImageProperty =
            DependencyProperty.Register("ThumbImage", typeof(ImageSource), typeof(MediaPlayer), null);
        private double originalwidth;
        private double originalheight;
        private Application app = Application.Current;
        private DispatcherTimer videoProgressTimer;
        RoutedPropertyChangedEventHandler<double> slrSeekerValueChanged;
        public MediaPlayer()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MediaPlayer_Loaded);

        }
        void MediaPlayer_Loaded(object sender, RoutedEventArgs e)
        {
            originalwidth = Main.Width;
            originalheight = Main.Height;
            Thumb.Source = ThumbImage;
            Thumb.MouseLeftButtonDown += new MouseButtonEventHandler(Thumb_MouseLeftButtonDown);
            btnFullScreen.Click += new RoutedEventHandler(btnFullScreen_Click);
            tbtnPlayPause.Click += new RoutedEventHandler(tbtnPlayPause_Click);
            btnBack.Click += new RoutedEventHandler(btnBack_Click);
            //VideoProgressChange=>Slider
            videoProgressTimer = new DispatcherTimer();
            videoProgressTimer.Interval = new TimeSpan(500);
            videoProgressTimer.Tick += new EventHandler(videoProgressTimer_Tick);
            Video.BufferingProgressChanged += new RoutedEventHandler(Video_BufferingProgressChanged);
            Video.DownloadProgressChanged += new RoutedEventHandler(Video_DownloadProgressChanged);
            //SliderChange=>VideoProgressChange
            slrSeekerValueChanged = new RoutedPropertyChangedEventHandler<double>(slrSeeker_ValueChanged);
            slrSeeker.ThumbDragStarted += new EventHandler<EventArgs>(slrSeeker_ThumbDragStarted);
            slrSeeker.ThumbDragCompleted += new EventHandler<EventArgs>(slrSeeker_ThumbDragCompleted);
            slrVolume.Value = Video.Volume;
            slrVolume.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slrVolume_ValueChanged);
            //btnMute
            Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);
            btnMute.Click += new RoutedEventHandler(btnMute_Click);
            Video.MediaEnded += new RoutedEventHandler(Video_MediaEnded);
            Controller.MouseEnter += new MouseEventHandler(Controller_MouseEnter);
            Controller.MouseLeave += new MouseEventHandler(Controller_MouseLeave);
        }

        void Content_FullScreenChanged(object sender, EventArgs e)
        {
            if (Application.Current.Host.Content.IsFullScreen == true)
            {
                this.Width = Application.Current.Host.Content.ActualWidth;
                this.Height = Application.Current.Host.Content.ActualHeight;

            }
            else
            {
                this.Width = originalwidth;
                this.Height = originalheight;

            }
        }
        void Controller_MouseLeave(object sender, MouseEventArgs e)
        {
            controller_go.Begin();
        }
        void Controller_MouseEnter(object sender, MouseEventArgs e)
        {
            controller_in.Begin();
        }
        void Thumb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Video.Source = Source;
            Video.MediaOpened += new RoutedEventHandler(Video_MediaOpened);
        }
        void Video_MediaOpened(object sender, RoutedEventArgs e)
        {
            thumb_go.Begin();
            thumb_go.Completed += new EventHandler(thumb_go_Completed);
            tbtnPlayPause.IsChecked = true;
            tbtnPlayPause_Click(null, null);
        }
        void thumb_go_Completed(object sender, EventArgs e)
        {
            Thumb.Visibility = Visibility.Collapsed;
        }
        void Video_DownloadProgressChanged(object sender, RoutedEventArgs e)
        {
            downloadProgress.Value = Video.DownloadProgress * 100;
        }
        void Video_BufferingProgressChanged(object sender, RoutedEventArgs e)
        {
            bufferingProgress.Value = Video.BufferingProgress * 100;
        }
        void Video_MediaEnded(object sender, RoutedEventArgs e)
        {
            btnBack_Click(null, null);
        }
        void slrVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Video.Volume = slrVolume.Value;
            if (slrVolume.Value == 0)
            {
                btnMute.IsChecked = true;
                btnMute_Click(null, null);
                btnMute.IsEnabled = false;
            }
            else
            {
                btnMute.IsChecked = false;
                Video.Volume = slrVolume.Value;
                btnMute_Click(null, null);
                btnMute.IsEnabled = true;
            }
        }
        void btnMute_Click(object sender, RoutedEventArgs e)
        {
            if (btnMute.IsChecked == true)
            {
                Video.IsMuted = true;
            }
            else
            {
                Video.IsMuted = false;
            }
        }
        void slrSeeker_ThumbDragCompleted(object sender, EventArgs e)
        {
            slrSeeker.ValueChanged -= slrSeekerValueChanged;
            //Azért hogy ha valaki eszetlenül rángatja a slidert indítás előtt
            if (Video.Source != null)
                videoProgressTimer.Start();

        }
        void slrSeeker_ThumbDragStarted(object sender, EventArgs e)
        {
            slrSeeker.ValueChanged += slrSeekerValueChanged;
            videoProgressTimer.Stop();
        }
        void videoProgressTimer_Tick(object sender, EventArgs e)
        {

            double position = (Video.Position.TotalSeconds / Video.NaturalDuration.TimeSpan.TotalSeconds) * 100;

            slrSeeker.Value = position;

        }
        void slrSeeker_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            long position = Video.NaturalDuration.TimeSpan.Ticks;
            position = position / 100;
            position = position * (int)slrSeeker.Value;
            Video.Position = new TimeSpan(position);
        }
        void btnBack_Click(object sender, RoutedEventArgs e)
        {
            Thumb.Visibility = Visibility.Visible;
            thumb_in.Begin();
            Video.Stop();
            slrSeeker.Value = 0;
            //ez azért mert amikor kiveszem a kurva szelekcióból akkor stoppolom a semmit.
            if (videoProgressTimer != null)
                videoProgressTimer.Stop();
            tbtnPlayPause.IsChecked = false;
            Video.Source = null;
        }
        void tbtnPlayPause_Click(object sender, RoutedEventArgs e)
        {
            if (Video.Source != null)
            {
                if (tbtnPlayPause.IsChecked == true)
                {
                    Video.Play();
                    //Azért hogy ha valaki eszetlenül rángatja a slidert indítás előtt
                    slrSeeker_ValueChanged(null, null);
                    videoProgressTimer.Start();
                }
                else
                {
                    Video.Pause();
                    videoProgressTimer.Stop();
                }
            }
            else
            {
                Thumb_MouseLeftButtonDown(null, null);
            }
        }

        void btnFullScreen_Click(object sender, RoutedEventArgs e)
        {
            if (Application.Current.Host.Content.IsFullScreen == true)
            {
                Application.Current.Host.Content.IsFullScreen = false;
            }
            else
            {

                Application.Current.Host.Content.IsFullScreen = true;
            }
        }
    }
}
