import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.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/learningDevelopment/learning_development_bloc.dart';
import 'package:unit2/bloc/profile/profile_bloc.dart';
import 'package:unit2/bloc/user/user_bloc.dart';
import 'package:unit2/model/profile/learning_development.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 LearningAndDevelopmentScreen extends StatelessWidget {

  const LearningAndDevelopmentScreen(
      {super.key,});

  @override
  Widget build(BuildContext context) {
    DateFormat dteFormat2 = DateFormat.yMMMMd('en_US');
    return Scaffold(
        appBar: AppBar(
          title: const Text(learningAndDevelopmentScreenTitle),
          centerTitle: true,
          backgroundColor: primary,
          actions: [AddLeading(onPressed: () {})],
        ),
        body: ProgressHUD(
          padding: const EdgeInsets.all(24),
          indicatorWidget: const SpinKitFadingCircle(
            color: Colors.white,
          ),
             backgroundColor: Colors.black87,
          child: BlocBuilder<UserBloc, UserState>(
            builder: (context, state) {
              if (state is UserLoggedIn) {
                return BlocBuilder<ProfileBloc, ProfileState>(
                  builder: (context, state) {
                    if (state is ProfileLoaded) {
                      return BlocConsumer<LearningDevelopmentBloc,
                          LearningDevelopmentState>(
                        listener: (context, state) {
                          if (state is LearningDevelopmentLoadingState) {
                            final progress = ProgressHUD.of(context);
                            progress!.showWithText("Please wait...");
                          }
                          if (state is LearningDevelopmentLoadedState ||
                              state is LeaningDevelopmentErrorState) {
                            final progress = ProgressHUD.of(context);
                            progress!.dismiss();
                          }
                          // TODO: implement listener
                        },
                        builder: (context, state) {
                          if (state is LearningDevelopmentLoadedState) {
                            if (state.learningsAndDevelopment.isNotEmpty) {
                              return ListView.builder(
                                  padding: const EdgeInsets.symmetric(
                                      vertical: 8, horizontal: 10),
                                  itemCount:
                                      state.learningsAndDevelopment.length,
                                  itemBuilder:
                                      (BuildContext context, int index) {
                                    String training = state
                                        .learningsAndDevelopment[index]
                                        .conductedTraining!
                                        .title!
                                        .title!;
                                    String provider = state
                                        .learningsAndDevelopment[index]
                                        .conductedTraining!
                                        .conductedBy!
                                        .name!;
                                    String start = dteFormat2.format(state
                                        .learningsAndDevelopment[index]
                                        .conductedTraining!
                                        .fromDate!);
                                    String end = dteFormat2.format(state
                                        .learningsAndDevelopment[index]
                                        .conductedTraining!
                                        .toDate!);
                                    String type = state
                                        .learningsAndDevelopment[index]
                                        .conductedTraining!
                                        .learningDevelopmentType!
                                        .title!;
                                    return Column(
                                      children: [
                                        Container(
                                          decoration: box1(),
                                          padding: const EdgeInsets.symmetric(
                                              horizontal: 12, vertical: 8),
                                          width: screenWidth,
                                          child: Row(
                                            children: [
                                              Expanded(
                                                child: Column(
                                                    mainAxisAlignment:
                                                        MainAxisAlignment.start,
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment
                                                            .start,
                                                    children: [
                                                      Text(
                                                        training,
                                                        style: Theme.of(context)
                                                            .textTheme
                                                            .titleMedium!
                                                            .copyWith(
                                                                fontWeight:
                                                                    FontWeight
                                                                        .w500),
                                                      ),
                                                      const Divider(),
                                                      const SizedBox(
                                                        height: 5,
                                                      ),
                                                      Text(
                                                        provider,
                                                        style: Theme.of(context)
                                                            .textTheme
                                                            .titleSmall,
                                                      ),
                                                      const SizedBox(
                                                        height: 5,
                                                      ),
                                                      Text(
                                                        "$duration: $start to $end",
                                                        style: Theme.of(context)
                                                            .textTheme
                                                            .labelMedium,
                                                      ),
                                                      const SizedBox(
                                                        height: 5,
                                                      ),
                                                 
                                                    ]),
                                              ),
                                              IconButton(
                                                  onPressed: () {},
                                                  icon: const Icon(
                                                      Icons.more_vert)),
                                            ],
                                          ),
                                        ),
                                        const SizedBox(
                                          height: 8,
                                        ),
                                      ],
                                    );
                                  });
                            } else {
                              const EmptyData(
                                  message:
                                      "You don't have any Learning and Development added. Please click + to add.");
                            }
                          }
                          if (state is LeaningDevelopmentErrorState) {
                            return (SomethingWentWrong(
                                message: state.message, onpressed: () {}));
                          }
                          return Container();
                        },
                      );
                    }
                    return Container();
                  },
                );
              }
              return Container();
            },
          ),
        ));
  }
}
