import os
import logging

from typing import (
	Optional,
)

logger = logging.getLogger(__name__)


class DebugPy:
	@classmethod
	def set_trace(
		cls,
		host: Optional[str] = None,
		port: Optional[int] = None,
		wait: Optional[bool] = None,
	) -> None:
		if host is None:
			host = '127.0.0.1'
		if port is None:
			port = 4444
		if wait is None:
			wait = True

		import debugpy

		if os.environ.get('DEBUGPY_RUNNING') != 'true':
			logger.info('debugpy init')
			import debugpy

			debugpy.listen((host, port))
			os.environ['DEBUGPY_RUNNING'] = 'true'

			if wait:
				debugpy.wait_for_client()
				debugpy.breakpoint()

			logger.info('debugpy done')
