// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';

class DynamicFormRatingWidget extends StatefulWidget {
  DynamicFormRatingWidget(
      {super.key, required this.field,this.onChanged, this.selectedRate = 0});
  FormFieldState field;
  int selectedRate = 0;
  final void Function(int selectedLenght)? onChanged;
  @override
  State<DynamicFormRatingWidget> createState() =>
      _DynamicFormRatingWidgetState();
}

class _DynamicFormRatingWidgetState extends State<DynamicFormRatingWidget> {
  int selectedRate= 0;
  @override
  void initState() {
    // TODO: implement initState
    selectedRate= widget.selectedRate;
  }
  @override
  Widget build(BuildContext context) {
    return StarsWidget(
      selectedColor: Colors.amber,
      selectedLength: selectedRate,
      unselectedColor: Colors.grey,
      length: 5,
      iconSize: 50,
      onChanged: (count) {
        setState(() {
          selectedRate = count;
          widget.onChanged!(selectedRate);
        });
      },
    );
    ;
  }
}

class StarsWidget extends StatefulWidget {
  final int length;
  final String? title;
  final bool enabled;
  final double iconSize;
  final int selectedLength;
  final Color? selectedColor;
  final Color? unselectedColor;
  final void Function(int selectedLenght)? onChanged;

  const StarsWidget({
    Key? key,
    this.title,
    required this.length,
    this.enabled = true,
    this.selectedLength = 0,
    this.selectedColor,
    this.unselectedColor,
    this.onChanged,
    this.iconSize = 28,
  })  : assert(length > 0),
        assert(length >= selectedLength),
        super(key: key);

  @override
  _StarsWidgetState createState() => _StarsWidgetState();
}

class _StarsWidgetState extends State<StarsWidget> {
  @override
  Widget build(BuildContext context) {
    var selectedLength = widget.selectedLength;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        if (widget.title != null)
          Padding(
            padding: const EdgeInsets.only(bottom: 5),
            child: Text(
              widget.title!,
              style: Theme.of(context).textTheme.headline6,
            ),
          ),
        Row(
          mainAxisSize: MainAxisSize.min,
          children: List.generate(
            widget.length,
            (index) {
              final selected = selectedLength > index;
              return GestureDetector(
                onTap: () {
                  if (widget.enabled) {
                    var isRepeat = selectedLength != index + 1;
                    if (isRepeat) {
                      if (selectedLength == 1 && index == 0) {
                        setState(() => selectedLength = 0);
                      } else {
                        setState(() => selectedLength = index + 1);
                      }
                      if (widget.onChanged != null) {
                        widget.onChanged?.call(selectedLength);
                      }
                    }
                  }
                },
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 2),
                  child: Icon(
                    Icons.star_rounded,
                    size: widget.iconSize,
                    color: selected
                        ? (widget.selectedColor ??
                            Theme.of(context).colorScheme.secondary)
                        : widget.unselectedColor ??
                            Theme.of(context).textTheme.headline5?.color,
                  ),
                ),
              );
            },
          ),
        ),
      ],
    );
  }
}
