import httpx
import logging

logger = logging.getLogger(__name__)

BD_VERSION_URL = (
    "https://bdversion.ggbluefox.com/live/ver.php"
    "?version=1.120.1"
    "&lang=ar"
    "&device=android"
    "&channel=android"
    "&appstore=googleplay"
    "&region=ME"
    "&whitelist_version=1.3.0"
    "&whitelist_sp_version=1.0.0"
    "&device_name=google%20G011A"
    "&device_CPU=ARMv7%20VFPv3%20NEON%20VMH"
    "&device_GPU=Adreno%20(TM)%20640"
    "&device_mem=1993"
)

class OBVersionManager:

    def __init__(self):
        self.raw_data = None
        self.ob_version = None
        self.server_url = None
        self.gop_url = None

    async def fetch(self):
        try:
            async with httpx.AsyncClient(timeout=15) as client:
                response = await client.get(BD_VERSION_URL)

            response.raise_for_status()
            data = response.json()

            if data.get("code") != 0:
                logger.error("Invalid response code from server")
                return False

            self.raw_data = data
            self.ob_version = data.get("latest_release_version")
            self.server_url = data.get("server_url")
            self.gop_url = data.get("gop_url")

            logger.info(f"OB VERSION LOADED => {self.ob_version}")
            logger.info(f"LOGIN SERVER => {self.server_url}")

            return True

        except Exception as e:
            logger.error(f"OB Fetch Failed: {e}")
            return False


    def get_ob(self):
        return self.ob_version

    def get_login_server(self):
        return self.server_url

    def get_gop_servers(self):
        if self.gop_url:
            return self.gop_url.split(";")
        return []