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

// 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 PageRapidRead : Page
    {
        ReaderConnect readerobj;
        BaseReciever baserecieverObj;
        public PageRapidRead()
        {
            this.InitializeComponent();
            readerobj = ReaderConnect.getInstance();
            baserecieverObj = BaseReciever.getInstance();
            TextBlockToatalTag.DataContext = ReaderConfig.TagsRead;
            TextBlockUniqueTag.DataContext = ReaderConfig.TagsRead;
            ButtonStart.DataContext = ReaderConfig.StartStopButtonStatus;
            TextBlockReadRate.DataContext = ReaderConfig.inventoryReadRate;
            TextBlockReadTime.DataContext = ReaderConfig.inventoryReadRate;
            TextBlockBatchModeMessage.DataContext = ReaderConfig.BatchMode;
            BatteryDPO.DataContext = ReaderConfig.batteryDPO;
            ReaderConfig.onRapidReadScreen = true;
            if(!ReaderConfig.isInventoryRunning)
                ReaderConfig.BatchMode.isEnabled = false;
            TextBlockBatchModeMessage.UpdateLayout();
        }

        /// <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.onRapidReadScreen = true;
            //Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            ReaderConfig.onRapidReadScreen = false;
            base.OnNavigatedFrom(e);
            //Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
            SystemNavigationManager.GetForCurrentView().BackRequested -= App_BackRequested;
        }

        private void AppBarButtonHome_Click(object sender, RoutedEventArgs e)
        {
            ReaderConfig.onRapidReadScreen = false;
            this.Frame.Navigate(typeof(MainPage));
        }

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

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

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

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

        void App_BackRequested(object sender, BackRequestedEventArgs e)
        {
            ReaderConfig.onRapidReadScreen = false;
            Frame.Navigate(typeof(MainPage));
        }
        private async void ButtonStart_Click(object sender, RoutedEventArgs e)
        {
            if (ReaderConfig.isReaderConnected == false)
            {
                baserecieverObj.showStatus(Command_Inventory.commandName, "No active connection with reader");
                return;
            }
            ReaderConfig.isCommandComplete = false;
            if (ButtonStart.Content.ToString() == "STOP")
            {
                baserecieverObj.abortOperation();
                TextBlockBatchModeMessage.Visibility = Visibility.Collapsed;
                TextBlockUniqueTag.Visibility = Visibility.Visible;
            }
            else
            {
                baserecieverObj.startInventory();
            }
            await baserecieverObj.isCommandComplete();
            if (!ReaderConfig.SettingsStaus.ResponseStatusMsg.Equals("OK") && !ReaderConfig.SettingsStaus.ResponseStatusMsg.Equals("Inventory Started in Batch Mode"))
            {
                TextInventoryStatus.Text = ReaderConfig.SettingsStaus.ResponseStatusMsg;
                FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
                await Task.Delay(1000);
                InventoryFlyout.Hide();
            }
            if(ReaderConfig.SettingsStaus.ResponseStatusMsg.Equals("Inventory Started in Batch Mode"))
            {
                TextBlockBatchModeMessage.UpdateLayout();
            }
        }

        private void Batterybutton_Click(object sender, RoutedEventArgs e)
        {
            // navigate to battery / dpo
            ReaderConfig.onRapidReadScreen = false;
            this.Frame.Navigate(typeof(PageBattery));
        }
    }
}
