import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/data/enums/state_status.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_input.dart';
import 'package:krow/features/profile/inclusive/domain/bloc/inclusive_info_bloc.dart';

class AccessibilityDetailsWidget extends StatefulWidget {
  const AccessibilityDetailsWidget({super.key});

  @override
  State<AccessibilityDetailsWidget> createState() =>
      _AccessibilityDetailsWidgetState();
}

class _AccessibilityDetailsWidgetState
    extends State<AccessibilityDetailsWidget> {
  final _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<InclusiveInfoBloc, InclusiveInfoState>(
      buildWhen: (previous, current) =>
          previous.areAccommodationsRequired !=
              current.areAccommodationsRequired ||
          previous.status != current.status,
      listenWhen: (previous, current) => previous.status != current.status,
      listener: (context, state) {
        _controller.text = state.accommodationsDetails;
      },
      builder: (context, state) {
        return AnimatedOpacity(
          opacity: state.areAccommodationsRequired ?? false ? 1 : 0,
          duration: Durations.medium2,
          child: Column(
            children: [
              Text(
                'describe_accommodations'.tr(),
                style: AppTextStyles.bodyMediumReg.copyWith(
                  color: AppColors.blackGray,
                ),
                textAlign: TextAlign.start,
              ),
              const Gap(16),
              KwTextInput(
                enabled: state.areAccommodationsRequired ?? false,
                controller: _controller,
                minHeight: 160,
                maxLength: 300,
                showCounter: true,
                radius: 12,
                title: 'additional_details'.tr(),
                hintText: 'enter_main_text'.tr(),
                showError: state.status == StateStatus.error,
                helperText: state.status == StateStatus.error
                    ? 'required_to_fill'.tr()
                    : null,
                onChanged: (details) {
                  context
                      .read<InclusiveInfoBloc>()
                      .add(ChangeAccommodationsDetails(details));
                },
              ),
            ],
          ),
        );
      },
    );
  }
}
