import requests
import urllib3
import struct

urllib3.disable_warnings()

def hex_to_dec(hex_str):
    try:
        # MTR używa 64-bitowych liczb całkowitych ze znakiem (signed long)
        return struct.unpack('>q', bytes.fromhex(hex_str.rjust(16, '0')))[0]
    except Exception:
        return 0

def main():
    print("=== SZPIEG ODJAZDÓW MTR ===")
    
    # ID Piramidy Centralnej ze screena
    station_id_hex = "0824141AF14C93C0"
    station_id_dec = hex_to_dec(station_id_hex)
    
    print(f"[*] Stacja docelowa: Piramida Centralna")
    print(f"[*] Przechwycone ID (HEX): {station_id_hex}")
    print(f"[*] Przeliczone ID (DEC): {station_id_dec}\n")
    
    # Test 1: Stare API (Departures) z kluczem HEX
    print("--- TEST 1: Stare API /departures (Payload HEX) ---")
    try:
        r1 = requests.post("https://mtr.ciapongi.szablix.pl/mtr/api/map/departures", json={station_id_hex: 0}, verify=False, timeout=5)
        d1 = r1.json()
        deps1 = d1.get('departures', [])
        print(f"Status HTTP: {r1.status_code}")
        print(f"Ilość zwróconych grup tras: {len(deps1)}")
        if deps1:
            print(f"SUKCES! Przykładowe Route ID z odpowiedzi: {deps1[0].get('id')} (Typ: {type(deps1[0].get('id')).__name__})")
        else:
            print("PORAŻKA: Pusta lista (Serwer zignorował zapytanie HEX)")
    except Exception as e:
        print(f"Błąd Testu 1: {e}")
        
    # Test 2: Stare API (Departures) z kluczem DEC
    print("\n--- TEST 2: Stare API /departures (Payload DEC) ---")
    try:
        r2 = requests.post("https://mtr.ciapongi.szablix.pl/mtr/api/map/departures", json={str(station_id_dec): 0}, verify=False, timeout=5)
        d2 = r2.json()
        deps2 = d2.get('departures', [])
        print(f"Status HTTP: {r2.status_code}")
        print(f"Ilość zwróconych grup tras: {len(deps2)}")
        if deps2:
            print(f"SUKCES! Przykładowe Route ID z odpowiedzi: {deps2[0].get('id')} (Typ: {type(deps2[0].get('id')).__name__})")
        else:
            print("PORAŻKA: Pusta lista")
    except Exception as e:
        print(f"Błąd Testu 2: {e}")

    # Test 3: Nowe API (Arrivals) z stationIdsHex
    print("\n--- TEST 3: Nowe API /arrivals (stationIdsHex) ---")
    try:
        r3 = requests.post("https://mtr.ciapongi.szablix.pl/mtr/api/map/arrivals?dimension=0", json={"stationIdsHex": [station_id_hex], "maxCountPerPlatform": 5}, verify=False, timeout=5)
        d3 = r3.json()
        arrs = d3.get('data', {}).get('arrivals', [])
        print(f"Status HTTP: {r3.status_code}")
        print(f"Ilość zwróconych odjazdów pociągów: {len(arrs)}")
        if arrs:
            print(f"SUKCES! Przykładowy pociąg: Linia {arrs[0].get('routeNumber')} -> {arrs[0].get('destination')}")
            print(f"Zwracane Route ID przez ten endpoint to: {arrs[0].get('routeId')} (Typ: {type(arrs[0].get('routeId')).__name__})")
        else:
            print("PORAŻKA: Pusta lista")
    except Exception as e:
        print(f"Błąd Testu 3: {e}")

if __name__ == "__main__":
    main()
