import 'package:flutter/material.dart';

import 'package:get/get.dart';

import 'package:f0ckapp/models/item.dart';
import 'package:f0ckapp/widgets/actiontag.dart';
import 'package:f0ckapp/controller/mediacontroller.dart';

class TagSection extends StatefulWidget {
  final List<Tag> tags;
  const TagSection({super.key, required this.tags});

  @override
  State<TagSection> createState() => _TagSectionState();
}

class _TagSectionState extends State<TagSection> {
  bool _areTagsExpanded = false;

  @override
  Widget build(BuildContext context) {
    final MediaController mediaController = Get.find<MediaController>();
    final bool hasMoreTags = widget.tags.length > 5;
    final List<Tag> tagsToShow = _areTagsExpanded
        ? widget.tags
        : widget.tags.take(5).toList();

    return Column(
      children: [
        Wrap(
          spacing: 6.0,
          runSpacing: 4.0,
          alignment: WrapAlignment.center,
          children: [
            ...tagsToShow.map(
              (tag) => ActionTag(
                tag,
                (tag.tag == 'sfw' || tag.tag == 'nsfw')
                    ? (onTagTap) => {}
                    : (onTagTap) {
                        mediaController.setTag(onTagTap);
                        Get.offAllNamed('/');
                      },
              ),
            ),
          ],
        ),
        if (hasMoreTags)
          TextButton(
            onPressed: () {
              setState(() => _areTagsExpanded = !_areTagsExpanded);
            },
            child: Text(
              _areTagsExpanded
                  ? 'Weniger anzeigen'
                  : 'Alle ${widget.tags.length} Tags anzeigen',
            ),
          ),
      ],
    );
  }
}
