from typing import Callable

from clarabot_nano.core.spawn.picoture import PicotureSpawn
from clarabot_nano.core.spawner.base import AbstractSpawner


class PicotureSpawner(AbstractSpawner):
    def __init__(
        self,
        spawn_callback: Callable[[PicotureSpawn], None],
        dispose_callback: Callable[[PicotureSpawn], None],
        spawn_cls=PicotureSpawn,
    ):
        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()
        picoture_jar = nano_config.get_picoture_jar()
        spawn = self.__spawn_cls(
            java_home,
            java_executable,
            picoture_jar,
            nano_config.get_picoture_logs_dir(),
            nano_config.get_pid_dir(),
            nano_config.get_picoture_heap_size(),
        )
        spawn.start()
        return spawn

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