using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Phone.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace ControlExplorer.Controls.BarCode
{
    public sealed partial class Editor : Page
    {
        public Editor()
        {
            this.InitializeComponent();
            this.Loaded += Editor_Loaded;
            this.Unloaded -= Editor_UnLoaded;
        }

        void Editor_Loaded(object sender, RoutedEventArgs e)
        {
            HardwareButtons.BackPressed += OnBackPressed;
        }

        void Editor_UnLoaded(object sender, RoutedEventArgs e)
        {
            HardwareButtons.BackPressed -= OnBackPressed;
        }

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

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            Root.Children.Clear();
            var format = (Format)e.Parameter;
            Header.DataContext = format.ToString();
            switch (format)
            {
                case Format.Email:
                    Root.Children.Add(new Email() 
                    { 
                        DataContext = new EmailEntity() { Address = "sales@componentone.com" } 
                    });
                    break;
                case Format.Text:
                    Root.Children.Add(new Text() 
                    {
                        DataContext = new TextEntity() { Text = "C1BarCode control" } 
                    });
                    break;
                case Format.Url:
                    Root.Children.Add(new Url()
                    {
                        DataContext = new UrlEntity() { Url = "http://componentone.com" }
                    });
                    break;
                case Format.VCard:
                    Root.Children.Add(new VCard()
                    {
                        DataContext = new VCardEntity() 
                        {
                            Prefix = "Mr",
                            FirstName = "C1",
                            LastName = "Sales",
                            HomeCountry = "US",
                            Suffix = "Jr",
                            FullName = "Mr.C1.Sales.Jr",
                            Nickname = "C1Sales",
                            Title = "Developer",
                            Photo = "myimage.jpg",
                            Role = "Software developer"
                        }
                    });
                    break;
            }
        }

        private void GenerateBarCode(object sender, RoutedEventArgs e)
        {
            string text = string.Empty;
            if (Root.Children.Count != 0)
            {
                UserControl control = Root.Children[0] as UserControl;
                var entity = control.DataContext as Entity;
                if (entity != null)
                {
                    text = entity.EncodingText;
                }
            }
            Frame.Navigate(typeof(BarCodePage), text);
        }
    }
}
