#!/usr/bin/env node
/**
 * LLUAV SDK Demo: Camera & Map Management (Node.js)
 *
 * Demonstrates:
 *   - Capture single photo
 *   - SIYI gimbal control
 *   - Toggle recording
 *   - List and save maps
 *
 * Usage:
 *   node demo_camera_map.js [host] [port] [password] [mapName]
 */

const { LLUAVClient, LLUAVCommandError } = require('../src/client');

async function main() {
    const host = process.argv[2] || 'localhost';
    const port = parseInt(process.argv[3] || '53001', 10);
    const password = process.argv[4] || null;
    const mapName = process.argv[5] || '';

    const client = new LLUAVClient(host, port, password, 10000, {
        autoReconnect: true,
        heartbeatInterval: 5000,
    });

    try {
        console.log('==================================================');
        console.log('LLUAV Camera & Map Demo (Node.js)');
        console.log('==================================================');

        console.log('\n[1/6] Connecting...');
        await client.connect();
        console.log(`      Connected to ${client.url}`);

        // List maps
        console.log('\n[2/6] Listing saved maps...');
        try {
            const maps = await client.listMaps();
            const arr = maps.maps || [];
            console.log(`      Found ${arr.length} map(s):`);
            arr.forEach(m => console.log(`        - ${m}`));
        } catch (e) {
            console.log(`      Error: ${e.message}`);
        }

        // Capture
        console.log('\n[3/6] Capturing photo...');
        try {
            const resp = await client.capture();
            console.log(`      Capture OK: ret=${resp.ret ?? '?'}`);
        } catch (e) {
            console.log(`      Error: ${e.message}`);
        }

        // SIYI center
        console.log('\n[4/6] Centering SIYI gimbal...');
        try {
            const resp = await client.siyiCenter();
            console.log(`      Center OK: ret=${resp.ret ?? '?'}`);
        } catch (e) {
            console.log(`      Error: ${e.message}`);
        }

        // Toggle record
        console.log('\n[5/6] Toggling video recording...');
        try {
            const resp = await client.recordToggle();
            console.log(`      Record toggle OK: ret=${resp.ret ?? '?'}`);
        } catch (e) {
            console.log(`      Error: ${e.message}`);
        }

        // Save map
        if (mapName) {
            console.log(`\n[6/6] Saving map to '${mapName}'...`);
            try {
                const resp = await client.saveMap(mapName);
                console.log(`      Save OK: ret=${resp.ret ?? '?'}`);
                if (resp.path) console.log(`      Path: ${resp.path}`);
            } catch (e) {
                console.log(`      Error: ${e.message}`);
            }
        } else {
            console.log('\n[6/6] Skipping map save (pass map name as 5th arg)');
        }

        console.log('\n==================================================');
        console.log('Demo completed.');
        console.log('==================================================');

        await client.disconnect();
    } catch (e) {
        if (e instanceof LLUAVCommandError) {
            console.error(`Command failed: ${e.message} (ret=${e.retCode})`);
            console.error('Response:', JSON.stringify(e.response, null, 2));
        } else {
            console.error('Error:', e.message);
        }
        process.exit(1);
    }
}

main();
