/*
 * Hunt Feature Usage Examples
 * 
 * This file demonstrates how to use the Hunt feature components
 * and integrate them into your app.
 */

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:lba/screens/mains/hunt/hunt.dart';
import 'package:lba/screens/mains/hunt/providers/hunt_provider.dart';
import 'package:lba/screens/mains/hunt/models/hunt_card.dart';
import 'package:lba/screens/mains/hunt/widgets/hunt_card_widget.dart';
import 'package:lba/screens/mains/hunt/widgets/leaderboard_widget.dart';

// Example 1: Basic Hunt Screen Integration
class ExampleHuntIntegration extends StatelessWidget {
  const ExampleHuntIntegration({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => HuntState()..initializeGame(),
      child: const Hunt(),
    );
  }
}

// Example 2: Custom Hunt Card Widget Usage
class ExampleHuntCard extends StatelessWidget {
  const ExampleHuntCard({super.key});

  @override
  Widget build(BuildContext context) {
    final exampleCard = HuntCard(
      id: 'example_1',
      category: 'Coffee Shop',
      categoryIcon: '☕',
      points: 150,
      question: 'Where coffee meets books in perfect harmony...',
      answer: 'Literary Café',
      description: 'A cozy bookstore café',
      targetLatitude: 32.62501010252744,
      targetLongitude: 51.72622026956878,
      hintLatitude: 32.62501010252744,
      hintLongitude: 51.72622026956878,
      hintDescription: 'Look for the AR marker near the entrance',
    );

    return Scaffold(
      body: Center(
        child: HuntCardWidget(
          card: exampleCard,
          onTap: () {
            // Handle card selection
            print('Card ${exampleCard.id} selected!');
          },
          isSelected: false,
          isFlipped: false,
        ),
      ),
    );
  }
}

// Example 3: Leaderboard Widget Usage
class ExampleLeaderboard extends StatelessWidget {
  const ExampleLeaderboard({super.key});

  @override
  Widget build(BuildContext context) {
    final mockLeaderboard = [
      const LeaderboardEntry(
        id: '1',
        name: 'John Doe',
        avatar: '🏆',
        totalPoints: 850,
        rank: 1,
      ),
      const LeaderboardEntry(
        id: '2',
        name: 'Jane Smith',
        avatar: '🥈',
        totalPoints: 720,
        rank: 2,
      ),
      const LeaderboardEntry(
        id: 'current',
        name: 'You',
        avatar: '👤',
        totalPoints: 450,
        rank: 3,
        isCurrentUser: true,
      ),
    ];

    return Scaffold(
      body: LeaderboardWidget(
        entries: mockLeaderboard,
        userPoints: 450,
        onClose: () {
          Navigator.of(context).pop();
        },
      ),
    );
  }
}

// Example 4: Custom Hunt State Management
class ExampleHuntStateUsage extends StatefulWidget {
  const ExampleHuntStateUsage({super.key});

  @override
  State<ExampleHuntStateUsage> createState() => _ExampleHuntStateUsageState();
}

class _ExampleHuntStateUsageState extends State<ExampleHuntStateUsage> {
  late HuntState huntState;

  @override
  void initState() {
    super.initState();
    huntState = HuntState();
    huntState.initializeGame();
  }

  @override
  void dispose() {
    huntState.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: huntState,
      child: Scaffold(
        appBar: AppBar(title: const Text('Hunt State Example')),
        body: Consumer<HuntState>(
          builder: (context, state, child) {
            return Column(
              children: [
                // Display current game state
                Text('Current State: ${state.gameState}'),
                Text('User Points: ${state.userPoints}'),
                Text('Cards Available: ${state.cards.length}'),
                
                // Control buttons
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    ElevatedButton(
                      onPressed: state.cards.isEmpty 
                        ? null 
                        : () => state.selectCard(state.cards.first),
                      child: const Text('Select First Card'),
                    ),
                    ElevatedButton(
                      onPressed: state.gameState == HuntGameState.cardFlipped 
                        ? () => state.startHunt()
                        : null,
                      child: const Text('Start Hunt'),
                    ),
                    ElevatedButton(
                      onPressed: state.gameState == HuntGameState.huntingActive 
                        ? () => state.completeHunt()
                        : null,
                      child: const Text('Complete Hunt'),
                    ),
                  ],
                ),
                
                // Display selected card info
                if (state.selectedCard != null) ...[
                  const SizedBox(height: 20),
                  Text('Selected Card: ${state.selectedCard!.category}'),
                  Text('Points: ${state.selectedCard!.points}'),
                  Text('Question: ${state.selectedCard!.question}'),
                ],
                
                // Time remaining display
                if (state.gameState == HuntGameState.huntingActive) ...[
                  const SizedBox(height: 20),
                  Text('Time Remaining: ${_formatDuration(state.timeRemaining)}'),
                ],
              ],
            );
          },
        ),
      ),
    );
  }

  String _formatDuration(Duration duration) {
    final hours = duration.inHours;
    final minutes = duration.inMinutes % 60;
    final seconds = duration.inSeconds % 60;
    return '${hours.toString().padLeft(2, '0')}:'
           '${minutes.toString().padLeft(2, '0')}:'
           '${seconds.toString().padLeft(2, '0')}';
  }
}

// Example 5: Custom Hunt Integration with Navigation
class ExampleHuntNavigation extends StatelessWidget {
  const ExampleHuntNavigation({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Hunt Navigation Example')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => ChangeNotifierProvider(
                      create: (_) => HuntState()..initializeGame(),
                      child: const Hunt(),
                    ),
                  ),
                );
              },
              child: const Text('Start Treasure Hunt'),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => const ExampleLeaderboard(),
                  ),
                );
              },
              child: const Text('View Leaderboard'),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => const ExampleHuntCard(),
                  ),
                );
              },
              child: const Text('Preview Hunt Card'),
            ),
          ],
        ),
      ),
    );
  }
}

/*
 * Integration Notes:
 * 
 * 1. Always wrap Hunt components with ChangeNotifierProvider<HuntState>
 * 2. Initialize the game state with huntState.initializeGame()
 * 3. Handle permissions properly before starting hunts
 * 4. Clean up resources in dispose methods
 * 5. Use Consumer widgets to react to state changes
 * 6. Test on physical devices for location and camera features
 */
