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

namespace BarCodeSamples
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class PhoneBarCodePage : Page
    {
        public PhoneBarCodePage()
        {
            this.InitializeComponent();
            this.Loaded += BarCodePage_Loaded;
            this.Unloaded += BarCodePage_Unloaded;
            List<CodeType> codeTypeList = new List<CodeType>()
            {
                CodeType.QRCode,
                CodeType.DataMatrix,
                CodeType.Pdf417,
                CodeType.Code39x,
                CodeType.Ansi39x,
                CodeType.Code93x
            };
            cbCodeType.ItemsSource = codeTypeList;
            cbCodeType.SelectedItem = CodeType.QRCode;
        }

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

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

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

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            var encodingText = (string)e.Parameter;
            this.barCode.DataContext = encodingText;
        }
    }
}
