using C1.BarCode;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

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

namespace ControlExplorer.Controls.BarCode
{
    /// <summary>
    /// Class defined to store the properties of a category
    /// </summary>
    class Category
    {
        public string Key { get; set; }
        public string Value { get; set; }
        public Format Format { get; set; }
        public Category(string key, string value, Format format)
        {
            Key = key;
            Value = value;
            Format = format;
        }
    }

    public sealed partial class Generator : UserControl
    {
        List<Category> Categories = new List<Category>();
        public Generator()
        {
            this.InitializeComponent();

            this.Loaded += Generator_Loaded;
        }

        void Generator_Loaded(object sender, RoutedEventArgs e)
        {
            CreateCategories();
            categories.DataContext = Categories;
        }

        private void CreateCategories()
        {
            if (Categories.Count == 0)
            {
                Categories.Add(new Category("ms-appx:///Resources/BarCode/Email.png", "Email", Format.Email));
                Categories.Add(new Category("ms-appx:///Resources/BarCode/Text.png", "Text", Format.Text));
                Categories.Add(new Category("ms-appx:///Resources/BarCode/Url.png", "Url", Format.Url));
                Categories.Add(new Category("ms-appx:///Resources/BarCode/VCard.png", "VCard", Format.VCard));
            }
        }

        private void categories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var gridView = sender as GridView;
            if (gridView.SelectedItem != null)
            {
                var selectedItem = (Category)gridView.SelectedItem;
                var format = selectedItem.Format;
                var frame = Window.Current.Content as Frame;
                frame.Navigate(typeof(Editor), format);
            }
        }

        private void Grid_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            var gridView = categories;
            if (gridView.SelectedItem != null)
            {
                var selectedItem = (Category)gridView.SelectedItem;
                var format = selectedItem.Format;
                var frame = Window.Current.Content as Frame;
                frame.Navigate(typeof(Editor), format);
            }
        }
    }
}
