/// Flutter entry point for the GUI host application.
///
/// The GUI bootstraps bundled and discoverable game data through [Wolf3d]
/// before presenting the game-selection flow.
library;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:window_manager/window_manager.dart';
import 'package:wolf_3d_flutter/wolf_3d_flutter.dart';
import 'package:wolf_3d_gui/screens/game_screen.dart';

/// Creates the application shell after loading available Wolf3D data sets.
void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (_supportsDesktopWindowing) {
    await windowManager.ensureInitialized();
  }

  final Wolf3d wolf3d = await Wolf3d().init();

  runApp(
    MaterialApp(
      darkTheme: ThemeData.dark(useMaterial3: true),
      theme: ThemeData.light(useMaterial3: true),
      themeMode: ThemeMode.system,
      home: wolf3d.availableGames.isEmpty
          ? const _NoGameDataScreen()
          : GameScreen(wolf3d: wolf3d),
    ),
  );
}

/// Whether desktop window-management APIs should be initialized for this host.
bool get _supportsDesktopWindowing {
  if (kIsWeb) {
    return false;
  }

  return switch (defaultTargetPlatform) {
    TargetPlatform.linux ||
    TargetPlatform.windows ||
    TargetPlatform.macOS => true,
    _ => false,
  };
}

class _NoGameDataScreen extends StatelessWidget {
  const _NoGameDataScreen();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF140000),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: ConstrainedBox(
            constraints: const BoxConstraints(maxWidth: 640),
            child: DecoratedBox(
              decoration: BoxDecoration(
                color: const Color(0xFF590002),
                border: Border.all(color: const Color(0xFFB00000), width: 2),
              ),
              child: const Padding(
                padding: EdgeInsets.all(20),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'WOLF3D DATA NOT FOUND',
                      style: TextStyle(
                        color: Color(0xFFFFF700),
                        fontSize: 24,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 16),
                    Text(
                      'No game files were discovered.\n\n'
                      'Add Wolfenstein 3D data files to one of these locations:\n'
                      '- packages/wolf_3d_assets/assets/retail\n'
                      '- packages/wolf_3d_assets/assets/shareware\n'
                      '- or a discoverable local game-data folder.\n\n'
                      'Restart the app after adding the files.',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 15,
                        height: 1.4,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
