import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

import '../../../../../common/edit_chip.dart';
import '../../../../../utils/color_manager.dart';
import '../../../helper/tag_helper.dart';
import '../../../space_model/widgets/button_content_widget.dart';
import '../../model/subspace_model.dart';
import '../../model/tag.dart';

class DevicesPartWidget extends StatelessWidget {
  const DevicesPartWidget({
    super.key,
    required this.tags,
    required this.subspaces,
    required this.screenWidth,
    required this.onEditChip,
    required this.onTextButtonPressed,
    required this.isTagsAndSubspaceModelDisabled,
  });
  final bool isTagsAndSubspaceModelDisabled;
  final void Function() onEditChip;
  final void Function() onTextButtonPressed;
  final double screenWidth;
  final List<Tag>? tags;
  final List<SubspaceModel>? subspaces;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        (tags?.isNotEmpty == true ||
                subspaces?.any(
                        (subspace) => subspace.tags?.isNotEmpty == true) ==
                    true)
            ? SizedBox(
                width: 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: [
                      // Combine tags from spaceModel and subspaces
                      ...TagHelper.groupTags([
                        ...?tags,
                        ...?subspaces?.expand((subspace) => subspace.tags ?? [])
                      ]).entries.map(
                            (entry) => Chip(
                              avatar: SizedBox(
                                width: 24,
                                height: 24,
                                child: SvgPicture.asset(
                                  entry.key.icon ?? 'assets/icons/gateway.svg',
                                  fit: BoxFit.contain,
                                ),
                              ),
                              label: Text(
                                'x${entry.value}', // Show count
                                style: Theme.of(context)
                                    .textTheme
                                    .bodySmall
                                    ?.copyWith(color: ColorsManager.spaceColor),
                              ),
                              backgroundColor: ColorsManager.whiteColors,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(16),
                                side: const BorderSide(
                                  color: ColorsManager.spaceColor,
                                ),
                              ),
                            ),
                          ),

                      EditChip(onTap: onEditChip)
                    ],
                  ),
                ),
              )
            : TextButton(
                onPressed: onTextButtonPressed,
                style: TextButton.styleFrom(
                  padding: EdgeInsets.zero,
                ),
                child: ButtonContentWidget(
                  icon: Icons.add,
                  label: 'Add Devices',
                  disabled: isTagsAndSubspaceModelDisabled,
                ),
              )
      ],
    );
  }
}
