from functools import wraps
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import core.logger as logger
log = logger.get_logger()
from database.base import BaseConfig, AutoInstanceDecorator
from database.dirs import CheckDirAliasDecorator
from database.common import *


def BlackListUpdateRemoveDecorator(cls:BaseConfig):
    """黑名单更新装饰器：对于黑名单来说更新配置就是将默认的黑名单里面的项目删除
    Args:
        cls: 被装饰的类
    Returns:
        cls: 装饰后的类
    """
    cls.update_from_extra = cls.remove
    return cls

@AutoInstanceDecorator
@BlackListUpdateRemoveDecorator
class ProjectBlackList(BaseConfig):
    _config = [
            "project/tools/codesize",
            "project/image/codesize",
        ]
    def check(self) -> bool:
        for item in self._list:
            if not check_file_exists(item):
                log.warning(f"path {item} not exists")
        return True

@AutoInstanceDecorator
@CheckDirAliasDecorator
@BlackListUpdateRemoveDecorator
class BlackListDefault(BaseConfig):
    """BLACK_LIST_DEFAULT
    """
    _config = [
        "mi_ai",
        "mi_ao",
        "mi_aio",
        "mi_alsa",
        "mi_cipher",
        "mi_common",
        "mi_cus3a",
        "mi_debug",
        "mi_dummy",
        "mi_disp",
        "mi_dpu",
        "mi_dsp",
        "mi_fb",
        "mi_gfx",
        "mi_hdmi",
        "mi_hdmirx",
        "mi_hvp",
        "mi_ipu",
        "mi_iqserver",
        "mi_ive",
        "mi_jpd",
        "mi_mipitx",
        "mi_nir",
        "mi_panel",
        "mi_pcie",
        "mi_pspi",
        "mi_rgn",
        "mi_scl",
        "mi_sed",
        "mi_sensor",
        "mi_shadow",
        "sdk/impl/sys/",
        "mi_vdec",
        "mi_vdf",
        "sdk/interface/src/vcodec/",
        "mi_vdisp",
        "mi_wlan",
        "dualos",
        "otp",
        "usb_gadget_udc_usb30",
        "freertos",
        "context_switch",
        "lh_monitor",
        "pm_mhal_pm_default",
        "pm_mhal_pm_aio",
        "pm_mhal_pm_idle",
        "pm_mhal_pm_jpe",
        "pm_mhal_pm_md",
        "pm_mhal_pm_radar",
        "pm_mhal_pm_usbpoc",
        "pm_mhal_pm_vif",
        "pm_mhal_pm_usb_gadget_udc_usb30",
    ]

@AutoInstanceDecorator
@BlackListUpdateRemoveDecorator
@CheckDirAliasDecorator
class BlackListInternal(BaseConfig):
    """BLACK_LIST_INTERNAL
    """
    _config = [
        "mi_isp",
        "mi_ispalgo",
        "mi_ldc",
        "mi_venc",
        "mi_vif",
        "pm_mhal_pm_isp",
        "pm_mhal_pm_ispalgo",
        "pm_mhal_pm_radar_algo",
    ]

@AutoInstanceDecorator
@BlackListUpdateRemoveDecorator
class DemoBlackList(BaseConfig):
    """verify 目录下面的黑名单，不允许被 release 出去
    """
    _config = [
        "sdk/verify/mi_demo/source/ipu",
        "sdk/verify/vectorcast",
    ]