import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:unit2/bloc/profile/primary_information/address/address_bloc.dart';
import 'package:unit2/bloc/profile/profile_bloc.dart';
import 'package:unit2/bloc/user/user_bloc.dart';
import 'package:unit2/model/profile/basic_information/adress.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 'package:unit2/widgets/error_state.dart';

class AddressScreen extends StatelessWidget {

  const AddressScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text(adressScreenTitle),
          centerTitle: true,
          backgroundColor: primary,
          actions: [AddLeading(onPressed: () {})],
        ),
        body: ProgressHUD(
          child: BlocBuilder<UserBloc, UserState>(
            builder: (context, state) {
              if (state is UserLoggedIn) {
                return BlocBuilder<ProfileBloc, ProfileState>(
                  builder: (context, state) {
                    if (state is ProfileLoaded) {
                      return BlocConsumer<AddressBloc, AddressState>(
                        listener: (context, state) {
                          if (state is AddressLoadingState) {
                            final progress = ProgressHUD.of(context);
                            progress!.showWithText("Please wait...");
                          }
                          if (state is AddressLoadedState ||
                              state is AddressErrorState) {
                            final progress = ProgressHUD.of(context);
                            progress!.dismiss();
                          }
                        },
                        builder: (context, state) {
                          if (state is AddressLoadedState) {
                            if (state.addresses.isNotEmpty) {
                            return  ListView.builder(
                                  padding: const EdgeInsets.symmetric(
                                      vertical: 8, horizontal: 10),
                                  itemCount: state.addresses.length,
                                  itemBuilder:
                                      (BuildContext context, int index) {
                                    String? subdivision =
                                        state.addresses[index].details ?? '';
                                    String category = state.addresses[index]
                                        .address!
                                        .category!
                                        .name!;
                                    String? barangay = state.addresses[index]
                                                .address!
                                                .barangay !=
                                            null
                                        ? '${state.addresses[index].address!.barangay!.description!.toUpperCase()},'
                                        : '';
                                    String cityMunicipality = state.addresses[index]
                                        .address!
                                        .cityMunicipality!
                                        .description!;
                                    String province = state.addresses[index]
                                        .address!
                                        .cityMunicipality!
                                        .province!
                                        .description!;
                                    String region = state.addresses[index]
                                        .address!
                                        .cityMunicipality!
                                        .province!
                                        .region!
                                        .description!;
                                    return Column(
                                      children: [
                                        Column(
                                          children: [
                                            Container(
                                              width: screenWidth,
                                              decoration: box1(),
                                              padding:
                                                  const EdgeInsets.fromLTRB(
                                                      8, 16, 0, 16),
                                              child: Row(children: [
                                                Expanded(
                                                    child: Column(
                                                  children: [
                                                    Row(
                                                      children: [
                                                        Text(
                                                          subdivision,
                                                          style:
                                                              Theme.of(context)
                                                                  .textTheme
                                                                  .titleMedium,
                                                        ),
                                                        const SizedBox(
                                                          width: 5,
                                                        ),
                                                        Text(
                                                          category,
                                                          style:
                                                              Theme.of(context)
                                                                  .textTheme
                                                                  .bodySmall,
                                                        )
                                                      ],
                                                    ),
                                                    const Divider(),
                                                    const SizedBox(
                                                      height: 5,
                                                    ),
                                                    Text(
                                                      "$barangay $cityMunicipality, $province, $region",
                                                      style: Theme.of(context)
                                                          .textTheme
                                                          .labelLarge,
                                                    ),
                                                  ],
                                                )),
                                                IconButton(
                                                    onPressed: () {},
                                                    icon: const Icon(
                                                      Icons.more_vert,
                                                      color: Colors.grey,
                                                    ))
                                              ]),
                                            ),
                                          ],
                                        ),
                                        const SizedBox(
                                          height: 5,
                                        ),
                                      ],
                                    );
                                  });
                            } else {
                              const EmptyData(
                                  message:
                                      "You don't have address added. Please click + to add.");
                            }
                          }
                          return Container();
                        },
                      );
                    }
                    return Container();
                  },
                );
              }
              return Container();
            },
          ),
        ));
  }
}
