import 'package:flutter/material.dart';

typedef RewardRequest = Future<void> Function(void Function(int sec) grant);

class RewardAddTimeButton extends StatefulWidget {
  final int extraSeconds;
  final bool disabled;
  final RewardRequest onRequestReward;

  const RewardAddTimeButton({
    super.key,
    required this.extraSeconds,
    required this.disabled,
    required this.onRequestReward,
  });

  @override
  State<RewardAddTimeButton> createState() => _RewardAddTimeButtonState();
}

class _RewardAddTimeButtonState extends State<RewardAddTimeButton> {
  bool loading = false;

  void safeGrant(int sec) {
    if (mounted) {
      setState(() => loading = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    final disabled = widget.disabled || loading;
    return GestureDetector(
      onTap: disabled
          ? null
          : () async {
              setState(() => loading = true);
              await widget.onRequestReward(safeGrant);
            },
      child: AnimatedOpacity(
        duration: const Duration(milliseconds: 120),
        opacity: disabled ? 0.55 : 1,
        child: Container(
          height: 54,
          decoration: BoxDecoration(
            color: const Color.fromRGBO(255, 190, 40, 0.92),
            borderRadius: BorderRadius.circular(16),
            border: Border.all(color: const Color.fromRGBO(255, 255, 255, 0.22), width: 2),
          ),
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  loading ? 'LOADING...' : 'ADD TIME +${widget.extraSeconds}s',
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 16,
                    fontWeight: FontWeight.w900,
                    letterSpacing: 1,
                  ),
                ),
                const SizedBox(height: 2),
                const Text(
                  'Watch Rewarded Ad',
                  style: TextStyle(
                    color: Color.fromRGBO(255, 255, 255, 0.92),
                    fontSize: 12,
                    fontWeight: FontWeight.w800,
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
