using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using ATK;
using System;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls.Primitives;
using System.Threading.Tasks;

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

namespace Zebra_RFID_Mobile
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class PageLocateTag : Page
    {        
        ReaderConnect readerobj;
        BaseReciever basereciverobj;
        public PageLocateTag()
        {
            this.InitializeComponent();
            ReaderConfig.onTagLocateScreen = true;
            readerobj = ReaderConnect.getInstance();
            basereciverobj = BaseReciever.getInstance();
            ButtonStart.DataContext = ReaderConfig.locateData;
            TextBoxTagID.DataContext = ReaderConfig.locateData;
            ProgressBarLocateTag.DataContext = ReaderConfig.locateData;            
            BatteryDPO.DataContext = ReaderConfig.batteryDPO;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ReaderConfig.onTagLocateScreen = true;
            //Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            ReaderConfig.onTagLocateScreen = false;
            base.OnNavigatedFrom(e);
            //Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
            SystemNavigationManager.GetForCurrentView().BackRequested -= App_BackRequested;
        }
        
        private void AppBarButton_Click(object sender, RoutedEventArgs e)
        {
            ReaderConfig.onTagLocateScreen = false;
            this.Frame.Navigate(typeof(MainPage));
        }

        private void AppBarButtonGoToInventory(object sender, RoutedEventArgs e)
        {
            ReaderConfig.onTagLocateScreen = false;
            this.Frame.Navigate(typeof(PageInventory));
        }

        private void AppBarButtonGoToRapidRead(object sender, RoutedEventArgs e)
        {
            ReaderConfig.onTagLocateScreen = false;
            this.Frame.Navigate(typeof(PageRapidRead));
        }

        private void AppBarButtonGoToAccess(object sender, RoutedEventArgs e)
        {
            ReaderConfig.onTagLocateScreen = false;
            this.Frame.Navigate(typeof(PageAccess));
        }

        private void AppBarButtonGoToLocateTag(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(PageLocateTag));
        }

        private async void ButtonStart_Click(object sender, RoutedEventArgs e)
        {
            if (ReaderConfig.isReaderConnected == false)
            {
                basereciverobj.showStatus(Command_LocateTag.commandName, "No active connection with reader");
                return;
            }
            if (ButtonStart.Content.ToString() == "START")
            {
                if (TextBoxTagID.Text.Trim() != "")
                {
                    ReaderConfig.isCommandComplete = false;
                    Command_LocateTag locateTag = new Command_LocateTag();
                    locateTag.epc = (byte[])ASCIIUtil.ParseArrayFromString(TextBoxTagID.Text.Trim(), "byteArray", "HEX");
                    if (locateTag.epc != null)
                    {
                        readerobj.writeData(locateTag.ToString());
                        ReaderConfig.locateData.LocateStartStop = "STOP";
                        ReaderConfig.locateData.LocateTagID_ON = false;
                        ReaderConfig.isLocateRunning = true;
                    }
                    else
                    {
                        basereciverobj.showStatus(Command_LocateTag.commandName, "Invalid input:TAG ID");
                    }
                }
                else
                {
                    basereciverobj.showStatus(Command_LocateTag.commandName, "Please fill tag ID");
                }
            }
            else
            {
                ReaderConfig.isCommandComplete = false;
                Command_abort abortObj = new Command_abort();
                readerobj.writeData(abortObj.ToString());
            }
            await basereciverobj.isCommandComplete();
            if (!ReaderConfig.SettingsStaus.ResponseStatusMsg.Equals("OK"))
            {
                TextLocateStatus.Text = ReaderConfig.SettingsStaus.ResponseStatusMsg;
                FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
                await Task.Delay(1000);
                LocateFlyout.Hide();
            }
        }

        void App_BackRequested(object sender, BackRequestedEventArgs e)
        {
            ReaderConfig.onTagLocateScreen = false;
            e.Handled = true;
            Frame.Navigate(typeof(MainPage));
        }       
        private void Batterybutton_Click(object sender, RoutedEventArgs e)
        {
            // navigate to battery / dpo
            ReaderConfig.onTagLocateScreen = false;
            this.Frame.Navigate(typeof(PageBattery));
        }
    }
}
