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


class OpenIAdapter(BaseAdapter):
    def __init__(self):
        from c2net.context import prepare, upload_output
        self._prepare = prepare()
        self._upload_output = upload_output
        self.output = ""

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

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

    def input_prepare(self, data_type: str, file_path: str):
        if data_type == DataType.DATASET:
            name_no_ext, _ = os.path.splitext(get_dataset_input())
            return os.path.join(self._prepare.dataset_path, name_no_ext)

        if data_type == DataType.MODEL:
            name_no_ext, _ = os.path.splitext(get_model_input())
            return os.path.join(self._prepare.pretrain_model_path, name_no_ext)

        if data_type == DataType.CODE:
            name_no_ext, _ = os.path.splitext(get_code_input())
            return os.path.join(self._prepare.code_path, name_no_ext)
        jcwLogger.error(f"Unknown data type for input: {data_type}")
        return ""

    def output_prepare(self, data_type: str, file_path: str):
        self.output = os.path.join(self._prepare.output_path, file_path)
        return self.output

    def runtime_data_copy(self, remote_path: str, local_path: str):
        return remote_path
