import os
from common.utils import HostMessage

class Environment(HostMessage):

    def __init__(self, release):
        self.host_envs_list = [
            "PATH",
            "DISPLAY",
            "WAYLAND_DISPLAY",
            "LANG",
            "LANGUAGE",
            "SHELL",
            "TEMP",
            "TEMPDIR",
            "PWD",
            "EMAIL_ADDRESS",
            "HOME",
            "HOSTNAME",
            "LOGNAME",
            "REAL_NAME",
            "TERM",
            "USER",
            "USERNAME",
            "XAUTHORITY",
            "XDG_SESSION_DESKTOP",
            "D_DISABLE_RT_SCREEN_SCALE",
            "XMODIFIERS",
            "DESKTOP_SESSION",
            "DEEPIN_WINE_SCALE",
            "XDG_CURRENT_DESKTOP",
            "XDG_DATA_HOME",
            "XIM",
            "XDG_SESSION_TYPE",
            "XDG_RUNTIME_DIR",
            "CLUTTER_IM_MODULE",
            "QT4_IM_MODULE",
            "GTK_IM_MODULE",
            "auto_proxy",
            "http_proxy",
            "https_proxy",
            "ftp_proxy",
            "SOCKS_SERVER",
            "no_proxy",
            "QT_IM_MODULE",
            "QT_QPA_PLATFORM",
            "WAYLAND_DISPLAY",
            "QT_QPA_PLATFORM",
            "QT_WAYLAND_SHELL_INTEGRATION",
            "GDMSESSION",
            "QT_WAYLAND_FORCE_DPI",
            "GIO_LAUNCHED_DESKTOP_FILE",
            "GNOME_DESKTOP_SESSION_ID",
            "PYTHONPATH",
            "PERLLIB",
            "PERL5LIB",
            "XCURSOR_PATH",
            "GST_PLUGIN_PATH_1_0",
            "GST_REGISTRY",
            "GST_REGISTRY_1_0",
            "GST_PLUGIN_PATH",
            "GST_PLUGIN_SYSTEM_PATH",
            "GST_PLUGIN_SCANNER",
            "GST_PLUGIN_SCANNER_1_0",
            "GST_PLUGIN_SYSTEM_PATH_1_0",
            "GST_PRESET_PATH",
            "GST_PTP_HELPER",
            "GST_PTP_HELPER_1_0",
            "GST_INSTALL_PLUGINS_HELPER",
            "KRB5CCNAME",
            "XKB_CONFIG_ROOT",
            "GIO_EXTRA_MODULES",
            "GDK_BACKEND",
            # "SUDO_COMMAND",
            "SUDO_USER",
            "SUDO_UID",
            "SUDO_GID",
            "GSETTINGS_BACKEND",
            "DBUS_SESSION_BUS_ADDRESS",
            "XDG_DATA_DIRS",
            "SESSION_MANAGER"
        ]

        self.container_envs = [
            "QT_X11_NO_MITSHM",
            "XDG_DATA_DIRS",
            "browser_agent_env",
            "PANSHI_APP_DESKTOP",
            "BROWSER",
            "DBUS_SYSTEM_BUS_ADDRESS",
            "PULSE_CLIENTCONFIG",
            "PULSE_SERVER",
            "IMMU",
            "PANSHI_APP_DESKTOP"
        ]

        self.envs = self.get_host_environment
        self.release = release
        self._update_environment

    """
    :brief 获取主机环境变量
    :return: 环境变量字典
    """
    @property
    def get_host_environment(self) -> dict:
        envs = {}
        for host_env in self.host_envs_list:
            value = os.getenv(host_env)
            if value:
                envs[host_env] = value
        for env_key,env_value in dict(os.environ).items():
            envs[env_key]=env_value
        return envs

    @property
    def _update_environment(self) -> str:
        # 设置需特殊处理的环境变量
        self.envs.setdefault("IMMU", "true" if self.is_ostree else "false")
        self.envs.setdefault("QT_X11_NO_MITSHM", "1")
        self.envs.setdefault("browser_agent_env", "true")
        self.envs.setdefault("BROWSER", "qaxbrowser-safe-stable")
        if self.envs.get("PATH"):
            self.envs["PATH"] = self.envs.get("PATH") + ":/usr/games:/bin"
        else:
            self.envs["PATH"] = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
        if self.envs.get("XDG_DATA_DIRS"):
            self.envs["XDG_DATA_DIRS"] = self.envs.get("XDG_DATA_DIRS") + ":/run/host/share:/run/host/local-share:/run/host/user-share:/run/host/usr/share/ukui:/run/host/usr/local/share:/run/host/usr/share"
        else:
            self.envs["XDG_DATA_DIRS"] = "/usr/share/ukui:/usr/local/share:/usr/share:/run/host/share:/run/host/local-share:/run/host/user-share:/run/host/usr/share/ukui:/run/host/usr/local/share:/run/host/usr/share"
        if self.envs.get("XDG_CONFIG_DIRS", None):
            self.envs["XDG_CONFIG_DIRS"] = self.envs.get("XDG_CONFIG_DIRS") + "/run/host/etc/xdg/xdg-ukui:/run/host/etc/xdg"
        else:
            self.envs["XDG_CONFIG_DIRS"] = "/etc/xdg/xdg-ukui:/etc/xdg:/run/host/etc/xdg/xdg-ukui:/run/host/etc/xdg"
        if self.release == "v10sp1":
            self.envs["XDG_SESSION_TYPE"] = "x11"
            self.envs["WAYLAND_DISPLAY"] = ""
            self.envs.pop("DESKTOP_SESSION", None)
            self.envs.pop("XDG_SESSION_DESKTOP", None)
        # self.envs.setdefault("PANSHI_APP_DESKTOP", "/usr/share/applications")
        self.envs.setdefault("KARE_APP", "True")
        self.envs.setdefault("TZ", self.run_command("cat /etc/timezone"))

    
if __name__ == "__main__":
    env = Environment()
    env.get_host_environment()