import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/models/view/action_sheet_data.dart';
import 'package:didvan/providers/user.dart';
import 'package:didvan/utils/action_sheet.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

class LikedButton extends StatefulWidget {
  final bool value;
  final Color? color;
  final Color? unlikedColor;
  final void Function(bool value) onMarkChanged;
  final bool askForConfirmation;
  final double gestureSize;
  final String type;
  final int itemId;
  final int likes;
  const LikedButton({
    Key? key,
    required this.value,
    required this.onMarkChanged,
    required this.gestureSize,
    required this.type,
    required this.itemId,
    required this.likes,
    this.askForConfirmation = false,
    this.color,
    this.unlikedColor,
  }) : super(key: key);

  @override
  State<LikedButton> createState() => _LikedButtonState();
}

class _LikedButtonState extends State<LikedButton> {
  bool _value = false;

  @override
  void didUpdateWidget(covariant LikedButton oldWidget) {
    _value = widget.value;
    super.didUpdateWidget(oldWidget);
  }

  @override
  void initState() {
    _value = widget.value;
    super.initState();
  }

  late int likes = widget.likes;

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        if (likes != 0)
          SizedBox(
            height: 16,
            child: DidvanText(
              likes.toString(),
              fontSize: 14,
              color: Colors.white,
            ),
          ),
        InkWell(
          onTap: () async {
            bool confirm = false;
            if (widget.askForConfirmation) {
              await ActionSheetUtils(context).openDialog(
                data: ActionSheetData(
                  content: const DidvanText(
                    'آیا می‌خواهید این محتوا از نشان‌ شده‌ها حذف شود؟',
                  ),
                  titleIcon: DidvanIcons.bookmark_regular,
                  titleColor: Theme.of(context).colorScheme.secondary,
                  title: 'تایید عملیات',
                  onConfirmed: () => confirm = true,
                ),
              );
            }
            if (!widget.askForConfirmation || confirm) {
              setState(() {
                _value = !_value;
                if (_value) {
                  likes += 1;
                } else {
                  likes -= 1;
                }
              });
              widget.onMarkChanged(_value);
              UserProvider.changeItemLiked(widget.type, widget.itemId, _value);
            }
          },
          child: SvgPicture.asset(
            _value
                ? 'lib/assets/icons/heart_fill.svg'
                : 'lib/assets/icons/heart2.svg',
            height: 24,
            color: widget.color ??
                (_value
                    ? Theme.of(context).colorScheme.error
                    : widget.unlikedColor),
          ),
        ),
      ],
    );
  }
}
