// main.dart
// Einstiegspunkt der App und globale Konfigurationen
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:trainerbox/firebase_options.dart';
import 'screens/home_screen.dart';
import 'screens/login_screen.dart';
import 'screens/search_tab.dart';
import 'package:intl/date_symbol_data_local.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  await initializeDateFormatting('de_DE', null);
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _loggedIn = false;

  void _handleLoginSuccess() {
    setState(() {
      _loggedIn = true;
    });
  }

  void _handleLogoutSuccess() {
    setState(() {
      _loggedIn = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'TrainerBox',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: _loggedIn
          ? HomeScreen(onLogoutSuccess: _handleLogoutSuccess)
          : LoginScreen(onLoginSuccess: _handleLoginSuccess),
      routes: {
        '/search': (context) => SearchTab(
          selectMode: (ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?)?['selectMode'] ?? false,
          remainingTime: (ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?)?['remainingTime'] as int?,
        ),
      },
    );
  }
}
