using System;

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

    {

        int selectedLinkProfileIndex=-1 ;

        ReaderConnect readerobj;

        BaseReciever basereciverobj;

        public PageAntennaConfig()

        {            

            this.InitializeComponent();

            readerobj = ReaderConnect.getInstance();

            basereciverobj = BaseReciever.getInstance();

        }



        public void selectedLinkProfileToDisplay() // to handle the missing link profile ID's

        {           

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

            {

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

                {

                    ReaderConfig.antennaConfigParams.LinkProfileIndex = (int)i;

                    break;

                }

            }

        }



        private void UpdateUI()

        {

            if (ReaderConfig.isReaderConnected && ReaderConfig.isInitialSettingsRecieved)

            {

                selectedLinkProfileIndex = ReaderConfig.antennaConfigParams.LinkProfileIndex;

                selectedLinkProfileToDisplay();

                ComboBoxLinkProfile.ItemsSource = ReaderConfig.linkProfiles;

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

                TextBoxTari.Text = ReaderConfig.antennaConfigParams.Tari.ToString();

                ComboBoxLinkProfile.SelectedIndex = ReaderConfig.antennaConfigParams.LinkProfileIndex;

            }



        }



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

            UpdateUI();

        }



        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)

        {

            sendAntennaConfig();

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

        }



        void App_BackRequested(object sender, BackRequestedEventArgs e)

        {

            sendAntennaConfig();

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

                   

        } 



        public async void sendAntennaConfig()

        {

            if (!ReaderConfig.isReaderConnected || !ReaderConfig.isInitialSettingsRecieved)

            {               

                return;

            }

            Command_SetAntennaConfiguration setAntennaConfigObj = new Command_SetAntennaConfiguration();

            int LinkProfileIndex = ReaderConfig.linkProfilesWithIndex[ComboBoxLinkProfile.SelectedIndex].RFindex; 

            if((TextBoxTari.Text!= ReaderConfig.antennaConfigParams.Tari.ToString()) || (TextBoxPower.Text!=ReaderConfig.antennaConfigParams.Power.ToString())

                ||(selectedLinkProfileIndex != LinkProfileIndex))

            {

                if (!TextBoxPower.Text.Equals("") && !TextBoxTari.Text.Equals(""))

                {

                    setAntennaConfigObj.Power = Convert.ToInt16(TextBoxPower.Text);

                    setAntennaConfigObj.Tari = Convert.ToInt32(TextBoxTari.Text);

                    setAntennaConfigObj.LinkProfileIndex = LinkProfileIndex;

                    readerobj.writeData(setAntennaConfigObj.ToString());

                    await basereciverobj.isAntennaConfigSetCompltete();

                    ReaderConfig.isAntennaSetComplete = false;

                    readerobj.writeData(ASCIIProcessor.getCommandConfig(setAntennaConfigObj.getCommandType(), CONFIG_TYPE.CURRENT));

                }

                else

                {

                    basereciverobj.showStatus(Command_SetAntennaConfiguration.commandName, "Invalid input");

                }

            }

        }

    }

}

