#!/usr/bin/env python3
# coding=UTF-8
# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
runtime holder
"""

import importlib
from yr import _preload_native_libraries

_preload_native_libraries()


class RuntimeHolder:
    """
    runtime holder
    """

    def __init__(self):
        self.yr_runtime = None

    def init(self, runtime):
        """init"""
        self.yr_runtime = runtime

    def get_runtime(self):
        """get runtime"""
        runtime = self.yr_runtime
        if runtime is None:
            raise RuntimeError("runtime not enable, please call yr.init() first")
        return runtime


global_runtime = RuntimeHolder()


def save_real_instance_id(instance_id, need_order):
    """
    save real instance id
    :param instance_id: instance id
    :param need_order: need order
    :return: None
    """
    global_runtime.get_runtime().save_real_instance_id(instance_id, need_order)


def init(runtime=global_runtime) -> None:
    """
    init yr
    :param runtime: RuntimeHolder
    :return: None
    """
    config_manager_cls = importlib.import_module("yr.config_manager").ConfigManager
    if config_manager_cls().local_mode:
        local_mode_runtime_cls = importlib.import_module(
            "yr.local_mode.local_mode_runtime"
        ).LocalModeRuntime
        rt = local_mode_runtime_cls()
        rt.init()
    else:
        cluster_mode_runtime_cls = importlib.import_module(
            "yr.cluster_mode_runtime"
        ).ClusterModeRuntime
        rt = cluster_mode_runtime_cls()
        rt.init()
    runtime.init(rt)