from .entity import Entity
from .matrix import np

class Status:
	def __init__(self, update_interval=10):
		self.progress = -1
		self.update_interval = update_interval

	@staticmethod
	def print_status(entity: Entity, status: dict):
		print("-------------------------------------------")
		for key in status.keys():
			value = status[key]['value']
			unit = status[key]['unit']
			if isinstance(value, float):
				print(f"{entity.name}: {key}              : {value:0.6f}{unit}")
			else:
				print(f"{entity.name}: {key}              : {value}{unit}")

		l2_norm = np.sum(np.square(entity.state.w_hv))/entity.state.w_hv.shape[1]
		print(f"{entity.name}: l2_norm              : {l2_norm}")

	def on_change(self, entity: Entity, status: dict|None=None) -> bool:
		if status is None:
			self.progress = -1
			return True

		do_continue = self.on_report(entity, status=status)
		return do_continue

	def want_report(self, progress) -> bool:
		if self.progress < 0 or progress - self.progress >= self.update_interval:
			self.progress = progress
			return True
		return False

	def on_report(self, entity: Entity, status: dict) -> bool:
		Status.print_status(entity, status)
		return True