#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 1998-2026 Stephane Galland <galland@arakhne.org>
#
# This program is free library; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; see the file COPYING.  If not,
# write to the Free Software Foundation, Inc., 59 Temple Place - Suite
# 330, Boston, MA 02111-1307, USA.

"""
Python implementation of an interpreter for the translators.
"""

import shutil
import re
from typing import override

from autolatex2.translator.interpreters.abstractinterpreter import AbstractTranslatorInterpreter
from autolatex2.config.configobj import Config
from autolatex2.utils.runner import ScriptOutput


class TranslatorInterpreter(AbstractTranslatorInterpreter):
	"""
	Definition of a Python implementation of an interpreter for the translators.
	"""

	def __init__(self,  configuration : Config):
		"""
		Construct a translator interpreter.
		:param configuration: The general configuration.
		:type configuration: Config
		"""
		super().__init__(configuration)

	# noinspection PyDeprecation
	@property
	@override
	def runnable(self) -> bool:
		"""
		Replies if the interpreter is runnable, i.e., the underground interpreter can be run.
		:return: True if the interpreter could be run.
		:rtype: bool
		"""
		if self.configuration:
			cmd = self.configuration.python_interpreter
		else:
			cmd = 'python3'
		return shutil.which(cmd) is not None

	
	@property
	@override
	def interpreter(self) -> str:
		"""
		Replies the name of the interpreter.
		:return: The name of the interpreter.
		:rtype: str
		"""
		if self.configuration:
			cmd = self.configuration.python_interpreter
		else:
			cmd = 'python3'
		return cmd


	@override
	def filter_variable_name(self, name : str) -> str:
		"""
		Filter the name of the variable.
		:param name: The name to filter.
		:type name: str
		:return: The filtering result, that must be a valid name in the translator's language.
		:rtype: str
		"""
		return "_%s" % name.strip()

	# noinspection PyMethodMayBeStatic
	def __find_line_prefix(self,  code_array : list[str]) -> str:
		prefix = ''
		if code_array and len(code_array) > 0:
			m = re.match('^([ \t]+)',  code_array[0])
			if m:
				prefix = m.group(1)
		return prefix

	def __reformat_code(self, code : str) -> str:
		code_array = code.rstrip().split("\n")
		prefix = self.__find_line_prefix(code_array)
		for i in range(len(code_array)):
			code_array[i] = code_array[i].replace(prefix,  '')
		return "\n".join(code_array)

	@override
	def run(self, code : str, show_script_on_error : bool = True) -> ScriptOutput:
		"""
		Run the interpreter.
		:param code: The Python code to interpret.
		:type code: str
		:param show_script_on_error: Indicates if the script must be output on the standard error output in case of an error. Default is True.
		:type show_script_on_error: bool
		:return: A quadruplet containing the standard output, the
				 error output, the error and the exit code.
		:rtype: tuple[str,str,Any,int]
		"""
		full_code = "#!/usr/bin/env " + self.configuration.python_interpreter + "\n\n"
		full_code = full_code + "from autolatex2.utils.runner import Runner\n"
		if 'python_script_dependencies' in self.global_variables and self.global_variables['python_script_dependencies']:
			for dep in self.global_variables['python_script_dependencies']:
				dep = str(dep).strip()
				if not dep.startswith('from ') and not dep.startswith('import '):
					full_code = full_code + 'import '
				full_code = full_code + dep
				full_code = full_code + "\n"
		full_code = full_code + "\n"
		full_code = full_code + self.__reformat_code(code)
		variables = dict()
		for k, v in self.global_variables.items():
			variables[self.filter_variable_name(k)] = v
		return self.run_python(full_code, False, variables, show_script_on_error)
