import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';

import '../../../../../utils/color_manager.dart';
import '../../../all_spaces/model/subspace_model.dart';
import '../../../create_subspace_model/widgets/subspace_chip.dart';
import '../../../space_model/models/subspace_template_model.dart';
import '../../bloc/subspace_bloc.dart';
import '../../bloc/subspace_event.dart';

class TextFieldSubSpaceDialogWidget extends StatelessWidget {
  const TextFieldSubSpaceDialogWidget({
    super.key,
    required TextEditingController subspaceNameController,
    required this.subSpaces,
  }) : _subspaceNameController = subspaceNameController;

  final TextEditingController _subspaceNameController;
  final List<SubspaceModel> subSpaces;
  @override
  Widget build(BuildContext context) {
    return Container(
      width: context.screenWidth * 0.35,
      padding: const EdgeInsets.symmetric(
        vertical: 10,
        horizontal: 16,
      ),
      decoration: BoxDecoration(
        color: ColorsManager.boxColor,
        borderRadius: BorderRadius.circular(10),
      ),
      child: Wrap(
        spacing: 8,
        runSpacing: 8,
        alignment: WrapAlignment.start,
        crossAxisAlignment: WrapCrossAlignment.center,
        children: [
          ...subSpaces.asMap().entries.map(
            (entry) {
              final index = entry.key;
              final subSpace = entry.value;

              final lowerName = subSpace.subspaceName.toLowerCase();

              final duplicateIndices = subSpaces
                  .asMap()
                  .entries
                  .where((e) => e.value.subspaceName.toLowerCase() == lowerName)
                  .map((e) => e.key)
                  .toList();
              final isDuplicate = duplicateIndices.length > 1 &&
                  duplicateIndices.indexOf(index) != 0;
              return SubspaceChip(
                subSpace: SubspaceTemplateModel(
                  subspaceName: entry.value.subspaceName,
                  disabled: entry.value.disabled,
                ),
                isDuplicate: isDuplicate,
                onDeleted: () => context.read<SubSpaceBloc>().add(
                      RemoveSubSpace(subSpace),
                    ),
              );
            },
          ),
          SizedBox(
            width: 200,
            child: TextField(
              controller: _subspaceNameController,
              decoration: InputDecoration(
                border: InputBorder.none,
                hintText: subSpaces.isEmpty ? 'Please enter the name' : null,
                hintStyle: context.textTheme.bodySmall?.copyWith(
                  color: ColorsManager.lightGrayColor,
                ),
              ),
              onSubmitted: (value) {
                final trimmedValue = value.trim();
                if (trimmedValue.isNotEmpty) {
                  context.read<SubSpaceBloc>().add(
                        AddSubSpace(
                          SubspaceModel(
                            subspaceName: trimmedValue,
                            disabled: false,
                          ),
                        ),
                      );
                  _subspaceNameController.clear();
                }
              },
              style: context.textTheme.bodyMedium,
            ),
          ),
        ],
      ),
    );
  }
}
