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

class BookmarkButton extends StatefulWidget {
  final bool value;
  final void Function(bool value) onMarkChanged;
  final bool bigGestureSize;
  final bool askForConfirmation;
  const BookmarkButton({
    Key? key,
    required this.value,
    this.bigGestureSize = false,
    required this.onMarkChanged,
    this.askForConfirmation = false,
  }) : 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();
  }

  @override
  Widget build(BuildContext context) {
    return DidvanIconButton(
      gestureSize: widget.bigGestureSize ? 32 : 24,
      icon: _value ? DidvanIcons.bookmark_solid : DidvanIcons.bookmark_regular,
      onPressed: () async {
        bool confirm = false;
        if (widget.askForConfirmation) {
          await ActionSheetUtils.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);
        }
      },
    );
  }
}
