#####
# 
# This class is part of the Programming the Internet of Things project.
# 
# It is provided as a simple shell to guide the student and assist with
# implementation for the Programming the Internet of Things exercises,
# and designed to be modified by the student as needed.
#

import logging
import random

from programmingtheiot.data.ActuatorData import ActuatorData

class BaseActuatorSimTask():
	"""
	This is a Base Actuator Simulator Task which will act as a parent class for other specific actuator tasks.
	
	"""

	def __init__(self, actuatorType: int = ActuatorData.DEFAULT_ACTUATOR_TYPE, simpleName: str = "Actuator"):
		"""
		This is used to initialize the Base Class BaseActuatorSimTask. It takes in Parameter actuatorType and simpleName.
		"""
		self.actuatorType = actuatorType
		#print(self.actuatorType, "BaseAct...")
		self.simpleName = simpleName
		self.latestActuatorData = ActuatorData(actuatorType = self.actuatorType)
		
	def activateActuator(self, val: float) -> bool:
		"""
		This method is used to activate the actuator who calls this function. i.e this method will set the Command Value to 1.
		@param val is the value of the actuator which will be set within this method
		Returns True if everything works
		"""
		logging.info("Actuator is sent ON command")
		self.latestActuatorData.setCommand(ActuatorData.COMMAND_ON)
		self.latestActuatorData.setValue(val)
		return True
		
	def deactivateActuator(self) -> bool:
		"""
		This Method is used to Deactivate the actuator who calls this function. i.e this method will set the Command Value to 0.
		Returns True is Command is Set Successfully 
		"""
		logging.info("Actuator is sent OFF command")
		self.latestActuatorData.setCommand(ActuatorData.COMMAND_OFF)
		return True
		
	def getLatestActuatorResponse(self) -> ActuatorData:
		"""
		This Method Returns the latestActuatorData Object
		"""
		return self.latestActuatorData
	
	def getSimpleName(self) -> str:
		"""
		This Method returns the name of Actuator Object
		"""
		return self.simpleName
	
	def updateActuator(self, data: ActuatorData) -> bool:
		"""
		This Method is used to update the Actuator Object.
		It will check the command state of the Object. If the Command is 0, Then this method will call self.deactivateActuator() Method.
		Else the method will call self.activateActuator() method
		@param data is the ActuatorData object which is passed to this method
		This Data will then be set to self.latestActuatorData
		Returns True if everything works accordingly. 
		"""
		if (data != None):
			if (data.getCommand() == 1):
				self.activateActuator(data.getValue())
			elif (data.getCommand() == 0):
				self.getLatestActuatorResponse()
				self.deactivateActuator()
			self.latestActuatorData = data
			logging.info("Updated Data " + str(self.latestActuatorData))
			self.latestActuatorData.setStatusCode(data.getStatusCode())
			self.latestActuatorData.setAsResponse()
			return True
		
		