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;

namespace BarCodeSamples
{
    public sealed partial class Editor : Page
    {
        public Editor()
        {
            this.InitializeComponent();
            if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            {
                this.Loaded += Editor_Loaded;
                this.Unloaded -= Editor_UnLoaded;
            }
        }

        public Format CurrentCategory
        {
            get { return (Format)GetValue(CurrentCategoryProperty); }
            set
            {
                SetValue(CurrentCategoryProperty, value);
            }
        }

        public static readonly DependencyProperty CurrentCategoryProperty =
            DependencyProperty.Register(
                "CurrentCategory",
                typeof(Format),
                typeof(Editor),
                new PropertyMetadata(Format.Url, OnCurrentCategoryPropertyChanged));
        
        private static void OnCurrentCategoryPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var editor = d as Editor;
            if (e.NewValue != null)
            {
                Format newValue = (Format)e.NewValue;
                editor.CurrentEditorControl.Children.Clear();
                UserControl control = null;
                switch (newValue)
                {
                    case Format.Url:
                        control = new Url();
                        editor.DataContext = new UrlEntity() { Url = "http://www.componentone.com" };
                        break;
                    case Format.Text:
                        control = new Text();
                        editor.DataContext = new TextEntity() { Text = "Welcome to use C1BarCode control" };
                        break;
                    case Format.Email:
                        control = new Email();
                        editor.DataContext = new EmailEntity() { Address ="sales@componentone.com" };
                        break;
                    case Format.VCard:
                        control = new VCard();
                        editor.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;
                }
                if (control != null)
                {
                    editor.CurrentEditorControl.Children.Add(control);
                }
            }
        }

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

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

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

        //this is for Phone
        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(PhoneBarCodePage), text);
        }
    }
}
