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:intl/intl.dart';
import 'package:unit2/bloc/profile/profile_bloc.dart';
import 'package:unit2/bloc/user/user_bloc.dart';
import 'package:unit2/bloc/voluntary_works/voluntary_work_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 VolunataryWorkScreen extends StatelessWidget {
  const VolunataryWorkScreen({super.key});

  @override
  Widget build(BuildContext context) {
    DateFormat dteFormat2 = DateFormat.yMMMMd('en_US');
    return Scaffold(
      appBar: AppBar(
        title: const Text(voluntaryScreenTitle),
        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<VoluntaryWorkBloc,
                          VoluntaryWorkState>(
                        listener: (context, state) {
                          if (state is VoluntaryWorkLoadingState) {
                            final progress = ProgressHUD.of(context);
                            progress!.showWithText("Please wait...");
                          }
                          if (state is VoluntaryWorkLoadedState ||
                              state is VoluntaryWorkErrorState) {
                            final progress = ProgressHUD.of(context);
                            progress!.dismiss();
                          }
                        },
                        builder: (context, state) {
                          if (state is VoluntaryWorkLoadedState) {
                            if (state.voluntaryWorks.isNotEmpty) {
                              return ListView.builder(
                                  itemCount: state.voluntaryWorks.length,
                                  padding: const EdgeInsets.symmetric(
                                      vertical: 8, horizontal: 10),
                                  itemBuilder:
                                      (BuildContext context, int index) {
                                    String position = state
                                        .voluntaryWorks[index].position!.title!;
                                    String agency = state
                                        .voluntaryWorks[index].agency!.name!;
                                    String from = dteFormat2.format(
                                        state.voluntaryWorks[index].fromDate!);
                                    String hours = state
                                        .voluntaryWorks[index].totalHours
                                        .toString();
                                    String? to =
                                        state.voluntaryWorks[index].toDate ==
                                                null
                                            ? "Present"
                                            : dteFormat2.format(state
                                                .voluntaryWorks[index].toDate!);
                                    return Column(
                                      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(
                                                        position,
                                                        style: Theme.of(context)
                                                            .textTheme
                                                            .titleMedium!
                                                            .copyWith(
                                                                fontWeight:
                                                                    FontWeight
                                                                        .w500),
                                                      ),
                                                      const SizedBox(
                                                        height: 5,
                                                      ),
                                                      Text(
                                                        agency,
                                                        style: Theme.of(context)
                                                            .textTheme
                                                            .titleSmall,
                                                      ),
                                                      const Divider(),
                                                      const SizedBox(
                                                        height: 3,
                                                      ),
                                                      Text(
                                                          "$duration : $from to $to"),
                                                      const SizedBox(
                                                        height: 5,
                                                      ),
                                                      Text(
                                                          "$numberOfHours : $hours hours"),
                                                    ]),
                                              ),
                                              IconButton(
                                                  onPressed: () {},
                                                  icon: const Icon(
                                                      Icons.more_vert))
                                            ],
                                          ),
                                        ),
                                        const SizedBox(
                                          height: 5,
                                        ),
                                      ],
                                    );
                                  });
                            } else {
                              return const EmptyData(
                                  message:
                                      "You don't have any Voluntary Works added. Please click + to add.");
                            }
                          }
                          return Container();
                        },
                      );
                    }
                    return Container();
                  },
                );
              }
              return Container();
            },
          )),
    );
  }
}
