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/education/education_bloc.dart';
import 'package:unit2/bloc/profile/profile_bloc.dart';
import 'package:unit2/bloc/user/user_bloc.dart';

import 'package:unit2/model/profile/educational_background.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';
import 'package:unit2/widgets/error_state.dart';

class EducationScreen extends StatelessWidget {

  const EducationScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text(educationScreenTitle),
          centerTitle: true,
          backgroundColor: primary,
          actions: [AddLeading(onPressed: () {})],
        ),
        //userbloc
        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) {
                //profilebloc
                return BlocBuilder<ProfileBloc, ProfileState>(
                  builder: (context, state) {
                    if (state is ProfileLoaded) {
                      //education bloc
                      return BlocConsumer<EducationBloc, EducationState>(
                        listener: (context, state) {
                            if (state is EducationalBackgroundLoadingState) {
                              final progress = ProgressHUD.of(context);
                              progress!.showWithText("Please wait...");
                            }
                            if(state is EducationalBackgroundLoadedState || state is EducationalBackgroundErrorState){
                                   final progress = ProgressHUD.of(context);
                              progress!.dismiss();
                            }
                        },
                        builder: (context, state) {
                          if (state is EducationalBackgroundLoadedState) {
                            if (state.educationalBackground.isNotEmpty) {
                              return ListView.builder(
                                  padding: const EdgeInsets.symmetric(
                                      vertical: 8, horizontal: 10),
                                  itemCount: state.educationalBackground.length,
                                  itemBuilder: (BuildContext context, int index) {
                                    String level = state
                                        .educationalBackground[index]
                                        .education!
                                        .level!;
                                    String periodFrom = state
                                        .educationalBackground[index].periodFrom!;
                                    String periodTo = state
                                        .educationalBackground[index].periodTo!;
                                    String? program = state
                                                .educationalBackground[index]
                                                .education!
                                                .course ==
                                            null
                                        ? null
                                        : state.educationalBackground[index]
                                            .education!.course!.program!;
                                    List<Honor>? honors = state
                                        .educationalBackground[index].honors!
                                        .toList();
                                    String school = state
                                        .educationalBackground[index]
                                        .education!
                                        .school!
                                        .name!;
                                    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: [
                                                      Row(
                                                        children: [
                                                          Expanded(
                                                              child: Text(
                                                            level,
                                                            style: Theme.of(
                                                                    context)
                                                                .textTheme
                                                                .titleMedium!
                                                                .copyWith(
                                                                    fontWeight:
                                                                        FontWeight
                                                                            .w500),
                                                          )),
                                                          Text(
                                                            "$periodFrom - ",
                                                            style:
                                                                Theme.of(context)
                                                                    .textTheme
                                                                    .bodyMedium,
                                                          ),
                                                        ],
                                                      ),
                                                      const SizedBox(
                                                        height: 5,
                                                      ),
                                                      Text(
                                                        school,
                                                        style: Theme.of(context)
                                                            .textTheme
                                                            .titleSmall,
                                                      ),
                                                      Container(
                                                          padding:
                                                              const EdgeInsets
                                                                  .only(top: 8),
                                                          child: honors.isNotEmpty
                                                              ? Column(
                                                                  mainAxisAlignment:
                                                                      MainAxisAlignment
                                                                          .start,
                                                                  crossAxisAlignment:
                                                                      CrossAxisAlignment
                                                                          .start,
                                                                  children: [
                                                                    const Text(
                                                                      " : ",
                                                                      style: TextStyle(
                                                                          fontWeight:
                                                                              FontWeight.w600),
                                                                    ),
                                                                    Column(
                                                                      children: honors
                                                                          .map((Honor
                                                                                  honor) =>
                                                                              Text("     - ${honor.name!}"))
                                                                          .toList(),
                                                                    ),
                                                                  ],
                                                                )
                                                              : const SizedBox()),
                                                      program == null
                                                          ? const SizedBox()
                                                          : Column(
                                                              mainAxisAlignment:
                                                                  MainAxisAlignment
                                                                      .start,
                                                              crossAxisAlignment:
                                                                  CrossAxisAlignment
                                                                      .start,
                                                              children: [
                                                                const SizedBox(
                                                                  height: 5,
                                                                ),
                                                                Text(program),
                                                              ],
                                                            ),
                                                    ]),
                                              ),
                                              IconButton(
                                                  onPressed: () {},
                                                  icon: const Icon(
                                                    Icons.more_vert,
                                                    color: Colors.grey,
                                                  ))
                                            ],
                                          ),
                                        ),
                                        const SizedBox(
                                          height: 5,
                                        ),
                                      ],
                                    );
                                  });
                            } else {
                              const EmptyData(
                                  message:
                                      "You don't have any Educational Background added. Please click + to add.");
                            }
                          }if(state is EducationalBackgroundErrorState){
                            return SomethingWentWrong(message: state.message, onpressed: (){});
                          }
                          return Container();
                        },
                      );
                    }
                    return Container();
                  },
                );
              }
              return Container();
            },
          ),
        ));
  }
}
