'use client';
import React from 'react';
import { IIconButtonProps, ITextButtonProps, TButtonVariant } from './button';
import { Icon } from '../icons';
import Link from 'next/link';

const Button: React.FC<ITextButtonProps> = ({
  children,
  loading = false,
  disabled = false,
  endIcon,
  className,
  link,
  variant = 'solid',
  onClick,
  ...props
}) => {
  const wrapperClass = `group relative h-12 cursor-pointer rounded-[0.5625rem] p-[0.125rem] flex w-fit ${className || ''} ${variant === 'solid' ? 'bg-primary' : ''}`;

  return link ? (
    <Link href={link} className={wrapperClass} {...props}>
      <ButtonBody
        {...props}
        loading={loading}
        endIcon={endIcon}
        variant={variant}
      >
        {children}
      </ButtonBody>
    </Link>
  ) : (
    <button
      disabled={disabled || loading}
      {...props}
      onClick={onClick}
      className={wrapperClass}
    >
      <ButtonBody
        {...props}
        loading={loading}
        endIcon={endIcon}
        variant={variant}
      >
        {children}
      </ButtonBody>
    </button>
  );
};

const variantClassNames = (variant: TButtonVariant) =>
  variant === 'solid'
    ? 'flex items-center justify-center rounded-lg bg-white text-gray-500'
    : 'text-primary text-base hover:underline';

const ButtonBody: React.FC<ITextButtonProps> = ({
  children,
  loading,
  endIcon,
  variant = 'solid',
}) => (
  <div className="flex">
    {loading ? (
      <svg
        className="text-primary mr-3 -ml-1 h-5 w-5 animate-spin"
        xmlns="http://www.w3.org/2000/svg"
        fill="none"
        viewBox="0 0 24 24"
      >
        <circle
          className="opacity-25"
          cx="12"
          cy="12"
          r="10"
          stroke="currentColor"
          strokeWidth="4"
        ></circle>
        <path
          className="opacity-75"
          fill="currentColor"
          d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l2-2.647z"
        ></path>
      </svg>
    ) : null}

    <div className={`px-3 ${variantClassNames(variant)}`}>{children}</div>

    {endIcon && !loading ? (
      <span className="flex items-center justify-center p-3">
        <Icon name={endIcon} className="text-white" />
      </span>
    ) : null}
  </div>
);

export default Button;

export const IconButton: React.FC<IIconButtonProps> = ({
  children,
  loading = false,
  disabled = false,
  size = 'medium',
  color = 'primary',
  className,
  onClick,
  ...props
}) => {
  const colorClass =
    color === 'primary' ? 'bg-primary text-white' : 'bg-gray-300 text-gray-500';

  const sizeClass =
    size === 'large' ? 'w-12 h-12' : size === 'small' ? 'w-8 h-8' : 'w-10 h-10';

  return (
    <button
      className={`group relative flex cursor-pointer items-center justify-center rounded-[0.5625rem] ${colorClass} ${sizeClass} ${className || ''} `}
      disabled={disabled || loading}
      {...props}
      onClick={onClick}
    >
      {loading ? (
        <svg
          className="text-primary mr-3 -ml-1 h-5 w-5 animate-spin"
          xmlns="http://www.w3.org/2000/svg"
          fill="none"
          viewBox="0 0 24 24"
        >
          <circle
            className="opacity-25"
            cx="12"
            cy="12"
            r="10"
            stroke="currentColor"
            strokeWidth="4"
          ></circle>
          <path
            className="opacity-75"
            fill="currentColor"
            d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l2-2.647z"
          ></path>
        </svg>
      ) : null}

      <div className={`flex items-center justify-center text-center`}>
        {children}
      </div>
    </button>
  );
};
