import 'package:flutter/material.dart';
import 'package:remever/common/resources.dart';
import 'package:remever/components/extensions/context.dart';
import 'package:remever/components/extensions/state.dart';

class PrimaryButton extends StatefulWidget {
  const PrimaryButton({
    required this.child,
    required this.onTap,
    this.color = AppColors.primary,

    super.key,
    this.height = 52,
    this.width = double.infinity,
  });

  final Widget child;
  final double height;
  final double width;
  final Function() onTap;
  final Color color;

  @override
  State<PrimaryButton> createState() => _PrimaryButtonState();
}

class _PrimaryButtonState extends State<PrimaryButton> {
  bool isLoading = false;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () async {
        safeSetState(() => isLoading = !isLoading);
        await widget.onTap();
        safeSetState(() => isLoading = !isLoading);
      },
      child: SizedBox(
        height: widget.height.h,
        width: widget.width,
        child: DecoratedBox(
          decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(16)).r,
            color: widget.color,
          ),
          child: Center(
            child:
                isLoading
                    ? const CircularProgressIndicator(
                      color: AppColors.bg,
                      backgroundColor: Colors.transparent,
                    )
                    : widget.child,
          ),
        ),
      ),
    );
  }
}
