import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:unit2/bloc/profile/primary_information/identification/identification_bloc.dart';
import 'package:unit2/bloc/profile/profile_bloc.dart';
import 'package:unit2/model/profile/basic_information/identification_information.dart';
import 'package:unit2/theme-data.dart/box_shadow.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:unit2/utils/global.dart';
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/Leadings/add_leading.dart';
import 'package:unit2/widgets/empty_data.dart';

import '../../../../bloc/user/user_bloc.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text(identificationScreenTitle),
          centerTitle: true,
          backgroundColor: primary,
          actions: [AddLeading(onPressed: () {})],
        ),
        body: BlocBuilder<UserBloc, UserState>(
          builder: (context, state) {
            if (state is UserLoggedIn) {
              return BlocBuilder<ProfileBloc, ProfileState>(
                builder: (context, state) {
                  if (state is ProfileLoaded) {
                    return BlocConsumer<IdentificationBloc,
                        IdentificationState>(
                      listener: (context, state) {},
                      builder: (context, state) {
                        if (state is IdentificationLoadedState) {
                          if (state.identificationInformation.isNotEmpty) {
                            return ListView.builder(
                                padding: const EdgeInsets.symmetric(
                                    vertical: 8, horizontal: 10),
                                itemCount: state.identificationInformation.length,
                                itemBuilder: (BuildContext context, int index) {
                                  String agency =
                                      state.identificationInformation[index].agency!.name!;
                                  String idNumber =
                                      state.identificationInformation[index].identificationNumber!;
                                  bool government =
                                      state.identificationInformation[index].agency!.privateEntity!;
                                  String issuedAt =
                                      "${state.identificationInformation[index].issuedAt!.cityMunicipality!.description!} ${state.identificationInformation[index].issuedAt!.cityMunicipality!.province!.description}";
                                  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(agency,
                                                        style: Theme.of(context)
                                                            .textTheme
                                                            .titleMedium!
                                                            .copyWith(
                                                                fontWeight:
                                                                    FontWeight
                                                                        .w400)),
                                                    const Divider(),
                                                    const SizedBox(
                                                      height: 5,
                                                    ),
                                                    Row(children: [
                                                      Expanded(
                                                        child: Text(
                                                          "$idNumberText : $idNumber",
                                                          style:
                                                              Theme.of(context)
                                                                  .textTheme
                                                                  .titleSmall,
                                                        ),
                                                      ),
                                                      Badge(
                                                          backgroundColor:
                                                              success2,
                                                          label: Text(
                                                            government == true
                                                                ? privateText
                                                                    .toUpperCase()
                                                                : governmentText
                                                                    .toUpperCase(),
                                                            style: Theme.of(
                                                                    context)
                                                                .textTheme
                                                                .bodySmall!
                                                                .copyWith(
                                                                    color: Colors
                                                                        .white),
                                                          ))
                                                    ]),
                                                    const SizedBox(
                                                      height: 5,
                                                    ),
                                                    Text(issuedAt),
                                                  ]),
                                            ),
                                            IconButton(
                                                onPressed: () {},
                                                icon: const Icon(
                                                  Icons.more_vert,
                                                  color: Colors.grey,
                                                ))
                                          ],
                                        ),
                                      ),
                                      const SizedBox(
                                        height: 5,
                                      ),
                                    ],
                                  );
                                });
                          } else {
                            const EmptyData(
                                message:
                                    "You don't have identifications added. Please click + to add.");
                          }
                        }
                        return Container();
                      },
                    );
                  }
                  return Container();
                },
              );
            }
            return Container();
          },
        ));
  }
}
