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

        public SCR()
        {
            this.InitializeComponent();

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

        private async void SCRPowerUP_Click(object sender, RoutedEventArgs e)
        {
            BxlUniversalSDK.SCRData scrData;
            MessageDialog Dialog;

            scrData = await _BxlUniversalSDK.ScrPowerUp();

            if (0x00 != scrData.ResponseS)
            {
                Dialog = new MessageDialog("ScrPowerUp fail.");
                Dialog.ShowAsync();

                return;
            }

            Dialog = new MessageDialog("Success SCR Power UP.");
            Dialog.ShowAsync();
        }

        private async void SCRPowerDown_Click(object sender, RoutedEventArgs e)
        {
            byte ResponseS = 0xff;
            MessageDialog Dialog;

            ResponseS = await _BxlUniversalSDK.ScrPowerDown();

            if (0x00 != ResponseS)
            {
                Dialog = new MessageDialog("ScrPowerDown fail.");
                Dialog.ShowAsync();

                return;
            }

            Dialog = new MessageDialog("Success SCR Power Down.");
            Dialog.ShowAsync();
        }

        private async void SCRStatus_Click(object sender, RoutedEventArgs e)
        {
            BxlUniversalSDK.SCRStatus scrStatus;
            MessageDialog Dialog;

            scrStatus = await _BxlUniversalSDK.ScrCheckStatus();

            if (0x00 != scrStatus.ResponseS)
            {  
                Dialog = new MessageDialog("ScrCheckStatus fail.");
                Dialog.ShowAsync();

                return;
            }

            string strStatus;

            if ((scrStatus.status & 0x03) == MainPage.BXL_SCR_STS_5V)
            {
                strStatus = "Power : 5V,";
            }
            else if ((scrStatus.status & 0x03) == MainPage.BXL_SCR_STS_3V)
            {
                strStatus = "Power : 3V,";
            }
            else
            {
                strStatus = "Power : 1.8V,";
            }

            if ((scrStatus.status & MainPage.BXL_SCR_STS_POWERED) != 0)
            {
                strStatus += " Card powered,";
            }
            else
            {
                strStatus += " Card not powered,";
            }

            if ((scrStatus.status & MainPage.BXL_SCR_STS_INSERTED) != 0)
            {
                strStatus += " Card inserted.";
            }
            else
            {
                strStatus += " Card not inserted.";
            }

            Dialog = new MessageDialog(strStatus);
            Dialog.ShowAsync();
        }

        private async void SmartCard_Click(object sender, RoutedEventArgs e)
        {
            byte ResponseS = 0xff;
            MessageDialog Dialog;

            ResponseS = await _BxlUniversalSDK.ScrSelectCard(MainPage.BXL_SCR_SMARTCARD);

            if (0x30 != ResponseS)
            {
                Dialog = new MessageDialog("Failed to select smart card.");
                Dialog.ShowAsync();

                return;
            }

            Dialog = new MessageDialog("Success to select smart card.");
            Dialog.ShowAsync();
        }

        private async void SAM1_Click(object sender, RoutedEventArgs e)
        {
            byte ResponseS = 0xff;
            MessageDialog Dialog;

            ResponseS = await _BxlUniversalSDK.ScrSelectCard(MainPage.BXL_SCR_SAM1);

            if (0x31 != ResponseS)
            {
                Dialog = new MessageDialog("Failed to select smart card.");
                Dialog.ShowAsync();

                return;
            }

            Dialog = new MessageDialog("Success to select smart card.");
            Dialog.ShowAsync();
        }

        private async void SAM2_Click(object sender, RoutedEventArgs e)
        {
            byte ResponseS = 0xff;
            MessageDialog Dialog;

            ResponseS = await _BxlUniversalSDK.ScrSelectCard(MainPage.BXL_SCR_SAM2);

            if (0x32 != ResponseS)
            {
                Dialog = new MessageDialog("Failed to select smart card.");
                Dialog.ShowAsync();

                return;
            }

            Dialog = new MessageDialog("Success to select smart card.");
            Dialog.ShowAsync();
        }
    }
}
