import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:unit2/bloc/profile/primary_information/contact/contact_bloc.dart';
import 'package:unit2/bloc/profile/profile_bloc.dart';
import 'package:unit2/bloc/user/user_bloc.dart';
import 'package:unit2/theme-data.dart/box_shadow.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/Leadings/add_leading.dart';
import 'package:unit2/widgets/empty_data.dart';

class ContactInformationScreen extends StatelessWidget {
  const ContactInformationScreen({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          appBar: AppBar(
            title: const Text(contactScreenTitle),
            centerTitle: true,
            backgroundColor: primary,
            actions: [AddLeading(onPressed: () {})],
          ),
          body: ProgressHUD(
            padding: const EdgeInsets.all(24),
            backgroundColor: Colors.black87,
            indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
            child: BlocBuilder<UserBloc, UserState>(
              builder: (context, state) {
                if (state is UserLoggedIn) {
                  return BlocBuilder<ProfileBloc, ProfileState>(
                    builder: (context, state) {
                      if (state is ProfileLoaded) {
                        return BlocConsumer<ContactBloc, ContactState>(
                          listener: (context, state) {
                            if (state is ContactLoadingState) {
                              final progress = ProgressHUD.of(context);
                              progress!.showWithText("Please wait...");
                            }
                            if (state is ContactLoadedState ||
                                state is ContactErrorState) {
                              final progress = ProgressHUD.of(context);
                              progress!.dismiss();
                            }
                          },
                          builder: (context, state) {
                            if (state is ContactLoadedState) {
                              if (state.contactInformation.isNotEmpty) {
                                return ListView.builder(
                                    padding: const EdgeInsets.symmetric(
                                        vertical: 8, horizontal: 10),
                                    itemCount: state.contactInformation.length,
                                    itemBuilder:
                                        (BuildContext context, int index) {
                                      String numberMail = state
                                          .contactInformation[index]
                                          .numbermail!;
                                      String commService = state
                                          .contactInformation[index]
                                          .commService!
                                          .serviceProvider!
                                          .alias!;
                                      return Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.start,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Container(
                                            decoration: box1(),
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 12, vertical: 8),
                                            child: Row(
                                              children: [
                                                Expanded(
                                                  child: Column(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .start,
                                                      crossAxisAlignment:
                                                          CrossAxisAlignment
                                                              .start,
                                                      children: [
                                                        Text(numberMail,
                                                            style: Theme.of(
                                                                    context)
                                                                .textTheme
                                                                .titleMedium!
                                                                .copyWith(
                                                                    fontWeight:
                                                                        FontWeight
                                                                            .w500)),
                                                        const Divider(),
                                                        const SizedBox(
                                                          height: 5,
                                                        ),
                                                        Row(
                                                          children: [
                                                            Expanded(
                                                              child: Text(
                                                                commService
                                                                    .toString(),
                                                                style: Theme.of(
                                                                        context)
                                                                    .textTheme
                                                                    .titleSmall,
                                                              ),
                                                            ),
                                                            state.contactInformation[index]
                                                                        .active ==
                                                                    true
                                                                ? const Badge(
                                                                    backgroundColor:
                                                                        Colors
                                                                            .green,
                                                                    label: Text(
                                                                      "Active",
                                                                    ),
                                                                  )
                                                                : const SizedBox(),
                                                            const SizedBox(
                                                                width: 5),
                                                            state.contactInformation[index]
                                                                        .primary ==
                                                                    true
                                                                ? const Badge(
                                                                    backgroundColor:
                                                                        Colors
                                                                            .blue,
                                                                    label: Text(
                                                                        "Primary"),
                                                                  )
                                                                : const SizedBox()
                                                          ],
                                                        ),
                                                        const SizedBox(
                                                          height: 5,
                                                        ),
                                                        Text(state
                                                            .contactInformation[
                                                                index]
                                                            .commService!
                                                            .serviceProvider!
                                                            .agency!
                                                            .name
                                                            .toString()),
                                                      ]),
                                                ),
                                                IconButton(
                                                    onPressed: () {},
                                                    icon: const Icon(
                                                      Icons.more_vert,
                                                      color: Colors.grey,
                                                    ))
                                              ],
                                            ),
                                          ),
                                          const SizedBox(
                                            height: 5,
                                          ),
                                        ],
                                      );
                                    });
                              } else {
                                const EmptyData(
                                    message:
                                        "You don't have contact information added. Please click + to add");
                              }
                            }
                            return Container();
                          },
                        );
                      }
                      return Container();
                    },
                  );
                }
                return Container();
              },
            ),
          )),
    );
  }
}
