using System;
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 PageSingulation : Page
    {
        ReaderConnect readerobj;
        BaseReciever baseRecieverObj;
        private List<string> querysessions= new List<string>();
        private List<string> querytargets = new List<string>();
        private List<string> queryselect = new List<string>();
        public PageSingulation()
        {            
            this.InitializeComponent();
            readerobj = ReaderConnect.getInstance();
            baseRecieverObj = BaseReciever.getInstance();
            querysessions.Add("Session S0");
            querysessions.Add("Session S1");
            querysessions.Add("Session S2");
            querysessions.Add("Session S3");
            comboSession.ItemsSource = querysessions;

            querytargets.Add("STATE A");
            querytargets.Add("STATE B");
            querytargets.Add("AB FLIP");
            ComboInventoryState.ItemsSource = querytargets;

            queryselect.Add("All");
            queryselect.Add("DEASSERTED");
            queryselect.Add("ASSERTED");
            ComboSLFlag.ItemsSource = queryselect;

            comboSession.SelectedIndex =(int) ReaderConfig.queryParams.QuerySession;
            ComboInventoryState.SelectedIndex = (int)ReaderConfig.queryParams.QueryTarget;
            if ((int)ReaderConfig.queryParams.QuerySelect > 1)
                ComboSLFlag.SelectedIndex = (int)ReaderConfig.queryParams.QuerySelect - 1;
            else
                ComboSLFlag.SelectedIndex = 0;
            TextPopulation.Text = ReaderConfig.queryParams.Population.ToString();
        }

        /// <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)
        {
            sendQueryParams();
            this.Frame.Navigate(typeof(PageSettings));
        }

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

        public async void sendQueryParams()
        {
            if (!ReaderConfig.isReaderConnected || !ReaderConfig.isInitialSettingsRecieved)
            {
                 return;
            }
            bool send = false;
            if ((int)ReaderConfig.queryParams.QuerySelect > 1)
            {
                if (ComboSLFlag.SelectedIndex != (int)ReaderConfig.queryParams.QuerySelect - 1)
                {
                    send = true;
                }
            }
            else
            {
                if (ComboSLFlag.SelectedIndex != (int)ReaderConfig.queryParams.QuerySelect)
                {
                    send = true;
                }
            }
            if((comboSession.SelectedIndex !=(int) ReaderConfig.queryParams.QuerySession)||
                (ComboInventoryState.SelectedIndex != (int)ReaderConfig.queryParams.QueryTarget)||
                (TextPopulation.Text != ReaderConfig.queryParams.Population.ToString()))
            {
                send = true;
            }

            if(send)
            {
                Command_SetQueryParams queryParamsObj = new Command_SetQueryParams();
                if(!TextPopulation.Text.Equals(""))
                    queryParamsObj.Population = Convert.ToInt32(TextPopulation.Text);
                switch (ComboInventoryState.SelectedIndex)
                {
                    case 0:
                        queryParamsObj.QueryTarget = ENUM_STATE.STATE_A;
                        break;
                    case 1:
                        queryParamsObj.QueryTarget =  ENUM_STATE.STATE_B;
                        break;
                    case 2:
                        queryParamsObj.QueryTarget = ENUM_STATE.AB_FLIP;
                        break;                    
                }
                switch (comboSession.SelectedIndex)
                {
                    case 0:
                        queryParamsObj.QuerySession = ENUM_SESSION.SESSION_S0;
                        break;
                    case 1:
                        queryParamsObj.QuerySession = ENUM_SESSION.SESSION_S1;
                        break;
                    case 2:
                        queryParamsObj.QuerySession = ENUM_SESSION.SESSION_S2;
                        break;
                    case 3:
                        queryParamsObj.QuerySession = ENUM_SESSION.SESSION_S3;
                        break;
                }

                switch (ComboSLFlag.SelectedIndex)
                {
                    case 0:
                        queryParamsObj.QuerySelect = ENUM_SL_FLAG.SL_ALL;
                        break;
                    case 2:
                        queryParamsObj.QuerySelect = ENUM_SL_FLAG.SL_FLAG_ASSERTED;
                        break;
                    case 1:
                        queryParamsObj.QuerySelect = ENUM_SL_FLAG.SL_FLAG_DEASSERTED;
                        break;
                   
                }              
                readerobj.writeData(queryParamsObj.ToString());
                await baseRecieverObj.isQueryConfigSetCompltete();
                ReaderConfig.isSingulationSetComplete = false;
                readerobj.writeData(ASCIIProcessor.getCommandConfig(queryParamsObj.getCommandType(), CONFIG_TYPE.CURRENT));
            }
        }
    }
}