from ...Internal.Core import Core
from ...Internal.CommandsGroup import CommandsGroup
from ...Internal import Conversions
from ...Internal.StructBase import StructBase
from ...Internal.ArgStruct import ArgStruct
from ... import enums


# noinspection PyPep8Naming,PyAttributeOutsideInit,SpellCheckingInspection
class BlerCls:
	"""Bler commands group definition. 18 total commands, 2 Subgroups, 8 group commands"""

	def __init__(self, core: Core, parent):
		self._core = core
		self._cmd_group = CommandsGroup("bler", core, parent)

	@property
	def setup(self):
		"""setup commands group. 1 Sub-classes, 6 commands."""
		if not hasattr(self, '_setup'):
			from .Setup import SetupCls
			self._setup = SetupCls(self._core, self._cmd_group)
		return self._setup

	@property
	def trigger(self):
		"""trigger commands group. 1 Sub-classes, 2 commands."""
		if not hasattr(self, '_trigger'):
			from .Trigger import TriggerCls
			self._trigger = TriggerCls(self._core, self._cmd_group)
		return self._trigger

	def load(self, filename: str) -> None:
		"""SCPI: BLER:LOAD \n
		Snippet: driver.bler.load(filename = 'abc') \n
		Loads the selected file from the default or the specified directory. Loaded are files with extension *.ber_bler Refer to
		'Accessing Files in the Default or Specified Directory' for general information on file handling in the default and in a
		specific directory. \n
			:param filename: string Filename or complete file path; file extension can be omitted
		"""
		param = Conversions.value_to_quoted_str(filename)
		self._core.io.write(f'BLER:LOAD {param}')

	def preset(self) -> None:
		"""SCPI: BLER:PRESet \n
		Snippet: driver.bler.preset() \n
		Sets the parameters of the BERT/BLER test generator to their default values (*RST values specified for the commands) .
		Not affected is the state set with the command .method RsSmbv.Bert.state/method RsSmbv.Bler.state. \n
		"""
		self._core.io.write(f'BLER:PRESet')

	def preset_with_opc(self, opc_timeout_ms: int = -1) -> None:
		"""SCPI: BLER:PRESet \n
		Snippet: driver.bler.preset_with_opc() \n
		Sets the parameters of the BERT/BLER test generator to their default values (*RST values specified for the commands) .
		Not affected is the state set with the command .method RsSmbv.Bert.state/method RsSmbv.Bler.state. \n
		Same as preset, but waits for the operation to complete before continuing further. Use the RsSmbv.utilities.opc_timeout_set() to set the timeout value. \n
			:param opc_timeout_ms: Maximum time to wait in milliseconds, valid only for this call."""
		self._core.io.write_with_opc(f'BLER:PRESet', opc_timeout_ms)

	# noinspection PyTypeChecker
	class ResultStruct(StructBase):  # From ReadStructDefinition CmdPropertyTemplate.xml
		"""Structure for reading output parameters. Fields: \n
			- Num_Block: int: integer Number of checked data bits.
			- Num_Errors: int: integer Number of error bits.
			- Error_Rate: float: float If no termination criterion has been reached since the beginning of the measurement, display the current quotient of NumErrors (number of error bits) and NumBlock (number of data bits) . When at least one final result has been reached in continuous measurement, display the most recent final result.
			- Meas_Finished: int: integer Status of measurement. 1 Measurement has been terminated, i.e. stopped or the defined number of data bits or error bits has been reached. 0 Measurement has not been terminated.
			- Clock_Detected: int: integer Status of clock line. 1 Clock line active. 0 Clock line not active.
			- Data_Detected: int: integer Status of data line. 1 Data line active. Only clocked data is detected. If the clock signal is missing, a data change is also not detected. 0 Data line is not active.
			- Synchronized: int: integer 1 The measurement is synchronized and the ratio is assumed realistic. That is, the clock and data lines are active and the following apples: NumErrors/NumBlock 0.1. 0 The measurement is not synchronized."""
		__meta_args_list = [
			ArgStruct.scalar_int('Num_Block'),
			ArgStruct.scalar_int('Num_Errors'),
			ArgStruct.scalar_float('Error_Rate'),
			ArgStruct.scalar_int('Meas_Finished'),
			ArgStruct.scalar_int('Clock_Detected'),
			ArgStruct.scalar_int('Data_Detected'),
			ArgStruct.scalar_int('Synchronized')]

		def __init__(self):
			StructBase.__init__(self, self)
			self.Num_Block: int = None
			self.Num_Errors: int = None
			self.Error_Rate: float = None
			self.Meas_Finished: int = None
			self.Clock_Detected: int = None
			self.Data_Detected: int = None
			self.Synchronized: int = None

	def get_result(self) -> ResultStruct:
		"""SCPI: BLER:RESult \n
		Snippet: value: ResultStruct = driver.bler.get_result() \n
		Queries the result of the last BLER measurement and responds with seven results, separated by commas.
		The first measurement following the start also queries intermediate results for the number of data bits, error bits and
		error rate. For method RsSmbv.Bler.Trigger.modeAUTO, only the final results of each single measurement are queried in the
		following measurements. Note: The restart of a new measurement is delayed until the first measurement result has been
		queried. The resulting brief measurement interruption is irrelevant because the subsequent measurement is synchronized
		within 24 data bits. \n
			:return: structure: for return value, see the help for ResultStruct structure arguments.
		"""
		return self._core.io.query_struct('BLER:RESult?', self.__class__.ResultStruct())

	def start(self) -> None:
		"""SCPI: BLER:STARt \n
		Snippet: driver.bler.start() \n
		Starts a continuous measurement. \n
		"""
		self._core.io.write(f'BLER:STARt')

	def start_with_opc(self, opc_timeout_ms: int = -1) -> None:
		"""SCPI: BLER:STARt \n
		Snippet: driver.bler.start_with_opc() \n
		Starts a continuous measurement. \n
		Same as start, but waits for the operation to complete before continuing further. Use the RsSmbv.utilities.opc_timeout_set() to set the timeout value. \n
			:param opc_timeout_ms: Maximum time to wait in milliseconds, valid only for this call."""
		self._core.io.write_with_opc(f'BLER:STARt', opc_timeout_ms)

	def get_state(self) -> bool:
		"""SCPI: BLER:STATe \n
		Snippet: value: bool = driver.bler.get_state() \n
		Activates/deactivates the measurement. \n
			:return: state: 1| ON| 0| OFF
		"""
		response = self._core.io.query_str('BLER:STATe?')
		return Conversions.str_to_bool(response)

	def set_state(self, state: bool) -> None:
		"""SCPI: BLER:STATe \n
		Snippet: driver.bler.set_state(state = False) \n
		Activates/deactivates the measurement. \n
			:param state: 1| ON| 0| OFF
		"""
		param = Conversions.bool_to_str(state)
		self._core.io.write(f'BLER:STATe {param}')

	def stop(self) -> None:
		"""SCPI: BLER:STOP \n
		Snippet: driver.bler.stop() \n
		Stops an ongoing measurement. \n
		"""
		self._core.io.write(f'BLER:STOP')

	def stop_with_opc(self, opc_timeout_ms: int = -1) -> None:
		"""SCPI: BLER:STOP \n
		Snippet: driver.bler.stop_with_opc() \n
		Stops an ongoing measurement. \n
		Same as stop, but waits for the operation to complete before continuing further. Use the RsSmbv.utilities.opc_timeout_set() to set the timeout value. \n
			:param opc_timeout_ms: Maximum time to wait in milliseconds, valid only for this call."""
		self._core.io.write_with_opc(f'BLER:STOP', opc_timeout_ms)

	def set_store(self, filename: str) -> None:
		"""SCPI: BLER:STORe \n
		Snippet: driver.bler.set_store(filename = 'abc') \n
		Saves the current settings into the selected file; the file extension (*.ber_bler) is assigned automatically. Refer to
		'Accessing Files in the Default or Specified Directory' for general information on file handling in the default and in a
		specific directory. \n
			:param filename: 'filename' Filename or complete file path.
		"""
		param = Conversions.value_to_quoted_str(filename)
		self._core.io.write(f'BLER:STORe {param}')

	# noinspection PyTypeChecker
	def get_unit(self) -> enums.BertUnit:
		"""SCPI: BLER:UNIT \n
		Snippet: value: enums.BertUnit = driver.bler.get_unit() \n
		No command help available \n
			:return: unit: No help available
		"""
		response = self._core.io.query_str('BLER:UNIT?')
		return Conversions.str_to_scalar_enum(response, enums.BertUnit)

	def set_unit(self, unit: enums.BertUnit) -> None:
		"""SCPI: BLER:UNIT \n
		Snippet: driver.bler.set_unit(unit = enums.BertUnit.ENGineering) \n
		No command help available \n
			:param unit: No help available
		"""
		param = Conversions.enum_scalar_to_str(unit, enums.BertUnit)
		self._core.io.write(f'BLER:UNIT {param}')

	def clone(self) -> 'BlerCls':
		"""Clones the group by creating new object from it and its whole existing subgroups
		Also copies all the existing default Repeated Capabilities setting,
		which you can change independently without affecting the original group"""
		new_group = BlerCls(self._core, self._cmd_group.parent)
		self._cmd_group.synchronize_repcaps(new_group)
		return new_group
