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 PageTagReport : Page
	{
		List<string> batchmode= new  List<string>();
		ReaderConnect readerobj;
		BaseReciever baseReciverObj;
		public PageTagReport()
		{
			this.InitializeComponent();
			baseReciverObj = BaseReciever.getInstance();
			if (ReaderConfig.isReaderConnected && ReaderConfig.isInitialSettingsRecieved)
			{
				readerobj = ReaderConnect.getInstance();
				toggleChannelIndex.IsOn = ReaderConfig.reportConfigParams.ReportConfig.IncChannelIndex;
				togglePc.IsOn = ReaderConfig.reportConfigParams.ReportConfig.IncPC;
				togglePhase.IsOn = ReaderConfig.reportConfigParams.ReportConfig.IncPhase;
				toggleRSSI.IsOn = ReaderConfig.reportConfigParams.ReportConfig.IncRSSI;
				toggleTagSeenCount.IsOn = ReaderConfig.reportConfigParams.ReportConfig.IncTagSeenCount;
				batchmode.Add("Disable");
				batchmode.Add("Auto");
				batchmode.Add("Enable");
				ComboBatchmode.ItemsSource = batchmode;
				ComboBatchmode.SelectedIndex = ReaderConfig.batchmode;
				toggleReportUniqueTags.IsOn = ReaderConfig.reportUniqueTags;
			}            
		}

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

		void App_BackRequested(object sender, BackRequestedEventArgs e)
		{
			sendReportConfigCommand();
			this.Frame.Navigate(typeof(PageSettings));
		}    
		
		private async void sendReportConfigCommand()
		{
			if (ReaderConfig.batchmode != ComboBatchmode.SelectedIndex)
			{
				Command_SetAttr batchmodeParam = new Command_SetAttr();
				batchmodeParam.attnum = 1500;
				batchmodeParam.atttype = "B";
				batchmodeParam.attvalue = ComboBatchmode.SelectedIndex;
				readerobj.writeData(batchmodeParam.ToString());
				Command_GetAttrInfo batchGetObj = new Command_GetAttrInfo();
				batchGetObj.attnum = 1500;  // attribute number for Batchmode
				readerobj.writeData(batchGetObj.ToString());
					   
			}
			// TODO get config after successful set into genXConfig varaibles - in case of failed cmd ui reflects failed values
			if (!ReaderConfig.isReaderConnected || !ReaderConfig.isInitialSettingsRecieved)
			{
				return;
			}
			if((toggleChannelIndex.IsOn != ReaderConfig.reportConfigParams.ReportConfig.IncChannelIndex)||
				(togglePc.IsOn != ReaderConfig.reportConfigParams.ReportConfig.IncPC)||
				(togglePhase.IsOn != ReaderConfig.reportConfigParams.ReportConfig.IncPhase)||
				(toggleRSSI.IsOn != ReaderConfig.reportConfigParams.ReportConfig.IncRSSI) ||
				(toggleTagSeenCount.IsOn != ReaderConfig.reportConfigParams.ReportConfig.IncTagSeenCount))
			{
			   
				Command_SetReportConfig reportConfigObj= new Command_SetReportConfig();
				Param_ReportConfig reportconfig =new Param_ReportConfig();
				reportconfig.IncChannelIndex = toggleChannelIndex.IsOn;
				reportconfig.ExcChannelIndex = (!(toggleChannelIndex.IsOn));
				reportconfig.IncPC = togglePc.IsOn;
				reportconfig.ExcPC = (!(togglePc.IsOn));
				reportconfig.IncPhase = togglePhase.IsOn;
				reportconfig.ExcPhase = (!(togglePhase.IsOn));
				reportconfig.IncRSSI = toggleRSSI.IsOn;
				reportconfig.ExcRSSI = (!(toggleRSSI.IsOn));
				reportconfig.IncTagSeenCount = toggleTagSeenCount.IsOn;
				reportconfig.ExcTagSeenCount = !toggleTagSeenCount.IsOn;
				reportConfigObj.ReportConfig=reportconfig;
				readerobj.writeData(reportConfigObj.ToString());
				await baseReciverObj.isTagReportConfigSetCompltete();
				ReaderConfig.isTagReportSetComplete = false;
				readerobj.writeData(ASCIIProcessor.getCommandConfig(reportConfigObj.getCommandType(), CONFIG_TYPE.CURRENT));
			}

			// update setting of report unique tags
			if(toggleReportUniqueTags.IsOn != ReaderConfig.reportUniqueTags)
			{
				Command_SetUniqueReport setUniqueReport = new Command_SetUniqueReport();
				if(toggleReportUniqueTags.IsOn)
				{
					setUniqueReport.Enable = true;
				}
				else
				{
					setUniqueReport.Disable = true;

				}
				readerobj.writeData(setUniqueReport.ToString());
				readerobj.writeData(ASCIIProcessor.getCommandConfig(setUniqueReport.getCommandType(), CONFIG_TYPE.CURRENT));
			}
		}
	}
}
