import 'package:date_time_picker/date_time_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:intl/intl.dart';
import 'package:unit2/bloc/profile/profile_bloc.dart';
import 'package:unit2/model/profile/basic_information/primary-information.dart';
import 'package:unit2/screens/profile/components/basic_information/edit_basic_info_modal.dart';
import 'package:unit2/theme-data.dart/btn-style.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:unit2/theme-data.dart/form-style.dart';
import 'package:unit2/utils/global.dart';
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/Leadings/close_leading.dart';
import 'package:unit2/widgets/error_state.dart';

import '../../../../utils/alerts.dart';

class PrimaryInfo extends StatefulWidget {
  final int profileId;
  final String token;
  const PrimaryInfo({super.key, required this.profileId, required this.token});

  @override
  State<PrimaryInfo> createState() => _PrimaryInfoState();
}

class _PrimaryInfoState extends State<PrimaryInfo> {
  @override
  Widget build(BuildContext context) {
    final _formKey = GlobalKey<FormBuilderState>();

    bool enabled = false;
    DateFormat dteFormat2 = DateFormat.yMMMMd('en_US');
    return Scaffold(
        appBar: AppBar(
            title: context.watch<ProfileBloc>().state
                    is BasicInformationEditingState
                ? const Text("Edit Profile")
                : const Text("Primary Information"),
            centerTitle: true,
            backgroundColor: primary,
            actions: context.watch<ProfileBloc>().state
                    is BasicInformationProfileLoaded
                ? [
                    IconButton(
                        onPressed: () {
                          context.read<ProfileBloc>().add(
                              ShowPrimaryInfoEditForm(token: widget.token));
                        },
                        icon: const Icon(Icons.edit))
                  ]
                : context.watch<ProfileBloc>().state
                        is BasicInformationEditingState
                    ? [
                        CloseLeading(onPressed: () {
                          context
                              .read<ProfileBloc>()
                              .add(LoadBasicPrimaryInfo());
                        })
                      ]
                    : []),
        body: ProgressHUD(
          padding: const EdgeInsets.all(24),
          backgroundColor: Colors.black87,
          indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
          child: BlocConsumer<ProfileBloc, ProfileState>(
            listener: (context, state) {
              if (state is BasicPrimaryInformationLoadingState) {
                final progress = ProgressHUD.of(context);
                progress!.showWithText("Please wait...");
              }
              if (state is BasicInformationProfileLoaded ||
                  state is BasicInformationEditingState ||
                  state is BasicPrimaryInformationErrorState ||
                  state is BasicProfileInfoEditedState) {
                final progress = ProgressHUD.of(context);
                progress!.dismiss();
              }
              if (state is BasicProfileInfoEditedState) {
                if (state.response['success']) {
                  successAlert(context, "Updated Successfull!",
                      state.response['message'], () {
                    Navigator.of(context).pop();
                    context.read<ProfileBloc>().add(LoadBasicPrimaryInfo());
                  });
                } else {
                  errorAlert(context, "Update Failed",
                      "Something went wrong. Please try again.", () {
                    Navigator.of(context).pop();
                    context.read<ProfileBloc>().add(LoadBasicPrimaryInfo());
                  });
                }
              }
            },
            builder: (context, state) {
              if (state is BasicInformationProfileLoaded) {
                return Container(
                  padding:
                      const EdgeInsets.symmetric(vertical: 24, horizontal: 24),
                  child: Column(
                    children: [
                      const SizedBox(
                        height: 28,
                      ),
                      FormBuilderTextField(
                        enabled: enabled,
                        name: lastname,
                        initialValue: state.primaryBasicInformation.lastName!,
                        decoration: normalTextFieldStyle("Last name", ""),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      FormBuilderTextField(
                        enabled: enabled,
                        name: firstname,
                        initialValue: state.primaryBasicInformation.firstName!,
                        decoration: normalTextFieldStyle("First name", ""),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      SizedBox(
                        width: screenWidth,
                        child: Row(children: [
                          Flexible(
                            flex: 2,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: middlename,
                              initialValue:
                                  state.primaryBasicInformation.middleName!,
                              decoration:
                                  normalTextFieldStyle("Middle name", ""),
                            ),
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: extensionName,
                              initialValue: state.primaryBasicInformation
                                  .nameExtension ??= 'N/A',
                              decoration:
                                  normalTextFieldStyle("Name extension", ""),
                            ),
                          )
                        ]),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      SizedBox(
                        width: screenWidth,
                        child: Row(children: [
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: extensionName,
                              initialValue: dteFormat2.format(
                                  state.primaryBasicInformation.birthdate!),
                              decoration:
                                  normalTextFieldStyle("Birth date", ""),
                            ),
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: sex,
                              initialValue: state.primaryBasicInformation.sex!,
                              decoration: normalTextFieldStyle("Sex", ""),
                            ),
                          )
                        ]),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      SizedBox(
                        width: screenWidth,
                        child: Row(children: [
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: "bloodType",
                              initialValue:
                                  state.primaryBasicInformation.bloodType!,
                              decoration:
                                  normalTextFieldStyle("Blood type", ""),
                            ),
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: "civilStatus",
                              initialValue:
                                  state.primaryBasicInformation.civilStatus!,
                              decoration:
                                  normalTextFieldStyle("Civil Status", ""),
                            ),
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: "gender",
                              initialValue: state
                                  .primaryBasicInformation.gender ??= "N/A",
                              decoration: normalTextFieldStyle("Gender", ""),
                            ),
                          ),
                        ]),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      SizedBox(
                        width: screenWidth,
                        child: Row(children: [
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: height,
                              initialValue: state
                                  .primaryBasicInformation.heightM!
                                  .toString(),
                              decoration: normalTextFieldStyle("Height", ""),
                            ),
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: width,
                              initialValue: state
                                  .primaryBasicInformation.weightKg!
                                  .toString(),
                              decoration: normalTextFieldStyle("Weight", ""),
                            ),
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: prefixSuffix,
                              initialValue:
                                  "${state.primaryBasicInformation.titlePrefix ??= "NA"} | ${state.primaryBasicInformation.titleSuffix ??= "N/A"}",
                              decoration: normalTextFieldStyle(
                                  "Title Prefix and Suffix", ""),
                            ),
                          ),
                        ]),
                      ),
                      const SizedBox(
                        height: 12,
                      ),
                      SizedBox(
                        width: screenWidth,
                        child: Row(children: [
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: height,
                              initialValue: state.primaryBasicInformation.ip ??=
                                  "N/A",
                              decoration:
                                  normalTextFieldStyle("Indigenous", ""),
                            ),
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: width,
                              initialValue: state
                                  .primaryBasicInformation.ethnicity ??= "N/A",
                              decoration: normalTextFieldStyle("Ethnicity", ""),
                            ),
                          ),
                        ]),
                      ),
                      const SizedBox(
                        height: 12,
                      ),
                      SizedBox(
                        width: screenWidth,
                        child: Row(children: [
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: height,
                              initialValue: state
                                  .primaryBasicInformation.religion ??= "N/A",
                              decoration: normalTextFieldStyle("Religion", ""),
                            ),
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          Flexible(
                            flex: 1,
                            child: FormBuilderTextField(
                              enabled: enabled,
                              name: width,
                              initialValue: state
                                  .primaryBasicInformation.disability ??= "N/A",
                              decoration:
                                  normalTextFieldStyle("Disability", ""),
                            ),
                          ),
                        ]),
                      ),
                    ],
                  ),
                );
              }
              if (state is BasicPrimaryInformationErrorState) {
                return SomethingWentWrong(
                    message: state.message,
                    onpressed: () {
                      context.read<ProfileBloc>().add(LoadBasicPrimaryInfo());
                    });
              }
              if (state is BasicInformationEditingState) {
                return EditBasicProfileInfoScreen(
                    profileId: widget.profileId, token: widget.token);
              }
              return Container();
            },
          ),
        ));
  }
}
