using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Popups;

using System;
using System.Collections.ObjectModel;

using BxlWinUniversalSDK;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace BxlUniversalTestAPP.Content
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class ConnectToPrinter : Page
    {
        //private MainPage rootPage;
        BxlUniversalSDK _BxlUniversalSDK;

        static ObservableCollection<BxlUniversalSDK.PairedPrinterInfo> pairedPrinter;

        private class PrinterInfo
        {
            public string DisplayName { get; set; }
            public string HostName { get; set; }
        }

        public ConnectToPrinter()
        {
            this.InitializeComponent();

            _BxlUniversalSDK = MainPage._BxlUniversalSDK;

            if (null != pairedPrinter)
            {
                if (0 != pairedPrinter.Count)
                {
                    foreach (var printer in pairedPrinter)
                    {
                        PairedList.Items.Add(new PrinterInfo { DisplayName = printer.DisplayName, HostName = printer.HostName });
                    }
                }
            }
        }

        private async void PortOpen_Click(object sender, RoutedEventArgs e)
        {
            var ret = await _BxlUniversalSDK.ConnectToPrinterAsync(PortName.Text);
     
            if (MainPage.BXL_SUCCESS != ret)
            {
                var dialog = new MessageDialog("Failed to connect printer.");

                dialog.ShowAsync();
            } 
            else
            {
                var dialog = new MessageDialog("Success to connect printer");
                dialog.ShowAsync();

                MainPage._ConnectedPrinter = true;
            }
        }

        private void PortClose_Click(object sender, RoutedEventArgs e)
        {
            _BxlUniversalSDK.ClosePrinter();

            MainPage._ConnectedPrinter = false;
        }

        private void SelectPairedPrinter(object sender, SelectionChangedEventArgs e)
        {
            if (null == PairedList.SelectedItem)
            {
                return;
            }

            try
            {
                PrinterInfo printer = PairedList.SelectedItem as PrinterInfo;

                PortName.Text = printer.HostName.Substring(1, printer.HostName.Length - 2);
            }
            catch (Exception ex)
            {
                var dialog = new MessageDialog(ex.Message);
                dialog.ShowAsync();
            }
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            PairedList.Items.Clear();

            if (null != pairedPrinter)
            {
                if (0 != pairedPrinter.Count)
                {
                    pairedPrinter.Clear();
                }
            }

            pairedPrinter = await _BxlUniversalSDK.GetPairedPrinterList();
            if (pairedPrinter == null)
            {
                return;
            }

            foreach (var printer in pairedPrinter)
            {
                PairedList.Items.Add(new PrinterInfo { DisplayName = printer.DisplayName, HostName = printer.HostName });
            }
        }
    }
}
