import 'package:flutter/material.dart';
import 'package:guru_utils/datetime/datetime_utils.dart';
import 'package:guru_widgets/theme/guru_theme.dart';
import 'package:guru_utils/feedback/feedback_manager.dart';

class SingleTapInkWell extends StatefulWidget {
  final int throttleTime;
  final VoidCallback onTap;
  final Widget child;
  final bool singleTap;

  const SingleTapInkWell({required this.onTap, required this.child, this.throttleTime = 1500, this.singleTap = false});

  @override
  State<StatefulWidget> createState() {
    return _SingleTapInkWell();
  }
}

class _SingleTapInkWell extends State<SingleTapInkWell> {
  int _latestTapTimestamp = 0;

  @override
  Widget build(BuildContext context) {
    final guruTheme = GuruTheme.of(context);
    return InkWell(
      onTap: (){
        if (_latestTapTimestamp != 0 && widget.singleTap) {
          return;
        }

        if (guruTheme.feedbackCapabilities.canPerform()) {
          FeedbackManager.instance.perform(FeedbackOccasion.clickButton);
        }
        final now = DateTimeUtils.currentTimeInMillis();
        if (now - _latestTapTimestamp > widget.throttleTime) {
          widget.onTap.call();
          _latestTapTimestamp = now;
        }
      },
      child: widget.child,
    );
  }
}
