import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gap/gap.dart';
import 'package:tanami_app/core/styles/app_color.dart';
import 'package:tanami_app/core/styles/app_text.dart';
import 'package:tanami_app/shared/components/text_widget.dart';

import '../../../../shared/components/bloc/checkbox/checkbox_bloc.dart';
import '../../../../shared/components/bloc/checkbox/checkbox_state.dart';
import '../../../../shared/components/checkbox_widget.dart';
import '../../../../shared/components/form_label_textfield.dart';
import '../bloc/delete_account_bloc.dart';
import '../bloc/text_bloc.dart';
import '../bloc/text_event.dart';
import '../bloc/text_state.dart';

class FormSection extends StatelessWidget {
  const FormSection({super.key});

  @override
  Widget build(BuildContext context) {
    final deleteAccountBloc = context.read<DeleteAccountBloc>();
    return Form(
      key: deleteAccountBloc.formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.only(
              left: 16.0,
              right: 16,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Gap(70),
                BlocBuilder<TextBloc, TextState>(
                  builder: (context, state) {
                    return FormLabelTextField(
                      onChangeFun: (p0) {
                        context.read<TextBloc>().add(TextChanged(
                              deleteAccountBloc.descriptionTextField.text,
                            ));
                      },
                      hintText: AppText.enterAdescription,
                      textEditingController:
                          deleteAccountBloc.descriptionTextField,
                      title: AppText
                          .toHelpUsImprovePleaseLetusKnowWhyYouWantToDeleteAccount,
                      type: "description",
                    );
                  },
                ),
                const Gap(8),
                BlocBuilder<TextBloc, TextState>(
                  builder: (context, state) {
                    if (state is TextUpdated) {
                      return TextWidget().text14W400(
                          "${state.charactersLeft} ${AppText.charactersLeft}",
                          clr: AppColor.descriptionText);
                    }
                    return TextWidget().text14W400(
                        "350 ${AppText.charactersLeft}",
                        clr: AppColor.descriptionText);
                  },
                ),
              ],
            ),
          ),
          Stack(
            children: [
              SizedBox(
                width: 1.sw,
                height: 85.h,
              ),
              const CheckBoxWidget(),
              Positioned(
                left: 45,
                top: 14,
                child: SizedBox(
                  width: 0.85.sw,
                  child: TextWidget().text14W500(
                    AppText.iUnderstandAndAgreeThatmyDataWillBeDeleted,
                    clr: AppColor.charcoalColor,
                    txtAlign: TextAlign.start,
                  ),
                ),
              ),
              BlocBuilder<CheckboxBloc, CheckBoxState>(
                builder: (context, state) {
                  if (state is CheckboxError) {
                    return Positioned(
                      left: 45,
                      top: 55,
                      child: SizedBox(
                        width: 0.75.sw,
                        child: TextWidget().text14W500(
                          AppText.pleaseCheckThisField,
                          clr: AppColor.txtErrorColor,
                          txtAlign: TextAlign.start,
                        ),
                      ),
                    );
                  }
                  return const SizedBox.shrink();
                },
              ),
            ],
          ),
        ],
      ),
    );
  }
}
