using System.ComponentModel;
using Windows.ApplicationModel.Activation;
using Windows.Phone.UI.Input;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace ControlExplorer
{
    public partial class SamplePage : Page, IWebAuthenticationContinuable, IFileOpenPickerContinuable,IFileSavePickerContinuable
    {
        public SamplePage()
        {
            InitializeComponent();
            Loaded += OnLoaded;
            Unloaded += OnUnloaded;
        }

        void OnLoaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            HardwareButtons.BackPressed += OnBackPressed;
        }

        void OnUnloaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            HardwareButtons.BackPressed -= OnBackPressed;
        }

        private void OnBackPressed(object sender, BackPressedEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                Frame.GoBack();
                e.Handled = true;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            var parameters = (NavigationParameters)e.Parameter;
            var controlVM = ViewModel.Instance.Controls[parameters.ControlId];
            var sample = controlVM.Samples[parameters.SampleIndex];
            sample.Control = controlVM;
            DataContext = sample;

            OnInitializeSample();
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);
            DataContext = null;
            bottomAppBar.PrimaryCommands.Clear();
            bottomAppBar.SecondaryCommands.Clear();
        }

        private void OnInitializeSample()
        {
            var sample = (SampleViewModel)DataContext;

            bottomAppBar.PrimaryCommands.Clear();
            bottomAppBar.SecondaryCommands.Clear();

            var exposeActions = sample.Demo as IExposeApplicationBarItems;
            if (exposeActions != null)
            {
                foreach (var item in exposeActions.ApplicationBarItems)
                {
                    if (item is AppBarButton && (item as AppBarButton).Icon != null ||
                        item is AppBarToggleButton && (item as AppBarToggleButton).Icon != null)
                        bottomAppBar.PrimaryCommands.Add(item);
                    else
                        bottomAppBar.SecondaryCommands.Add(item);
                }
            }

            bottomAppBar.Visibility = (bottomAppBar.PrimaryCommands.Count + bottomAppBar.SecondaryCommands.Count > 0) ? Windows.UI.Xaml.Visibility.Visible : Windows.UI.Xaml.Visibility.Collapsed;
        }

        public void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args)
        {
            var sample = DataContext as SampleViewModel;
            if (sample != null)
            {
                var webAuthContinuable = sample.Demo as IWebAuthenticationContinuable;
                if (webAuthContinuable != null)
                {
                    webAuthContinuable.ContinueWebAuthentication(args);
                }
            }
        }

        public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
        {
            var sample = DataContext as SampleViewModel;
            if (sample != null)
            {
                var filePickerContinuable = sample.Demo as IFileOpenPickerContinuable;
                if (filePickerContinuable != null)
                {
                    filePickerContinuable.ContinueFileOpenPicker(args);
                }
            }
        }

        public void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
        {
            var sample = DataContext as SampleViewModel;
            if (sample != null)
            {
                var fileSavePickerContinuable = sample.Demo as IFileSavePickerContinuable;
                if (fileSavePickerContinuable != null)
                {
                    fileSavePickerContinuable.ContinueFileSavePicker(args);
                }
            }
        }
    }
}