import platform
from tzlocal import get_localzone

# Windows-specific import
if platform.system() == "Windows":
    import winreg

# Comprehensive mapping of Windows registry timezone names to Python timezone names with IDs
TIMEZONE_MAPPING = {
    # Asia
    "India Standard Time": {"python_tz": "Asia/Kolkata", "id": 1},
    "Arab Standard Time": {"python_tz": "Asia/Riyadh", "id": 2},
    "China Standard Time": {"python_tz": "Asia/Shanghai", "id": 3},
    "Japan Standard Time": {"python_tz": "Asia/Tokyo", "id": 4},
    "Korea Standard Time": {"python_tz": "Asia/Seoul", "id": 5},
    "Singapore Standard Time": {"python_tz": "Asia/Singapore", "id": 6},
    "Malay Peninsula Standard Time": {"python_tz": "Asia/Kuala_Lumpur", "id": 7},
    "Bangkok Standard Time": {"python_tz": "Asia/Bangkok", "id": 8},
    "Hanoi Standard Time": {"python_tz": "Asia/Ho_Chi_Minh", "id": 9},
    "Jakarta Standard Time": {"python_tz": "Asia/Jakarta", "id": 10},
    "Manila Standard Time": {"python_tz": "Asia/Manila", "id": 11},
    "Taipei Standard Time": {"python_tz": "Asia/Taipei", "id": 12},
    "Hong Kong Standard Time": {"python_tz": "Asia/Hong_Kong", "id": 13},
    "Ulaanbaatar Standard Time": {"python_tz": "Asia/Ulaanbaatar", "id": 14},
    "Tashkent Standard Time": {"python_tz": "Asia/Tashkent", "id": 15},
    "Yekaterinburg Standard Time": {"python_tz": "Asia/Yekaterinburg", "id": 16},
    "Novosibirsk Standard Time": {"python_tz": "Asia/Novosibirsk", "id": 17},
    "Krasnoyarsk Standard Time": {"python_tz": "Asia/Krasnoyarsk", "id": 18},
    "Irkutsk Standard Time": {"python_tz": "Asia/Irkutsk", "id": 19},
    "Yakutsk Standard Time": {"python_tz": "Asia/Yakutsk", "id": 20},
    "Vladivostok Standard Time": {"python_tz": "Asia/Vladivostok", "id": 21},
    "Magadan Standard Time": {"python_tz": "Asia/Magadan", "id": 22},
    "Kamchatka Standard Time": {"python_tz": "Asia/Kamchatka", "id": 23},
    "Tehran Standard Time": {"python_tz": "Asia/Tehran", "id": 24},
    "Jerusalem Standard Time": {"python_tz": "Asia/Jerusalem", "id": 25},
    "Amman Standard Time": {"python_tz": "Asia/Amman", "id": 26},
    "Beirut Standard Time": {"python_tz": "Asia/Beirut", "id": 27},
    "Damascus Standard Time": {"python_tz": "Asia/Damascus", "id": 28},
    "Baghdad Standard Time": {"python_tz": "Asia/Baghdad", "id": 29},
    "Kuwait Standard Time": {"python_tz": "Asia/Kuwait", "id": 30},
    "Qatar Standard Time": {"python_tz": "Asia/Qatar", "id": 31},
    "Abu Dhabi Standard Time": {"python_tz": "Asia/Dubai", "id": 32},
    "Muscat Standard Time": {"python_tz": "Asia/Muscat", "id": 33},
    "Baku Standard Time": {"python_tz": "Asia/Baku", "id": 34},
    "Tbilisi Standard Time": {"python_tz": "Asia/Tbilisi", "id": 35},
    "Yerevan Standard Time": {"python_tz": "Asia/Yerevan", "id": 36},
    "Kabul Standard Time": {"python_tz": "Asia/Kabul", "id": 37},
    "Karachi Standard Time": {"python_tz": "Asia/Karachi", "id": 38},
    "Dhaka Standard Time": {"python_tz": "Asia/Dhaka", "id": 39},
    "Sri Lanka Standard Time": {"python_tz": "Asia/Colombo", "id": 40},
    "Nepal Standard Time": {"python_tz": "Asia/Kathmandu", "id": 41},
    "Myanmar Standard Time": {"python_tz": "Asia/Yangon", "id": 42},
    "Almaty Standard Time": {"python_tz": "Asia/Almaty", "id": 43},
    "Astana Standard Time": {"python_tz": "Asia/Almaty", "id": 44},  # Same as Almaty
    "Bishkek Standard Time": {"python_tz": "Asia/Bishkek", "id": 45},
    "Dushanbe Standard Time": {"python_tz": "Asia/Dushanbe", "id": 46},
    "Ashgabat Standard Time": {"python_tz": "Asia/Ashgabat", "id": 47},
    "Tashkent Standard Time": {"python_tz": "Asia/Tashkent", "id": 48},
    "Urumqi Standard Time": {"python_tz": "Asia/Urumqi", "id": 49},
    "Pyongyang Standard Time": {"python_tz": "Asia/Pyongyang", "id": 50},
    
    # Europe
    "GMT Standard Time": {"python_tz": "Europe/London", "id": 51},
    "Central Europe Standard Time": {"python_tz": "Europe/Budapest", "id": 52},
    "Central European Standard Time": {"python_tz": "Europe/Warsaw", "id": 53},
    "Romance Standard Time": {"python_tz": "Europe/Paris", "id": 54},
    "W. Europe Standard Time": {"python_tz": "Europe/Berlin", "id": 55},
    "E. Europe Standard Time": {"python_tz": "Europe/Bucharest", "id": 56},
    "FLE Standard Time": {"python_tz": "Europe/Helsinki", "id": 57},
    "GTB Standard Time": {"python_tz": "Europe/Athens", "id": 58},
    "Russian Standard Time": {"python_tz": "Europe/Moscow", "id": 59},
    "Turkey Standard Time": {"python_tz": "Europe/Istanbul", "id": 60},
    "Israel Standard Time": {"python_tz": "Asia/Jerusalem", "id": 61},
    "South Africa Standard Time": {"python_tz": "Africa/Johannesburg", "id": 62},
    "Namibia Standard Time": {"python_tz": "Africa/Windhoek", "id": 63},
    
    # Americas
    "Eastern Standard Time": {"python_tz": "America/New_York", "id": 64},
    "Central Standard Time": {"python_tz": "America/Chicago", "id": 65},
    "Mountain Standard Time": {"python_tz": "America/Denver", "id": 66},
    "Pacific Standard Time": {"python_tz": "America/Los_Angeles", "id": 67},
    "Alaskan Standard Time": {"python_tz": "America/Anchorage", "id": 68},
    "Hawaiian Standard Time": {"python_tz": "Pacific/Honolulu", "id": 69},
    "Atlantic Standard Time": {"python_tz": "America/Halifax", "id": 70},
    "Newfoundland Standard Time": {"python_tz": "America/St_Johns", "id": 71},
    "Central America Standard Time": {"python_tz": "America/Guatemala", "id": 72},
    "Eastern Standard Time (Mexico)": {"python_tz": "America/Cancun", "id": 73},
    "Central Standard Time (Mexico)": {"python_tz": "America/Mexico_City", "id": 74},
    "Mountain Standard Time (Mexico)": {"python_tz": "America/Chihuahua", "id": 75},
    "Pacific Standard Time (Mexico)": {"python_tz": "America/Tijuana", "id": 76},
    "SA Pacific Standard Time": {"python_tz": "America/Bogota", "id": 77},
    "Venezuela Standard Time": {"python_tz": "America/Caracas", "id": 78},
    "SA Western Standard Time": {"python_tz": "America/La_Paz", "id": 79},
    "Pacific SA Standard Time": {"python_tz": "America/Santiago", "id": 80},
    "SA Eastern Standard Time": {"python_tz": "America/Cayenne", "id": 81},
    "Argentina Standard Time": {"python_tz": "America/Argentina/Buenos_Aires", "id": 82},
    "Montevideo Standard Time": {"python_tz": "America/Montevideo", "id": 83},
    "Bahia Standard Time": {"python_tz": "America/Bahia", "id": 84},
    "E. South America Standard Time": {"python_tz": "America/Sao_Paulo", "id": 85},
    "Tocantins Standard Time": {"python_tz": "America/Araguaina", "id": 86},
    "Paraguay Standard Time": {"python_tz": "America/Asuncion", "id": 87},
    "Greenland Standard Time": {"python_tz": "America/Godthab", "id": 88},
    "Mid-Atlantic Standard Time": {"python_tz": "Atlantic/South_Georgia", "id": 89},
    "Azores Standard Time": {"python_tz": "Atlantic/Azores", "id": 90},
    "Cape Verde Standard Time": {"python_tz": "Atlantic/Cape_Verde", "id": 91},
    "Morocco Standard Time": {"python_tz": "Africa/Casablanca", "id": 92},
    "UTC": {"python_tz": "UTC", "id": 93},
    "UTC+12": {"python_tz": "Etc/GMT-12", "id": 94},
    "UTC+11": {"python_tz": "Etc/GMT-11", "id": 95},
    "UTC+10": {"python_tz": "Etc/GMT-10", "id": 96},
    "UTC+09": {"python_tz": "Etc/GMT-9", "id": 97},
    "UTC+08": {"python_tz": "Etc/GMT-8", "id": 98},
    "UTC+07": {"python_tz": "Etc/GMT-7", "id": 99},
    "UTC+06": {"python_tz": "Etc/GMT-6", "id": 100},
    "UTC+05": {"python_tz": "Etc/GMT-5", "id": 101},
    "UTC+04": {"python_tz": "Etc/GMT-4", "id": 102},
    "UTC+03": {"python_tz": "Etc/GMT-3", "id": 103},
    "UTC+02": {"python_tz": "Etc/GMT-2", "id": 104},
    "UTC+01": {"python_tz": "Etc/GMT-1", "id": 105},
    "UTC-01": {"python_tz": "Etc/GMT+1", "id": 106},
    "UTC-02": {"python_tz": "Etc/GMT+2", "id": 107},
    "UTC-03": {"python_tz": "Etc/GMT+3", "id": 108},
    "UTC-04": {"python_tz": "Etc/GMT+4", "id": 109},
    "UTC-05": {"python_tz": "Etc/GMT+5", "id": 110},
    "UTC-06": {"python_tz": "Etc/GMT+6", "id": 111},
    "UTC-07": {"python_tz": "Etc/GMT+7", "id": 112},
    "UTC-08": {"python_tz": "Etc/GMT+8", "id": 113},
    "UTC-09": {"python_tz": "Etc/GMT+9", "id": 114},
    "UTC-10": {"python_tz": "Etc/GMT+10", "id": 115},
    "UTC-11": {"python_tz": "Etc/GMT+11", "id": 116},
    "UTC-12": {"python_tz": "Etc/GMT+12", "id": 117},
}

def get_windows_timezone():
    """Get timezone name from Windows registry"""
    try:
        if platform.system() != "Windows":
            return None
            
        registry_path = r"SYSTEM\CurrentControlSet\Control\TimeZoneInformation"
        with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, registry_path) as key:
            tz_name, _ = winreg.QueryValueEx(key, "TimeZoneKeyName")
            return tz_name
    except FileNotFoundError:
        return None

def get_timezone_id(windows_tz_name=None, python_tz_name=None):
    """
    Get timezone ID based on Windows registry name or Python timezone name
    
    Args:
        windows_tz_name (str): Windows registry timezone name
        python_tz_name (str): Python timezone name from get_localzone()
    
    Returns:
        int: Timezone ID if found, None otherwise
    """
    if windows_tz_name:
        # Search by Windows registry name
        for win_tz, mapping in TIMEZONE_MAPPING.items():
            if win_tz == windows_tz_name:
                return mapping["id"]
    
    if python_tz_name:
        # Search by Python timezone name
        for win_tz, mapping in TIMEZONE_MAPPING.items():
            if mapping["python_tz"] == python_tz_name:
                return mapping["id"]
    
    return None

def get_timezone_info(windows_tz_name=None, python_tz_name=None):
    """
    Get complete timezone information including ID, Windows name, and Python name
    
    Args:
        windows_tz_name (str): Windows registry timezone name
        python_tz_name (str): Python timezone name from get_localzone()
    
    Returns:
        dict: Complete timezone information or None if not found
    """
    if windows_tz_name:
        # Search by Windows registry name
        for win_tz, mapping in TIMEZONE_MAPPING.items():
            if win_tz == windows_tz_name:
                return {
                    "id": mapping["id"],
                    "windows_name": win_tz,
                    "python_name": mapping["python_tz"]
                }
    
    if python_tz_name:
        # Search by Python timezone name
        for win_tz, mapping in TIMEZONE_MAPPING.items():
            if mapping["python_tz"] == python_tz_name:
                return {
                    "id": mapping["id"],
                    "windows_name": win_tz,
                    "python_name": mapping["python_tz"]
                }
    
    return None

def get_current_timezone_info():
    """
    Get current system timezone information using both Windows registry and Python
    
    Returns:
        dict: Current timezone information
    """
    windows_tz = get_windows_timezone()
    python_tz = str(get_localzone())
    
    # Try to get ID using Windows registry name
    tz_id = get_timezone_id(windows_tz_name=windows_tz)
    
    # If not found, try using Python timezone name
    if tz_id is None:
        tz_id = get_timezone_id(python_tz_name=python_tz)
    
    return {
        "windows_timezone": windows_tz,
        "python_timezone": python_tz,
        "timezone_id": tz_id,
        "mapped_info": get_timezone_info(windows_tz_name=windows_tz) or get_timezone_info(python_tz_name=python_tz)
    }