import 'package:flutter/material.dart';
import 'package:syncrow_web/utils/color_manager.dart';

class CounterWidget extends StatefulWidget {
  final int initialCount;
  final ValueChanged<int> onCountChanged;
  final bool isCreate;

  const CounterWidget(
      {Key? key,
      this.initialCount = 0,
      required this.onCountChanged,
      required this.isCreate})
      : super(key: key);

  @override
  State<CounterWidget> createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  late int _counter;

  @override
  void initState() {
    super.initState();
    _counter = widget.initialCount;
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
      widget.onCountChanged(_counter);
    });
  }

  void _decrementCounter() {
    setState(() {
      if (_counter > 0) {
        _counter--;
        widget.onCountChanged(_counter);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      decoration: BoxDecoration(
        color: ColorsManager.counterBackgroundColor,
        borderRadius: BorderRadius.circular(20),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          _buildCounterButton(Icons.remove, _decrementCounter,!widget.isCreate ),
          const SizedBox(width: 8),
          Text(
            '$_counter',
            style: theme.textTheme.bodyLarge
                ?.copyWith(color: ColorsManager.spaceColor),
          ),
          const SizedBox(width: 8),
          _buildCounterButton(Icons.add, _incrementCounter, false),
        ],
      ),
    );
  }

  Widget _buildCounterButton(IconData icon, VoidCallback onPressed, bool isDisabled) {
    return GestureDetector(
      onTap: isDisabled? null: onPressed,
      child: Icon(
        icon,
        color: isDisabled? ColorsManager.spaceColor.withOpacity(0.3): ColorsManager.spaceColor,
        size: 18,
      ),
    );
  }
}
