import 'package:flutter/material.dart';

import '../../../../../common/edit_chip.dart';
import '../../../../../utils/color_manager.dart';
import '../../../space_model/widgets/button_content_widget.dart';
import '../../../space_model/widgets/subspace_name_label_widget.dart';
import '../../model/subspace_model.dart';

class SubSpacePartWidget extends StatefulWidget {
  SubSpacePartWidget({
    super.key,
    required this.subspaces,
    required this.onPressed,
    required this.isTagsAndSubspaceModelDisabled,
    required this.screenWidth,
    required this.editChipOnTap,
  });
  double screenWidth;
  bool isTagsAndSubspaceModelDisabled;
  final void Function() editChipOnTap;
  List<SubspaceModel>? subspaces;
  final void Function() onPressed;

  @override
  State<SubSpacePartWidget> createState() => _SubSpacePartWidgetState();
}

class _SubSpacePartWidgetState extends State<SubSpacePartWidget> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        widget.subspaces == null || widget.subspaces!.isEmpty
            ? TextButton(
                style: TextButton.styleFrom(
                  padding: EdgeInsets.zero,
                  overlayColor: ColorsManager.transparentColor,
                ),
                onPressed: widget.onPressed,
                child: ButtonContentWidget(
                  icon: Icons.add,
                  label: 'Create Sub Spaces',
                  disabled: widget.isTagsAndSubspaceModelDisabled,
                ),
              )
            : SizedBox(
                width: widget.screenWidth * 0.25,
                child: Container(
                  padding: const EdgeInsets.all(8.0),
                  decoration: BoxDecoration(
                    color: ColorsManager.textFieldGreyColor,
                    borderRadius: BorderRadius.circular(15),
                    border: Border.all(
                      color: ColorsManager.textFieldGreyColor,
                      width: 3.0, // Border width
                    ),
                  ),
                  child: Wrap(
                    spacing: 8.0,
                    runSpacing: 8.0,
                    children: [
                      if (widget.subspaces != null)
                        ...widget.subspaces!.map((subspace) {
                          return Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SubspaceNameDisplayWidget(
                                text: subspace.subspaceName,
                                validateName: (updatedName) {
                                  bool nameExists = widget.subspaces!.any((s) {
                                    bool isSameId =
                                        s.internalId == subspace.internalId;
                                    bool isSameName =
                                        s.subspaceName.trim().toLowerCase() ==
                                            updatedName.trim().toLowerCase();

                                    return !isSameId && isSameName;
                                  });

                                  return !nameExists;
                                },
                                onNameChanged: (updatedName) {
                                  setState(() {
                                    subspace.subspaceName = updatedName;
                                  });
                                },
                              ),
                            ],
                          );
                        }),
                      EditChip(
                        onTap: widget.editChipOnTap,
                      )
                    ],
                  ),
                ),
              )
      ],
    );
  }
}
