import os
from common.errors import KareErrors
from common.utils import PathManager


class BaseRelease(PathManager, KareErrors):

    def __init__(self, release):
        child_release = {
            "v10":V10(),
            "v10sp1":V10SP1(),
            "shadow":Shadow(),
            "u16":U16(),
            "u18":U18(),
            "u20":U20(),
            "u22":U22(),
            "u24":U24(),
        }
        self.release = child_release.get(release)
        if self.release is None:
            raise RuntimeError(self.UNSUPPORTED_RELEASE)

        if os.path.exists(self.release.config_file_path) is False:
            raise RuntimeError(self.RELEASE_CONFIG_NOT_EXIST)

    """
    @brief 获取构建系统版本
    @return 系统版本
    """
    @property
    def get_build_suite(self) -> str:
        return self.get_config_json(self.config_file_path, "BUILD_SUITE")
    
    """
    @brief 获取构建镜像源
    @return 镜像源
    """
    @property
    def get_build_mirrors(self) -> str:
        return self.get_config_json(self.config_file_path, "MIRRORS")
    
    """
    @brief 获取构建组件
    @return 构建组件
    """
    @property
    def get_build_components(self) -> str:
        return ",".join(self.get_config_json(self.config_file_path, "COMPONENTS"))

    """
    @brief 获取构建包列表
    @return 包列表
    """  
    @property
    def get_build_includes(self) -> list:
        return self.get_config_json(self.config_file_path, "INCLUDES")

    """
    @brief 获取补充镜像源
    @return 镜像源
    """   
    @property
    def get_extra_mirrors(self) -> list:
        return self.get_config_json(self.config_file_path, "EXTRA_MIRRORS")

    """
    @brief 获取补充包
    @return 包列表
    """  
    @property
    def get_extra_includes(self) -> str:
        return " ".join(self.get_config_json(self.config_file_path, "EXTRA_INCLUDES"))


class V10(BaseRelease):

    def __init__(self):
        self.config_file_path = "/etc/kare/base/v10"

class V10SP1(BaseRelease):

    def __init__(self):
        self.config_file_path = "/etc/kare/base/v10sp1"


class Ubuntu(BaseRelease):

    def __init__(self):
        self.MIRRORS = "http://mirrors.tuna.tsinghua.edu.cn/ubuntu/"
        self.COMPONENTS = [
                "main",
                "universe",
                "multiverse",
                "restricted" 
            ]
        self.INCLUDES = [
                "bash",
                "bash-completion",
                "curl",
                "wget",
                "file",
                "libc-bin",
                "lsb-release",
                "grub-efi-$BUILD_ARCH",
                "dialog",
                "locales",
                "gettext-base",
                "language-pack-zh-hans",
                "fonts-wqy-microhei",
                "fonts-droid-fallback",
                "xinit",
                "nodm",
                "slim",
                "openbox",
                "xterm",
                "binutils",
                "feh",
                "sudo",
                "apt-file",
                "inotify-tools",
                "python3-pyelftools",
                "python3-dbus",
                "openssl",
                "ssl-cert",
                "mate-terminal"
        ]
        self.EXTRA_INCLUDES = ["apt-file", "dconf-gsettings-backend"]

class U16(Ubuntu):

    def __init__(self):
        super().__init__()
        self.BUILD_SUITE = "xenial"
        self.EXTRA_MIRRORS = ["deb {} xenial-updates {}".format(self.MIRRORS," ".join(self.COMPONENTS)), 
                               "deb {} xenial-backports {}".format(self.MIRRORS," ".join(self.COMPONENTS))]


class U18(Ubuntu):

    def __init__(self):
        super().__init__()
        self.BUILD_SUITE = "bionic"
        self.EXTRA_MIRRORS = ["deb {} bionic-updates {}".format(self.MIRRORS," ".join(self.COMPONENTS)), 
                               "deb {} bionic-backports {}".format(self.MIRRORS," ".join(self.COMPONENTS))]


class U20(Ubuntu):

    def __init__(self):
        super().__init__()
        self.BUILD_SUITE = "focal"
        self.EXTRA_MIRRORS = ["deb {} focal-updates {}".format(self.MIRRORS," ".join(self.COMPONENTS)), 
                               "deb {} focal-backports {}".format(self.MIRRORS," ".join(self.COMPONENTS))]

class U22(Ubuntu):

    def __init__(self):
        super().__init__()
        self.BUILD_SUITE = "jammy"
        self.EXTRA_MIRRORS = ["deb {} jammy-updates {}".format(self.MIRRORS," ".join(self.COMPONENTS)), 
                               "deb {} jammy-backports {}".format(self.MIRRORS," ".join(self.COMPONENTS))]


class U24(Ubuntu):

    def __init__(self):
        super().__init__()
        self.BUILD_SUITE = "noble"
        self.EXTRA_MIRRORS = ["deb {} noble-updates {}".format(self.MIRRORS," ".join(self.COMPONENTS)), 
                               "deb {} noble-backports {}".format(self.MIRRORS," ".join(self.COMPONENTS))]
        
class Shadow(BaseRelease):

    def __init__(self):
        self.MIRRORS = None
        self.config_file_path = "/etc/kare/base/shadow"

    def build(self, release_path):
        os.makedirs(release_path, exist_ok=True)        


if __name__ == "__main__":
    u24 = BaseRelease("u24")
    print(u24.release.BUILD_SUITE)
    print(u24.release.MIRRORS)
    print(u24.release.get_components)
    print(u24.release.get_build_includes)