import 'package:app_gaslieferung/bloc/authentication/auth_bloc.dart';
import 'package:app_gaslieferung/bloc/authentication/auth_event.dart';
import 'package:app_gaslieferung/bloc/authentication/auth_state.dart';
import 'package:app_gaslieferung/bloc/message_wrapper/message_bloc.dart';
import 'package:app_gaslieferung/bloc/message_wrapper/message_event.dart';
import 'package:app_gaslieferung/exceptions/login.dart';
import 'package:app_links/app_links.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../bloc/tour_select/bloc.dart';
import '../../repository/tour_repository.dart';
import '../../service/tour_service.dart';
import '../../util/login.dart';
import 'tour_select.dart';

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

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

class _LoginPageState extends State<LoginPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: BlocConsumer<AuthBloc, AuthState>(
        builder: (context, state) {
          return Center(
            child: Padding(
              padding: EdgeInsets.only(
                top: MediaQuery.of(context).size.height * 0.1,
              ),
              child: Column(
                children: [
                  Image.asset(
                    "assets/graphics/bg-supplier-clouds.png",
                    fit: BoxFit.contain,
                  ),
                  Text(
                    "Willkommen bei\nGaslieferung!",
                    style: Theme.of(context).textTheme.headlineMedium,
                    textAlign: TextAlign.center,
                  ),

                  Text(
                    "\nMelden Sie sich an, um Ihre Tour zu starten.",
                    style: Theme.of(context).textTheme.bodySmall,
                    textAlign: TextAlign.center,
                  ),

                  Padding(
                    padding: const EdgeInsets.all(50),
                    child: FilledButton(
                      onPressed: () {
                        context.read<AuthBloc>().add(AuthLoginEvent());
                      },
                      child: Text("Einloggen"),
                    ),
                  ),
                ],
              ),
            ),
          );
        },
        listener: (context, state) {
          if (state is AuthenticatedState) {
            Navigator.pushReplacement(
              context,
              MaterialPageRoute(
                builder: (context) => BlocProvider(
                  create: (context) => TourSelectBloc(
                    repository: TourRepository(
                      service: TourService(
                        baseUrl: "http://127.0.0.1:3000",
                      ),
                    ),
                  ),
                  child: TourSelectPage(),
                ),
              ),
            );
          }
        },
      ),
    );
  }
}
