using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using ATK;
using Windows.UI.Core;

// 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 PageBeeper : Page
    {
        ReaderConnect readerobj;
        BaseReciever basereciverobj;
        public List<string> beeperLevels = new List<string>();
        public PageBeeper()
        {
            this.InitializeComponent();
            readerobj = ReaderConnect.getInstance();
            basereciverobj = BaseReciever.getInstance();
            beeperLevels.Add("HIGH");
            beeperLevels.Add("MEDIUM");
            beeperLevels.Add("LOW");                       
            ComboBeepLevel.ItemsSource = beeperLevels;
            togglebeep.IsOn = ReaderConfig.isBeeperOn;
            
            if (ReaderConfig.isBeeperOn)
                ComboBeepLevel.SelectedIndex = (int)ReaderConfig.beeperLevel;
            else
                ComboBeepLevel.IsEnabled = false;           
        }

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

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

        void App_BackRequested(object sender, BackRequestedEventArgs e)
        {
            sendBeeper();
            Frame.Navigate(typeof(PageSettings));
        
        }

        private void sendBeeper()
        {
            if (!ReaderConfig.isReaderConnected || !ReaderConfig.isInitialSettingsRecieved)
            {
                return;
            }
          
            if (togglebeep.IsOn != ReaderConfig.isBeeperOn ||(ComboBeepLevel.SelectedIndex!=(int)ReaderConfig.beeperLevel))
            {
                Command_SetAttr beeperObj = new Command_SetAttr();
                beeperObj.attnum = 140;
                beeperObj.atttype = "B";
                if (togglebeep.IsOn == false)
                    beeperObj.attvalue = (int)ENUM_BEEPER_VOLUME.QUITE_BEEP;
                else
                    beeperObj.attvalue = ComboBeepLevel.SelectedIndex;
                readerobj.writeData(beeperObj.ToString());
                Command_GetAttrInfo beerperGetObj = new Command_GetAttrInfo();
                beerperGetObj.attnum = 140;
                readerobj.writeData(beerperGetObj.ToString());  
            }                     
        }

        private void togglebeep_Toggled(object sender, RoutedEventArgs e)
        {
            if(togglebeep.IsOn==true)
                ComboBeepLevel.IsEnabled = true;
            else
                ComboBeepLevel.IsEnabled = false;
        }

              
    }
}