using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace Bellwether.Pages
{
    /// <summary>
    /// Interaction logic for ucScreenSavePage.xaml
    /// </summary>
    public partial class ucScreenSavePage : UserControl
    {
        public ucScreenSavePage()
        {
            InitializeComponent();

            Loaded += UcScreenSavePage_Loaded;
        }

        private static string ScreenSaveMov = "ScreenSave.mp4";

        public event EventHandler<PageType> GoPageRequested;

        private MediaTimeline mt;
        private MediaElement mediaSource;

        private Process PlayerProcess;

        private void UcScreenSavePage_Loaded(object sender, RoutedEventArgs e)
        {
            //StartMediaPlayer();
            //StartHiddenExitWindow();

            AddVideo();
        }

        private void StartHiddenExitWindow()
        {
            HiddenExitWindow HiddenExitWindow = new HiddenExitWindow();
            HiddenExitWindow.Closed += HiddenExitWindow_Closed;
            HiddenExitWindow.Show();
        }

        private void HiddenExitWindow_Closed(object sender, EventArgs e)
        {
            PlayerProcess.Close();
            GoPageRequested?.Invoke(this, PageType.FrontPage);
        }

        private void StartMediaPlayer()
        {
            var psi = new ProcessStartInfo()
            {
                FileName = "C:\\Program Files (x86)\\K-Lite Codec Pack\\MPC-HC64\\mpc-hc64.exe",
                Arguments = ".\\ScreenSave.mp4",
            };
            PlayerProcess = new Process() { EnableRaisingEvents = true };
            PlayerProcess.Exited += P_Exited;
            PlayerProcess.StartInfo = psi;
            PlayerProcess.Start();
        }

        private void P_Exited(object sender, EventArgs e)
        {
            GoPageRequested?.Invoke(this, PageType.FrontPage);
        }

        private void AddVideo()
        {

            mediaSource = new MediaElement();
            mt = new MediaTimeline(new Uri(Path.GetFullPath(ScreenSaveMov)));
            mediaSource.Clock = mt.CreateClock();
            //mediaSource.Clock.CurrentTimeInvalidated += media_CurrentTimeInvalidated;
            mediaSource.Clock.Completed += Video_Completed;
            mediaSource.LoadedBehavior = MediaState.Play;
            mediaSource.MediaOpened += MediaSource_MediaOpened;
            mediaSource.VerticalAlignment = VerticalAlignment.Bottom;
            RenderOptions.SetBitmapScalingMode(mediaSource, BitmapScalingMode.HighQuality);
            //mediaSource.Margin = new Thickness(0, -10, 0, -10);

            uxVideoPanel.Children.Add(mediaSource);
        }

        private void Video_Completed(object sender, EventArgs e)
        {
            Play();
        }

        private void MediaSource_MediaOpened(object sender, RoutedEventArgs e)
        {
            Play();
        }

        private void Play()
        {
            mediaSource.Clock.Controller.Stop();
            mediaSource.Clock.Controller.Begin();
        }

        int hiddenStage2 = 0;
        Timer hiddenStage2Timer;
        private void HiddenButton2_TouchDown(object sender, TouchEventArgs e)
        {
            GoPageRequested(this, PageType.FrontPage);
            return;

            TryInitTimer();

            hiddenStage2++;
            if (hiddenStage2 == 1)
            {
                hiddenStage2Timer.Start();
            }
            if (hiddenStage2 == 2)
            {
                hiddenStage2Timer.Stop();
                hiddenStage2 = 0;
                GoPageRequested(this, PageType.FrontPage);
            }

            void TryInitTimer()
            {
                if (hiddenStage2Timer != null)
                {
                    return;
                }

                hiddenStage2Timer = new Timer() { Interval = 1000 };
                hiddenStage2Timer.Elapsed += (sender2, e2) => {
                    hiddenStage2 = 0;
                    hiddenStage2Timer.Stop();
                };
            }
        }
    }
}
