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.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;

using Windows.UI.Popups;
using BxlWinUniversalSDK;
using System.Text;
using System.Threading.Tasks;


// 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 MSR : Page
    {
        BxlUniversalSDK _BxlUniversalSDK;

        public MSR()
        {
            this.InitializeComponent();

            if (true == MainPage._ConnectedPrinter)
            {
                _BxlUniversalSDK = MainPage._BxlUniversalSDK;
            }
            else
            {
                var Dialog = new MessageDialog("First connect to the printer.");
                Dialog.ShowAsync();
            }
        }

        private async void MSRStart_Click(object sender, RoutedEventArgs e)
        {
            MessageDialog Dialog;

            MSR1Text.Text = "";
            MSR2Text.Text = "";
            MSR3Text.Text = "";

            try
            {
                Task<Int32> task = _BxlUniversalSDK.MSRReadReadyAsync();
                await task;

                if (MainPage.BXL_SUCCESS != task.Result)
                {
                    return;
                }

                byte[] track1 = new byte[120];
                byte[] track2 = new byte[120];
                byte[] track3 = new byte[120];

                Int32 ret = _BxlUniversalSDK.MSRReadTrack(ref track1, ref track2, ref track3);

                if (MainPage.BXL_MSR_DATAEMPTY == ret)
                {
                    Dialog = new MessageDialog("MSR DATA EMPTY");
                    Dialog.ShowAsync();

                    return;
                }

                string str1 = string.Format("{0}", Encoding.UTF8.GetString(track1, 0, track1.Length));
                string str2 = string.Format("{0}", Encoding.UTF8.GetString(track2, 0, track2.Length));
                string str3 = string.Format("{0}", Encoding.UTF8.GetString(track3, 0, track3.Length));

                /*
                MessageBox.Show(str1);
                MessageBox.Show(str2);
                MessageBox.Show(str3);
                */

                MSR1Text.Text = str1;
                MSR2Text.Text = str2;
                MSR3Text.Text = str3;

            }
            catch (Exception ex)
            {
                Dialog = new MessageDialog(ex.Message);
                Dialog.ShowAsync();
            }
        }

        private void MSRCancel_Click(object sender, RoutedEventArgs e)
        {
            _BxlUniversalSDK.MSRReadCancel();

            MSR1Text.Text = "";
            MSR2Text.Text = "";
            MSR3Text.Text = "";
        }
    }
}
