import { View } from '@tarojs/components'
import type { FC, ReactNode } from 'react'
import classnames from 'classnames'
import Loading from '../loading'
import styles from './index.module.scss'

type ButtonType = 'primary' | 'danger' | 'warning' | 'info'
type ButtonSize = 'normal' | 'small'

interface PropsType {
  size?: ButtonSize
  type?: ButtonType
  round?: boolean // 大圆角
  disabled?: boolean
  plain?: boolean // plain
  circle?: boolean // 小圆角
  children?: ReactNode
  onClick?: Function
  customClassName?: string
  customStyles?: React.CSSProperties
  customTextClassName?: string
  loading?: boolean
}

const NormalButton: FC<PropsType> = (props) => {
  const {
    type = 'primary',
    size = 'normal',
    round = false,
    disabled = false,
    children,
    onClick,
    plain = false,
    circle = false,
    customStyles = {},
    customClassName = '',
    customTextClassName = '',
    loading = false,
  } = props
  const getClassName = () => {
    const classObject = {
      [styles['button--disabled']]: disabled,
      [styles[`button--${size}`]]: size,
      [styles[`button--${type}`]]: type,
      [styles['button--round']]: round,
      [styles['button--plain']]: plain,
      [styles['button--circle']]: circle,
    }
    return classObject
  }

  const handleClick = (event) => {
    if (disabled) {
      return
    }
    onClick && onClick(event)
  }

  return (
    <View className={classnames(styles.button, getClassName(), customClassName)} style={customStyles} onClick={handleClick}>
      <View className={classnames(styles['button--text'], customTextClassName)}>
        <View className={styles.loading}>{loading && <Loading width={30} />}</View>
        {children}</View>
    </View>
  )
}
export default NormalButton
