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/flutter_svg.dart';

class BookmarkButton extends StatefulWidget {
  final bool value;
  final Color? color;
  final Color? unbookmarkedColor;
  final void Function(bool value) onMarkChanged;
  final bool askForConfirmation;
  final double gestureSize;
  final String type;
  final int itemId;
  final String? svgIconOn;
  final String? svgIconOff;

  const BookmarkButton({
    Key? key,
    required this.value,
    required this.onMarkChanged,
    required this.gestureSize,
    required this.type,
    required this.itemId,
    this.askForConfirmation = false,
    this.color,
    this.unbookmarkedColor,
    this.svgIconOn,
    this.svgIconOff,
  }) : super(key: key);

  @override
  State<BookmarkButton> createState() => _BookmarkButtonState();
}

class _BookmarkButtonState extends State<BookmarkButton> {
  bool _value = false;

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

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

  void _handleTap() 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;
      });
      widget.onMarkChanged(_value);
      UserProvider.changeItemMark(widget.type, widget.itemId, _value);
    }
  }

  Color? _getColor(BuildContext context) {
    if (_value) {
      return widget.color;
    } else {
      return widget.unbookmarkedColor;
    }
  }

  @override
  Widget build(BuildContext context) {
    print("BookmarkButton build - value: $_value");

    final iconOn = widget.svgIconOn ?? 'lib/assets/icons/bookmark_on.svg';
    final iconOff = widget.svgIconOff ?? 'lib/assets/icons/bookmark_off.svg';

    return IconButton(
      iconSize: widget.gestureSize,
      onPressed: _handleTap,
      icon: AnimatedSwitcher(
        duration: const Duration(milliseconds: 300),
        transitionBuilder: (Widget child, Animation<double> animation) {
          return ScaleTransition(scale: animation, child: child);
        },
        child: SvgPicture.asset(
          _value ? iconOn : iconOff,
          key: ValueKey('bookmark_$_value'),
          width: widget.gestureSize,
          height: widget.gestureSize,
          color: _getColor(context),
        ),
      ),
    );
  }
}
