import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/create_subspace_model/bloc/subspace_model_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/create_subspace_model/bloc/subspace_model_event.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/models/subspace_template_model.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';

class SubspacesTextfield extends StatefulWidget {
  const SubspacesTextfield({
    required this.hintText,
    super.key,
  });

  final String? hintText;

  @override
  State<SubspacesTextfield> createState() => _SubspacesTextfieldState();
}

class _SubspacesTextfieldState extends State<SubspacesTextfield> {
  late final TextEditingController _controller;
  @override
  void initState() {
    _controller = TextEditingController();
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 100,
      child: TextField(
        controller: _controller,
        decoration: InputDecoration(
          border: InputBorder.none,
          hintText: widget.hintText,
          hintStyle: context.textTheme.bodySmall?.copyWith(
            color: ColorsManager.lightGrayColor,
          ),
        ),
        onSubmitted: (value) {
          final trimmedValue = value.trim();
          if (trimmedValue.isNotEmpty) {
            context.read<SubSpaceModelBloc>().add(
                  AddSubSpaceModel(
                    SubspaceTemplateModel(
                      subspaceName: trimmedValue,
                      disabled: false,
                    ),
                  ),
                );
            _controller.clear();
          }
        },
        style: context.textTheme.bodyMedium?.copyWith(
          color: ColorsManager.blackColor,
        ),
      ),
    );
  }
}
