import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:moonwell_launcher/app/home_screen/bloc/home_screen_bloc.dart';
import 'package:moonwell_launcher/app/home_screen/home_screen_content.dart';
import 'package:moonwell_launcher/app/login_screen/login_screen.dart';
import 'package:moonwell_launcher/app/theme/mw_theme.dart';
import 'package:moonwell_launcher/app/widgets/window_title_bar.dart';
import 'package:moonwell_launcher/features/launcher/application/client_sync_use_case.dart';
import 'package:moonwell_launcher/features/launcher/data/game_installation_service.dart';
import 'package:moonwell_launcher/features/launcher/data/launcher_api_client.dart';
import 'package:moonwell_launcher/features/launcher/domain/entities/client_manifest.dart';
import 'package:moonwell_launcher/features/launcher/domain/entities/launcher_session.dart';
import 'package:moonwell_launcher/features/preferences/domain/repositories/preferences_repository.dart';
import 'package:moonwell_launcher/service_container.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key, required this.session, required this.manifest});

  final LauncherSession session;
  final ClientManifest manifest;

  @override
  Widget build(BuildContext context) {
    return BlocProvider<HomeScreenBloc>(
      create: (context) => HomeScreenBloc(
        clientSyncUseCase: getIt<ClientSyncUseCase>(),
        gameInstallationService: getIt<GameInstallationService>(),
        launcherApiClient: getIt<LauncherApiClient>(),
        preferencesRepository: getIt<PreferencesRepository>(),
        session: session,
        manifest: manifest,
      ),
      child: BlocListener<HomeScreenBloc, HomeScreenState>(
        listenWhen: (previous, current) =>
            previous.model.isAuthenticated && !current.model.isAuthenticated,
        listener: (context, state) async {
          await Navigator.of(context).pushReplacement(
            PageRouteBuilder(
              pageBuilder: (_, _, _) => const LoginScreen(),
              transitionsBuilder: (_, animation, _, child) {
                return FadeTransition(opacity: animation, child: child);
              },
              transitionDuration: const Duration(milliseconds: 250),
            ),
          );
        },
        child: Scaffold(
          body: Stack(
            children: [
              Positioned.fill(
                child: Image.asset('assets/background.png', fit: BoxFit.cover),
              ),
              Positioned.fill(
                child: DecoratedBox(
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                      colors: [
                        MWColors.abyss.withAlpha(180),
                        MWColors.abyss.withAlpha(220),
                        MWColors.abyss.withAlpha(240),
                      ],
                      stops: const [0.0, 0.5, 1.0],
                    ),
                  ),
                ),
              ),
              const Positioned.fill(child: HomeScreenContent()),
              const Positioned(
                top: 0,
                left: 0,
                right: 0,
                child: WindowTitleBar(),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
