#!/usr/bin/env python3
# MicroPython script to test LED bar patterns over ESP-NOW (no WebSocket)

import json
import uasyncio as asyncio

# Import P2P from src/p2p.py
# Note: When running on device, ensure src/p2p.py is in the path
try:
    from p2p import P2P
except ImportError:
    # Fallback: import from src directory
    import sys
    sys.path.insert(0, 'src')
    from p2p import P2P

async def main():
    p2p = P2P()
    
    # Test cases following msg.json format:
    # {"g": {"df": {...}, "group_name": {...}}, "sv": true, "st": 0}
    # Note: led-bar device must have matching group in settings["groups"]
    tests = [
        # Example 1: Default format with df defaults and dj group (matches msg.json)
        {
            "g": { 
                "df": {
                    "pt": "on",
                    "cl": ["#ff0000"],
                    "br": 200,
                    "n1": 10,
                    "n2": 10,
                    "n3": 10,
                    "n4": 10,
                    "n5": 10,
                    "n6": 10,
                    "dl": 100
                },
                "dj": {
                    "pt": "blink",
                    "cl": ["#00ff00"],
                    "dl": 500
                }
            },
            "sv": True,
            "st": 0
        },
        # Example 2: Different group with df defaults
        {
            "g": {
                "df": {
                    "pt": "on",
                    "br": 150,
                    "dl": 100
                },
                "group1": {
                    "pt": "rainbow",
                    "dl": 50
                }
            },
            "sv": False
        },
        # Example 3: Multiple groups
        {
            "g": {
                "df": {
                    "br": 200,
                    "dl": 100
                },
                "group1": {
                    "pt": "on",
                    "cl": ["#0000ff"]
                },
                "group2": {
                    "pt": "blink",
                    "cl": ["#ff00ff"],
                    "dl": 300
                }
            },
            "sv": True,
            "st": 1
        },
        # Example 4: Single group without df
        {
            "g": {
                "dj": {
                    "pt": "off"
                }
            },
            "sv": False
        }
    ]

    for i, test in enumerate(tests, 1):
        print(f"\n{'='*50}")
        print(f"Test {i}/{len(tests)}")
        print(f"Sending: {json.dumps(test, indent=2)}")
        await p2p.send(json.dumps(test))
        await asyncio.sleep_ms(2000)
    
    print(f"\n{'='*50}")
    print("All tests completed")

if __name__ == "__main__":
    asyncio.run(main())