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: (_data == data ? 12 : 8).toScreenWidth),
              foregroundDecoration: _data != data
                  ? null
                  : ShapeDecoration(
                      color: (context.isDark ? AppColor.neutral30 : AppColor.neutral50).withOpacity(0.1),
                      shape: RoundedRectangleBorder(
                        side: BorderSide(width: 1, color: AppColor.blueStatus(context)),
                        borderRadius: BorderRadius.circular(7),
                      ),
                    ),
              decoration: ShapeDecoration(
                color: _data == data ? AppColor.selectedButtonColor(context) : AppColor.unSelectedButtonColor(context),
                shape: RoundedRectangleBorder(
                  side: _data == data ? BorderSide(width: 1, color: AppColor.blueStatus(context)) : BorderSide.none,
                  borderRadius: BorderRadius.circular(7),
                ),
                shadows: [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 14, offset: const Offset(0, 0), spreadRadius: 0)],
              ),
              child: data.label!.tinyFont(context).custom(color: AppColor.filterButtonTextColor(context)),
            ).onPress(
              () {
                setState(() {
                  _data = data;
                });
                widget.onSelect(data);
              },
            ),
          )
          .toList(),
    );
  }
}
