import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/bloc/app_bloc.dart';
import 'package:hl_lieferservice/feature/authentication/bloc/auth_bloc.dart';
import 'package:hl_lieferservice/feature/authentication/presentation/login_enforcer.dart';
import 'package:hl_lieferservice/feature/authentication/service/userinfo.dart';
import 'package:hl_lieferservice/feature/cars/presentation/car_management_page.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/delivery_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/note_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/detail/repository/delivery_repository.dart';
import 'package:hl_lieferservice/feature/delivery/detail/repository/note_repository.dart';
import 'package:hl_lieferservice/feature/delivery/detail/service/notes_service.dart';
import 'package:hl_lieferservice/feature/delivery/overview/bloc/tour_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/overview/repository/tour_repository.dart';
import 'package:hl_lieferservice/feature/settings/bloc/settings_bloc.dart';
import 'package:hl_lieferservice/widget/home/bloc/navigation_bloc.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';
import 'package:hl_lieferservice/widget/operations/presentation/operation_view_enforcer.dart';

import 'package:hl_lieferservice/bloc/app_states.dart';
import '../feature/delivery/overview/service/delivery_info_service.dart';
import 'home/presentation/home.dart';

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

  @override
  State<StatefulWidget> createState() => _DeliveryAppState();
}

class _DeliveryAppState extends State<DeliveryApp> {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AppBloc, AppState>(
      builder: (context, state) {
        if (state is AppConfigLoaded) {
          final currentAppState = state;
          return MultiBlocProvider(
            providers: [
              BlocProvider(create: (context) => NavigationBloc()),
              BlocProvider(create: (context) => OperationBloc()),
              BlocProvider(
                create:
                    (context) => AuthBloc(
                      service: UserInfoService(
                        url: currentAppState.config.backendUrl,
                      ),
                      operationBloc: context.read<OperationBloc>(),
                    ),
              ),
              BlocProvider(
                create:
                    (context) => TourBloc(
                      opBloc: context.read<OperationBloc>(),
                      tourRepository: TourRepository(
                        service: DeliveryInfoService(
                          config: currentAppState.config,
                        ),
                      ),
                    ),
              ),
              BlocProvider(
                create:
                    (context) => NoteBloc(
                      opBloc: context.read<OperationBloc>(),
                      repository: NoteRepository(
                        service: NoteService(config: currentAppState.config),
                      ),
                    ),
              ),
              BlocProvider(
                create:
                    (context) => DeliveryBloc(
                      opBloc: context.read<OperationBloc>(),
                      noteRepository: NoteRepository(
                        service: NoteService(config: currentAppState.config),
                      ),
                      repository: DeliveryRepository(
                        service: DeliveryInfoService(
                          config: currentAppState.config,
                        ),
                      ),
                    ),
              ),
            ],
            child: MaterialApp(
              home: OperationViewEnforcer(
                child: BlocBuilder<AppBloc, AppState>(
                  builder: (context, state) {
                    if (state is AppConfigLoading) {
                      return Scaffold(
                        body: Center(child: CircularProgressIndicator()),
                      );
                    }

                    if (state is AppConfigLoadingFailed) {
                      return Scaffold(body: Center(child: Text(state.message)));
                    }

                    if (state is AppConfigLoaded) {
                      return LoginEnforcer(child: Home());
                    }

                    return Container();
                  },
                ),
              ),
              routes: {"/cars": (context) => CarManagementPage()},
            ),
          );
        }

        if (state is AppConfigLoadingFailed) {
          return MaterialApp(
            home: Scaffold(
              body: Center(child: Text("Fehler beim Laden der Konfiguration")),
            ),
          );
        }

        return MaterialApp(
          home: Scaffold(
            body: Center(child: const CircularProgressIndicator()),
          ),
        );
      },
    );
  }
}
