import 'package:didvan/config/design_config.dart';
import 'package:didvan/config/theme_data.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

class DidvanButton extends StatelessWidget {
  final VoidCallback? onPressed;
  final String? title;
  final double? width;
  final ButtonStyleMode style;
  final bool enabled;
  final double? height;
  final Color? color;
  final String? imagepath;
  const DidvanButton({
    Key? key,
    this.onPressed,
    this.title,
    this.style = ButtonStyleMode.primary,
    this.enabled = true,
    this.width,
    this.height = 53,
    this.color,
    this.imagepath,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Color foregroundColor = Colors.black;
    Color backgroundColor = Colors.black;
    switch (style) {
      case ButtonStyleMode.primary:
        backgroundColor = color ?? Theme.of(context).colorScheme.primary;
        foregroundColor = Theme.of(context).colorScheme.white;
        break;
      case ButtonStyleMode.secondary:
        backgroundColor = Theme.of(context).colorScheme.secondCTA;
        foregroundColor = Theme.of(context).colorScheme.text;
        break;
      case ButtonStyleMode.flat:
        if (DesignConfig.isDark) {
          backgroundColor = Theme.of(context).colorScheme.surface;
          foregroundColor = color ?? Theme.of(context).colorScheme.white;
        } else {
          backgroundColor = Theme.of(context).colorScheme.surface;
          foregroundColor = color ?? Theme.of(context).colorScheme.primary;
        }
        break;
      default:
    }
    return SizedBox(
      height: height,
      width: width ?? double.infinity,
      child: MaterialButton(
        shape: RoundedRectangleBorder(
          borderRadius: DesignConfig.highBorderRadius,
          side: style == ButtonStyleMode.flat
              ? BorderSide(color: foregroundColor)
              : BorderSide.none,
        ),
        color: backgroundColor,
        disabledColor: Theme.of(context).colorScheme.disabledBackground,
        onPressed: !enabled
            ? null
            : () {
                FocusScope.of(context).unfocus();
                onPressed?.call();
              },
        child: _childBuilder(foregroundColor, context),
      ),
    );
  }

  Widget? _childBuilder(Color color, context) {
    if (title != null) {
      return FittedBox(
        fit: BoxFit.scaleDown,
        child: Row(
          children: [
            if (imagepath != null)
            Row(
              children: [
                SvgPicture.asset("$imagepath"),
                const SizedBox(width: 5,)
              ],
            ),
            DidvanText(
              title!,
              color: enabled ? color : Theme.of(context).colorScheme.disabledText,
            ),
          ],
        ),
      );
    }
    return null;
  }
}
