import os

from jcclang.adapter.base_adapter import BaseAdapter
from jcclang.core.const import DataType
from jcclang.core.logger import jcwLogger

from jcclang.core.context import get_dataset_input, get_code_input, get_model_input, get_data_output


class OctopusAdapter(BaseAdapter):
    def __init__(self):
        self.output = ""

    def before_task(self, inputs, context: dict):
        jcwLogger.info("execute before task")

    def after_task(self, outputs, context: dict):
        jcwLogger.info("execute after task")

    def input_prepare(self, data_type: str, file_path: str):
        if data_type == DataType.DATASET:
            data_path = get_dataset_input()
            if not data_path:
                jcwLogger.error("dataset_input is not set")
                return ""
            return os.path.join(data_path, file_path)

        if data_type == DataType.MODEL:
            data_path = get_model_input()
            if not data_path:
                jcwLogger.error("model_input is not set")
                return ""
            return os.path.join(data_path, file_path)

        if data_type == DataType.CODE:
            data_path = get_code_input()
            if not data_path:
                jcwLogger.error("code_input is not set")
                return ""
            return os.path.join(data_path, file_path)
        jcwLogger.error(f"Unknown data type for input: {data_type}")
        return ""

    def output_prepare(self, data_type: str, file_path: str):
        data_path = get_data_output()

        if not os.path.exists(data_path):
            jcwLogger.info(f"create output directory: {data_path}")
            os.makedirs(data_path)

        return os.path.join(data_path, file_path)

    def runtime_data_copy(self, remote_path: str, local_path: str):
        # 判断remote_path是不是以/开头，如果不是，则加上/
        if not remote_path.startswith("/"):
            remote_path = "/" + remote_path

        return remote_path
