#!/usr/bin/env python3
"""
Simple test of the JSON-like API for Wasot.
"""

import sys
import os
sys.path.insert(0, os.path.join(os.getcwd(), 'python'))

import pywasot
import json


def example1():
    """Basic example - like working with JSON."""
    print("Example 1: Basic Usage")
    print("-" * 40)
    
    # Create a simple document structure
    doc = {
        '_type': 'block',
        'name': 'article',
        'kwargs': {'title': 'Hello Wasot', 'author': 'Python Dev'},
        'children': [
            'This is the introduction paragraph.',
            {
                '_type': 'block',
                'name': 'section',
                'kwargs': {'id': 'main'},
                'children': [
                    'Main content here.',
                    {
                        '_type': 'block',
                        'name': 'code',
                        'kwargs': {'lang': 'python'},
                        'children': ['print("Hello, World!")']
                    }
                ]
            },
            'Conclusion paragraph.'
        ]
    }
    
    # Convert to Wasot string (like json.dumps)
    wasot_text = pywasot.dumps(doc)
    print("Generated Wasot:")
    print(wasot_text)
    
    # Parse it back (like json.loads)
    parsed = pywasot.loads(wasot_text)
    print("\nParsed back to Python:")
    print(json.dumps(parsed, indent=2))
    
    return wasot_text


def example2():
    """File I/O example."""
    print("\nExample 2: File I/O")
    print("-" * 40)
    
    # Sample Wasot content
    wasot_content = """.document
.heading level=1
Welcome to Wasot
./

.paragraph
Wasot is now as easy to use as JSON in Python.
./

.list type=ordered
..item
First item
../
..item  
Second item
../
./
./"""
    
    # Save to file
    import tempfile
    with tempfile.NamedTemporaryFile(mode='w', suffix='.wasot', delete=False) as f:
        f.write(wasot_content)
        temp_path = f.name
    
    print(f"Saved to: {temp_path}")
    
    # Load from file (just like json.load)
    data = pywasot.load_file(temp_path)
    print("Loaded data structure:")
    print(json.dumps(data, indent=2))
    
    # Clean up
    os.unlink(temp_path)
    
    return data


def example3():
    """Working with simple text."""
    print("\nExample 3: Simple Text")
    print("-" * 40)
    
    # Parse simple text content
    text = "Just some plain text content."
    result = pywasot.loads(text)
    print(f"Input: {text}")
    print(f"Parsed: {result}")
    
    # Parse a single block
    single = pywasot.loads(".note\nRemember to use Wasot!")
    print(f"\nSingle block: {json.dumps(single, indent=2)}")
    
    # Convert back
    reconstructed = pywasot.dumps(single)
    print(f"Back to Wasot:\n{reconstructed}")


def main():
    print("=" * 50)
    print("PyWasot JSON-like API Examples")
    print("=" * 50)
    print("\nJust like the json module:")
    print("  import pywasot")
    print("  data = pywasot.loads(text)      # Parse from string")
    print("  text = pywasot.dumps(data)      # Convert to string")
    print("  data = pywasot.load(file)       # Parse from file")
    print("  pywasot.dump(data, file)        # Write to file")
    print("\n")
    
    example1()
    example2()
    example3()
    
    print("\n" + "=" * 50)
    print("✓ Wasot is now as easy as JSON for Python devs!")
    print("=" * 50)


if __name__ == "__main__":
    main()