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 PagePowerOptimization : Page
    {
        ReaderConnect readerobj;
        BaseReciever baserecieverObj;
        public PagePowerOptimization()
        {
            this.InitializeComponent();
            readerobj = ReaderConnect.getInstance();
            baserecieverObj = BaseReciever.getInstance();
            ToggelDynamicPower.IsOn = ReaderConfig.isDynmaicPowerEnabled;           
        }

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

        }

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

        private void AppBarButton_Click(object sender, RoutedEventArgs e)
        {
            sendDynamicPower();
            this.Frame.Navigate(typeof(PageSettings));
        }

        private async void sendDynamicPower()
        {
            if (ReaderConfig.isReaderConnected == false)
                return;
            if (ToggelDynamicPower.IsOn != ReaderConfig.isDynmaicPowerEnabled)
            {
                Command_SetDynamicPower dynamicPowerObj = new Command_SetDynamicPower();
                dynamicPowerObj.Enable = ToggelDynamicPower.IsOn;
                dynamicPowerObj.Disable = (!(ToggelDynamicPower.IsOn));
                readerobj.writeData(dynamicPowerObj.ToString());
                await baserecieverObj.isDPOConfigSetCompltete();
                ReaderConfig.isDPOSetComplete = false;
                readerobj.writeData(ASCIIProcessor.getCommandConfig(dynamicPowerObj.getCommandType(), CONFIG_TYPE.CURRENT));
                // at dpo change get battery notification
                Command_GetDeviceInfo deviceinfo = new Command_GetDeviceInfo();
                deviceinfo.battery = true;
                readerobj.writeData(deviceinfo.ToString());
            }
         
        }
    }
}
