from typing import Callable

from clarabot_nano_common.utility.path.resolve import resolve_path

from clarabot_nano.core.spawn.solr import SolrSpawn
from clarabot_nano.core.spawner.base import AbstractSpawner


class SolrSpawner(AbstractSpawner):
    def __init__(
        self,
        spawn_callback: Callable[[SolrSpawn], None],
        dispose_callback: Callable[[SolrSpawn], None],
        spawn_cls=SolrSpawn,
    ):
        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()
        solr_install_dir = nano_config.get_solr_install_dir()
        spawn = self.__spawn_cls(
            java_home,
            java_executable,
            solr_install_dir,
            resolve_path(solr_install_dir / "server", strict=True),
            nano_config.get_solr_data_dir(),
            nano_config.get_solr_logs_dir(),
            nano_config.get_pid_dir(),
            nano_config.get_solr_heap_init(),
            nano_config.get_solr_heap_size(),
        )
        spawn.start()
        return spawn

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