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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using VideoPlayer;

namespace Bellwether
{
    /// <summary>
    /// Interaction logic for ucMainControlPage.xaml
    /// </summary>
    public partial class ucMainControlPage : UserControl
    {
        //sprivate UIElement frontPage;

        private Storyboard mainContainterFadeOutSb;
        private Storyboard mainContainterFadeInSb;

        private PageType nextPage;

        private DispatcherTimer backHomeTimer;

        public ucMainControlPage()
        {
            InitializeComponent();
            InitializeStoryBoard();

            this.Loaded += UcMainControlPage_Loaded;
            this.TouchDown += new EventHandler<TouchEventArgs>(ResetRoutedEvent);
            this.TouchUp += new EventHandler<TouchEventArgs>(ResetRoutedEvent);
            this.TouchMove += new EventHandler<TouchEventArgs>(ResetRoutedEvent);
            this.MouseDown += new MouseButtonEventHandler(ResetRoutedEvent);
            this.MouseMove += new MouseEventHandler(ResetRoutedEvent);
        }

        private void UcMainControlPage_Loaded(object sender, RoutedEventArgs re)
        {
            //frontPage = this.uxMainContainter.Child;
            //if (frontPage is Pages.ucFrontPage frontpage)
            //{
            //    frontpage.GoPageRequested += Page_GoPageRequested;
            //}
            var frontPage = GetUcFrontPage();
            this.uxMainContainter.Child = frontPage;

            if (backHomeTimer == null)
            {
                backHomeTimer = new DispatcherTimer();
                backHomeTimer.Interval = TimeSpan.FromSeconds(Utility.Setting.BackHomeSec);
                backHomeTimer.Tick += BackHomeTimer_Tick;
            }
        }

        private void BackHomeTimer_Tick(object sender, EventArgs e)
        {
            if (VideoPlayer.VideoPlayer.playingCount == 0)
            {
                Page_GoPageRequested(null, PageType.FrontPage);
                backHomeTimer.Stop();
            }
        }

        private void InitializeStoryBoard()
        {
            mainContainterFadeOutSb = Utility.SbAssist.CreateOpacitySb(uxMainContainter, 1, 0, 500);
            mainContainterFadeOutSb.Completed += MainContainterFadeOutSb_Completed;
            mainContainterFadeInSb = Utility.SbAssist.CreateOpacitySb(uxMainContainter, 0, 1, 500);
            mainContainterFadeInSb.Completed += MainContainterFadeInSb_Completed;
        }

        private void MainContainterFadeInSb_Completed(object sender, EventArgs e)
        {
            if (nextPage != PageType.FrontPage)
                backHomeTimer.Start();
            else
                backHomeTimer.Stop();
        }

        private void MainContainterFadeOutSb_Completed(object sender, EventArgs e)
        {
            if (this.uxMainContainter.Child is Pages.ucHistoryHW historyWall)
            {
                historyWall.GoPageRequested -= Page_GoPageRequested;
            }
            if (this.uxMainContainter.Child is Pages.ucFrontPage frontPage)
            {
                frontPage.GoPageRequested -= Page_GoPageRequested;
            }

            if (nextPage == PageType.FrontPage)
            {
                this.uxMainContainter.Child = GetUcFrontPage();
            }
            else
            {
                this.uxMainContainter.Child = CreateHistoryUc(nextPage);
            }

            mainContainterFadeInSb.Begin();
        }

        private void Page_GoPageRequested(object sender, PageType e)
        {
            nextPage = e;
            mainContainterFadeOutSb.Begin();
        }

        private Pages.ucHistoryHW CreateHistoryUc(PageType type)
        {
            var tmp = new Pages.ucHistoryHW(type);
            tmp.GoPageRequested += Page_GoPageRequested;
            return tmp;
        }

        private void ResetRoutedEvent(object sender, RoutedEventArgs e)
        {
            if (backHomeTimer.IsEnabled)
            {
                backHomeTimer.Stop();
                backHomeTimer.Start();
            }
        }

        private Pages.ucFrontPage GetUcFrontPage()
        {
            var frontPage = new Pages.ucFrontPage();
            frontPage.GoPageRequested += Page_GoPageRequested;
            return frontPage;
        }
    }
}
