import 'package:flutter/material.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';

import '../../../../models/search_all_requests_model.dart';
import '../../../app_style/app_color.dart';

class RequestsSearchButtons extends StatefulWidget {
  final Function(SearchAllRequestsBaseModel) onSelect;
  final List<SearchAllRequestsBaseModel> items;
  final SearchAllRequestsBaseModel initialValue;

  const RequestsSearchButtons({Key? key, required this.items, required this.initialValue, required this.onSelect}) : super(key: key);

  @override
  State<RequestsSearchButtons> createState() => _RequestsSearchButtonsState();
}

class _RequestsSearchButtonsState extends State<RequestsSearchButtons> {
  late SearchAllRequestsBaseModel _data;

  @override
  void initState() {
    _data = widget.initialValue;
    super.initState();
  }

  @override
  void didUpdateWidget(covariant RequestsSearchButtons oldWidget) {
    if (widget.initialValue != oldWidget.initialValue) {
      setState(() {
        _data = widget.initialValue;
      });
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 8.toScreenWidth,
      runSpacing: 8.toScreenHeight,
      children: widget.items
          .map(
            (data) => Container(
              padding: EdgeInsets.symmetric(vertical: 8.toScreenHeight, horizontal: 12.toScreenWidth),
              decoration: ShapeDecoration(
                color: AppColor.background(context),
                shape: RoundedRectangleBorder(
                  side: BorderSide(width: 1, color: _data == data ? AppColor.primary10 : AppColor.border2Color(context)),
                  borderRadius: BorderRadius.circular(6),
                ),
                shadows: [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 14, offset: const Offset(0, 0), spreadRadius: 0)],
              ),
              child: data.label!.tinyFont(context).custom(color: _data == data ? AppColor.primary10 : AppColor.filterButtonTextColor(context)),
              // child: data.label!.tinyFont(context).custom(color: AppColor.filterButtonTextColor(context)),
            ).onPress(
              () {
                setState(() {
                  _data = data;
                });
                widget.onSelect(data);
              },
            ),
          )
          .toList(),
    );
  }
}
