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 PageSave : Page

    {

        ReaderConnect readerobj;

        BaseReciever baserecieverobj;

        public PageSave()

        {

            this.InitializeComponent();            

            readerobj = ReaderConnect.getInstance();

            baserecieverobj = BaseReciever.getInstance();

        }



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

        {

            if (ReaderConfig.isReaderConnected == true && ReaderConfig.isInitialSettingsRecieved)

            {



                // TextBlockAntennaPower

                TextBlockAntennaPower.Text = ReaderConfig.antennaConfigParams.Power.ToString();



                // TextBlockLinkProfile

                TextBlockLinkProfile.Text = "";

                for (var i = 0; i < ReaderConfig.linkProfilesWithIndex.Count; i++)

                {

                    if (ReaderConfig.linkProfilesWithIndex[i].RFindex == ReaderConfig.antennaConfigParams.LinkProfileIndex)

                    {

                        TextBlockLinkProfile.Text = ReaderConfig.linkProfiles[i].ToString();

                        break;

                    }

                }



                // TextBlockSession

                TextBlockSession.Text = ReaderConfig.queryParams.QuerySession.ToString();



                // TextBlockTagPopulation

                TextBlockTagPopulation.Text = ReaderConfig.queryParams.Population.ToString();



                // TextBlockInventoryState

                TextBlockInventoryState.Text = ReaderConfig.queryParams.QueryTarget.ToString();



                // TextBlockSLFlag

                TextBlockSLFlag.Text = ReaderConfig.queryParams.QuerySelect.ToString();



                // TextBlockPC

                TextBlockPC.Text = ReaderConfig.reportConfigParams.ReportConfig.IncPC ? "On" : "Off";



                // TextBlockRSSI

                TextBlockRSSI.Text = ReaderConfig.reportConfigParams.ReportConfig.IncRSSI ? "On" : "Off";



                // TextBlockPhase

                TextBlockPhase.Text = ReaderConfig.reportConfigParams.ReportConfig.IncPhase ? "On" : "Off";



                // TextBlockChannelIndex

                TextBlockChannelIndex.Text = ReaderConfig.reportConfigParams.ReportConfig.IncChannelIndex ? "On" : "Off";



                // TextBlockTagSeenCount

                TextBlockTagSeenCount.Text = ReaderConfig.reportConfigParams.ReportConfig.IncTagSeenCount ? "On" : "Off";



                // TextBlockReportUniqueTags

                TextBlockReportUniqueTags.Text = ReaderConfig.reportUniqueTags ? "On" : "Off";



                // TextBlockBatchMode

                switch(ReaderConfig.batchmode)

                {

                    case 0:

                        TextBlockBatchMode.Text = "Disable";

                        break;

                    case 1:

                        TextBlockBatchMode.Text = "Auto";

                        break;

                    case 2:

                        TextBlockBatchMode.Text = "Enable";

                        break;

                    default:

                        TextBlockBatchMode.Text = "Not Set";

                        break;

                }



                // TextBlockDynamicPower

                TextBlockDynamicPower.Text = ReaderConfig.isDynmaicPowerEnabled ? "On" : "Off";



                // TextBlockStartTrigger

                TextBlockStartTrigger.Text = "Periodic";

                if ((ReaderConfig.startTriggerParams.IgnoreHandHeldTrigger == true) && (ReaderConfig.startTriggerParams.NoRepeat == true))

                {

                    TextBlockStartTrigger.Text = "Immediate";

                }

                else if ((ReaderConfig.startTriggerParams.StartOnHandHeldTrigger == true))

                {

                    TextBlockStartTrigger.Text = "Handheld";

                }



                // TextBlockStopTrigger

                TextBlockStopTrigger.Text = "Duration";

                if ((ReaderConfig.stopTriggerParams.IgnoreHandHeldTrigger) && (ReaderConfig.stopTriggerParams.DisableStopOnAccessCount)

                    && (ReaderConfig.stopTriggerParams.DisableStoponInventoryCount) && (ReaderConfig.stopTriggerParams.DisableStopOntagcount)

                    && (ReaderConfig.stopTriggerParams.DisableStopOnTimeout))

                {

                    TextBlockStopTrigger.Text = "Immediate";

                }

                else if (ReaderConfig.stopTriggerParams.StopOnHandHeldTrigger)

                {

                    TextBlockStopTrigger.Text = "Handheld";

                }

                else if (ReaderConfig.stopTriggerParams.EnableStopOntagcount)

                {

                    TextBlockStopTrigger.Text = "Tag Observations";

                }

                else if (ReaderConfig.stopTriggerParams.EnableStopOnInventoryCount)

                {

                    TextBlockStopTrigger.Text = "N Attempts";

                }



                // TextBlockSledBeeper

                TextBlockSledBeeper.Text = ReaderConfig.isBeeperOn ? "On" : "Off";



                // TextBlockBeeperVolume

                switch (ReaderConfig.beeperLevel)

                {

                    case ENUM_BEEPER_VOLUME.HIGH_BEEP:

                        TextBlockBeeperVolume.Text = "High";

                        break;

                    case ENUM_BEEPER_VOLUME.MEDIUM_BEEP:

                        TextBlockBeeperVolume.Text = "Medium";

                        break;

                    case ENUM_BEEPER_VOLUME.LOW_BEEP:

                        TextBlockBeeperVolume.Text = "Low";

                        break;

                    default:

                        TextBlockBeeperVolume.Text = "Not Set";

                        break;

                }

            }

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

        {

            this.Frame.Navigate(typeof(PageSettings));

        }



        private async void Button_Click(object sender, RoutedEventArgs e)

        {           

            Command_ChangeConfig saveobj = new Command_ChangeConfig();

            saveobj.Mode = ENUM_CHANGE_CONFIG_MODE.SAVE_CONFIG;

            readerobj.writeData(saveobj.ToString());

            bar1.Opacity = 100;

            bar1.IsIndeterminate = true;

            ButtonOK.Content = "Saving...";

            ButtonOK.IsEnabled = false;

            TextFlyout.Text = "The Settings are being Saved..."; 

            Page_Save.IsEnabled = false;

            await baserecieverobj.isConfigSaved();

            closeFlyout();        

        }



        public void closeFlyout()

        {            

            Page_Save.IsEnabled = true;

            TextFlyout.Text = "Saved...";            

            Flyout f = this.Flyout1 as Flyout;

            f.Hide();

            ButtonOK.Content = "OK , Save";

            ButtonOK.IsEnabled = true;

            bar1.Opacity = 0;

            bar1.IsIndeterminate = false;

            ReaderConfig.isConfigSaved = false;             

        }





        void App_BackRequested(object sender, BackRequestedEventArgs e)

        {

            Frame.Navigate(typeof(PageSettings));

        

        }



        private void ButtonSave_CLick(object sender, RoutedEventArgs e)

        {

            if (ReaderConfig.isReaderConnected == false)

            {

                Flyout f = this.Flyout1 as Flyout;

                f.Hide();

                baserecieverobj.showStatus(Command_ChangeConfig.commandName, "No active connection with reader");

                return;

            }

        }





    }

}

