import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:rasadyar_core/presentation/common/app_color.dart';
import 'package:rasadyar_core/presentation/utils/color_utils.dart';

import '../../common/assets.gen.dart';

class RFab extends StatefulWidget {
  final VoidCallback? onPressed;
  Color? foregroundColor;
  Color? backgroundColor;
  Color? disabledForegroundColor;
  Color? disabledBackgroundColor;
  double radius;
  ShapeBorder? shapeBorder;
  Widget? icon;
  bool isLoading;

  @override
  State<RFab> createState() => _RFabState();

  //region Add
  RFab.smallAdd({required VoidCallback? onPressed, Key? key})
    : this.small(
        onPressed: onPressed,
        icon: Assets.vec.addSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.greenNormal,
        key: key,
      );

  RFab.add({required VoidCallback? onPressed, Key? key})
    : this(
        onPressed: onPressed,
        icon: Assets.vec.addSvg.svg(width: 56.w, height: 56.h),
        backgroundColor: AppColor.greenNormal,
        key: key,
      );

  //endregion

  //region Edit
  RFab.smallEdit({required VoidCallback? onPressed, Key? key})
    : this.small(
        onPressed: onPressed,
        icon: Assets.vec.addSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.blueNormal,
        key: key,
      );

  RFab.edit({required VoidCallback? onPressed, Key? key})
    : this(
        onPressed: onPressed,
        icon: Assets.vec.addSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.blueNormal,
        key: key,
      );

  //endregion

  //region delete
  RFab.smallDelete({required VoidCallback? onPressed, Key? key})
    : this.small(
        onPressed: onPressed,
        icon: Assets.vec.trashSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.redNormal,
        key: key,
      );

  RFab.delete({required VoidCallback? onPressed, Key? key})
    : this(
        onPressed: onPressed,
        icon: Assets.vec.trashSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.redNormal,
        key: key,
      );

  //endregion

  //region action
  RFab.smallAction({required VoidCallback? onPressed, Key? key})
    : this.small(
        onPressed: onPressed,
        icon: Assets.vec.scanSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.blueNormal,
        key: key,
      );

  RFab.action({required VoidCallback? onPressed, Key? key})
    : this(
        onPressed: onPressed,
        icon: Assets.vec.scanSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.blueNormal,
        key: key,
      );

  //endregion

  //region filter
  RFab.smallFilter({required VoidCallback? onPressed, Key? key})
    : this.small(
        onPressed: onPressed,
        icon: Assets.vec.scanSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.blueNormal,
        key: key,
      );

  RFab.filter({required VoidCallback? onPressed, Key? key})
    : this(
        onPressed: onPressed,
        icon: Assets.vec.scanSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.blueNormal,
        key: key,
      );

  //endregion

  //region download
  RFab.smallDownload({required VoidCallback? onPressed, Key? key})
    : this.small(
        onPressed: onPressed,
        icon: Assets.vec.downloadSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.blueNormal,
        key: key,
      );

  RFab.download({required VoidCallback? onPressed, Key? key})
    : this(
        onPressed: onPressed,
        icon: Assets.vec.downloadSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.blueNormal,
        key: key,
      );

  //endregion

  //region Excel
  RFab.smallExcel({required VoidCallback? onPressed, Key? key})
    : this.small(
        onPressed: onPressed,
        icon: Assets.vec.excelDownloadSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.greenDark,
        key: key,
      );

  RFab.excel({required VoidCallback? onPressed, Key? key})
    : this(
        onPressed: onPressed,
        icon: Assets.vec.excelDownloadSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.greenDark,
        key: key,
      );

  //endregion

  //region Back
  RFab.smallBack({required VoidCallback? onPressed, Key? key})
    : this.small(
        onPressed: onPressed,
        icon: Assets.vec.arrowLeftSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.blueNormal,
        key: key,
      );

  RFab.back({required VoidCallback? onPressed, Key? key})
    : this(
        onPressed: onPressed,
        icon: Assets.vec.arrowLeftSvg.svg(width: 20, height: 20),
        backgroundColor: AppColor.blueNormal,
        key: key,
      );

  //endregion

  //region General
  RFab.small({
    required this.onPressed,
    required this.icon,
    required this.backgroundColor,
    super.key,
    this.isLoading = false,
  }) : radius = 40.0,
       foregroundColor = Colors.white;

  RFab({
    required this.onPressed,
    required this.icon,
    required this.backgroundColor,
    this.isLoading = false,
    super.key,
  }) : radius = 56.0,
       foregroundColor = Colors.white;

  //endregion
}

class _RFabState extends State<RFab> {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: widget.isLoading == false ? widget.onPressed : null,
      style: ButtonStyle(
        side: WidgetStateProperty.all(BorderSide.none),
        backgroundColor: WidgetStateProperty.resolveWith<Color?>((states) {
          if (states.contains(WidgetState.pressed)) {
            return widget.backgroundColor?.pressedColor ?? AppColor.blueNormalActive;
          } else if (states.contains(WidgetState.hovered)) {
            return widget.backgroundColor?.hoverColor ?? AppColor.blueNormalHover;
          } else if (states.contains(WidgetState.disabled)) {
            return widget.backgroundColor?.disabledColor ?? AppColor.blueNormal.disabledColor;
          }
          return widget.backgroundColor ?? AppColor.blueNormal;
        }),
        foregroundColor: WidgetStateProperty.resolveWith<Color?>((states) {
          if (states.contains(WidgetState.disabled)) {
            return widget.foregroundColor?.disabledColor;
          }
          return widget.foregroundColor;
        }),

        shape: WidgetStatePropertyAll(
          CircleBorder(side: BorderSide(width: 1, color: Colors.transparent)),
        ),
        fixedSize: WidgetStatePropertyAll(Size(widget.radius ?? 56, widget.radius ?? 56)),
        padding: WidgetStatePropertyAll(EdgeInsets.zero),
      ),
      child: widget.isLoading
          ? SizedBox(
              height: widget.radius / 2,
              width: widget.radius / 2,
              child: CircularProgressIndicator(
                strokeWidth: 2.5,
                valueColor: AlwaysStoppedAnimation<Color>(widget.foregroundColor ?? Colors.white),
              ),
            )
          : widget.icon,
    );
  }
}
