import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_input.dart';
import 'package:krow/features/create_event/domain/bloc/create_event_bloc.dart';

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

  @override
  State<AddInfoInputWidget> createState() => _AddInfoInputWidgetState();
}

class _AddInfoInputWidgetState extends State<AddInfoInputWidget> {
  final TextEditingController _addInfoController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<CreateEventBloc, CreateEventState>(
      listener: (context, state) {
        if (state.entity.additionalInfo != _addInfoController.text) {
          _addInfoController.text = state.entity.additionalInfo??'';
        }
      },
      buildWhen: (previous, current) {
        return previous.entity.additionalInfo != current.entity.additionalInfo;
      },
      builder: (context, state) {
        return KwTextInput(
          controller: _addInfoController,
          onChanged: (value) {
            BlocProvider.of<CreateEventBloc>(context)
                .add(CreateEventAddInfoChange(value));
          },
          maxLength: 300,
          showCounter: true,
          minHeight: 144,
          hintText: 'Enter your main text here...',
          title: 'Additional Information',
        );
      },
    );
  }
}
