import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
import React from "react";
import {IconDefinition} from "@fortawesome/fontawesome-svg-core";
import {faSpinner} from "@fortawesome/free-solid-svg-icons";

interface SubmitButtonWLoadingProps {
    callBackAction: () => Promise<void> | void;
    isLoading: boolean;
    text: string;
    loadingText: string;
    icon?: IconDefinition;
}

export default function SubmitButtonWLoading(
    {
        callBackAction,
        isLoading,
        icon,
        text,
        loadingText
    }: SubmitButtonWLoadingProps) {
    return (
        <button
            onClick={callBackAction}
            disabled={isLoading}
            className={`group py-2.5 px-5 rounded-lg font-semibold transition-all flex items-center justify-center gap-2 relative overflow-hidden ${
                isLoading
                    ? 'bg-secondary cursor-not-allowed opacity-75'
                    : 'bg-secondary/80 hover:bg-secondary shadow-md hover:shadow-lg hover:shadow-primary/20 hover:scale-105 border border-secondary/50 hover:border-primary/30'
            }`}
        >
            <span
                className={`flex items-center gap-2 transition-all duration-200 ${isLoading ? 'opacity-0' : 'opacity-100'} text-primary`}>
                {
                    icon &&
                    <FontAwesomeIcon icon={icon} className={'w-4 h-4 transition-transform group-hover:scale-110'}/>
                }
                <span className="text-sm">{text}</span>
            </span>
            {isLoading && (
                <div className="absolute inset-0 flex items-center justify-center bg-secondary/50 backdrop-blur-sm">
                    <FontAwesomeIcon icon={faSpinner} className="w-4 h-4 text-primary animate-spin"/>
                    <span className="ml-3 text-primary text-sm font-medium">
                        <span className="hidden sm:inline">
                            {loadingText}
                        </span>
                        <span className="sm:hidden">
                            {loadingText}
                        </span>
                    </span>
                </div>
            )}
        </button>
    )
}
