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;

namespace TrailerPlayer
{
    public partial class Page : UserControl
    {
        ViewModel viewModel;
        public Page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
            
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //Create a ViewModel for the View and get all the videos by passing null as the argument
            viewModel = new ViewModel();
            viewModel.SearchVideos(null);
            viewModel.SearchCompleted += new EventHandler(viewModel_SearchCompleted);
            this.DataContext = viewModel;
            Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);
        }
        
        void Content_FullScreenChanged(object sender, EventArgs e)
        {
            if (Application.Current.Host.Content.IsFullScreen)
            {
                SearchControl.Visibility = Visibility.Collapsed;
            }
            else
            {
                SearchControl.Visibility = Visibility.Visible;
            }
        }

        void viewModel_SearchCompleted(object sender, EventArgs e)
        {
            //if the search returns any videos
            if (viewModel.Trailers.Count != 0)
            {
                result.Text = "" + viewModel.Trailers.Count + " videos.";
                trailerView.Begin();
            }
            //if not just leave it as it was and display the message
            else
            {
                result.Text = "No such video!";
            }
            
        }

        private void brnSearch_Click(object sender, RoutedEventArgs e)
        {
            viewModel.SearchVideos(txtSearch.Text);
        }


    }
}
