from typing import Callable

from clarabot_nano.core.spawn.tika import TikaSpawn
from clarabot_nano.core.spawner.base import AbstractSpawner


class TikaSpawner(AbstractSpawner):
    def __init__(
        self,
        spawn_callback: Callable[[TikaSpawn], None],
        dispose_callback: Callable[[TikaSpawn], None],
        spawn_cls=TikaSpawn,
    ):
        self.__spawn_cls = spawn_cls
        AbstractSpawner.__init__(
            self,
            instance_number=1,
            only_spawn_when_clean=True,
            spawn_callback=spawn_callback,
            dispose_callback=dispose_callback,
        )

    def _spawn(self, index, nano_config):
        java_home, java_executable = nano_config.get_java_home_executable()
        tika_jar = nano_config.get_tika_server_jar()
        spawn = self.__spawn_cls(
            java_home,
            java_executable,
            tika_jar,
            nano_config.get_tika_logs_dir(),
            nano_config.get_pid_dir(),
            nano_config.get_tika_heap_init(),
            nano_config.get_tika_heap_size(),
        )
        spawn.start()
        return spawn

    def set_instance_number(self, number):
        raise RuntimeError("not supported")
