import { forwardRef, MouseEventHandler, useState } from "react"
import { SwitchProps } from "./interface"
import {
  applySwitch,
  applySwitchDot,
  applySwitchText,
  applySwitchIcon,
} from "./style"
import { applyBoxStyle, deleteCssProps } from "../model/src/style"
import { isObject } from "@/redux/utils"
import { useTheme } from "@mui/material"

export const Switch = forwardRef<HTMLButtonElement, SwitchProps>(
  (props, ref) => {
    const {
      // colorScheme = "blue",
      disabled,
      size = "medium",
      checkedText = "",
      uncheckedText = "",
      checkedIcon,
      uncheckedIcon,
      checked: propsChecked,
      defaultChecked,
      onChange,
      onClick,
      ...restProps
    } = props
const theme = useTheme()
    const [checked, setChecked] = useState<boolean>(defaultChecked ?? false)
    const mergedChecked = propsChecked !== void 0 ? propsChecked : checked

    const onHandleClick: MouseEventHandler<HTMLButtonElement> = (event) => {
      if (propsChecked === void 0) {
        setChecked(!mergedChecked)
      }
      onChange && onChange(!mergedChecked, event)
      onClick && onClick(event)
    }

    const renderText = mergedChecked ? checkedText : uncheckedText

    return (
      <button
        css={[
          applySwitch("blue", mergedChecked, size),
          applyBoxStyle(props,theme),
        ]}
        ref={ref}
        onClick={onHandleClick}
        disabled={disabled}
        type="button"
        {...deleteCssProps(restProps)}
      >
        <div css={applySwitchDot(size, mergedChecked, disabled ?? false)}>
          {(checkedIcon || uncheckedIcon) && (
            <div css={applySwitchIcon}>
              {mergedChecked ? checkedIcon : uncheckedIcon}
            </div>
          )}
        </div>
        <div css={applySwitchText(mergedChecked, size)}>
          {isObject(renderText) ? (
            <div css={applySwitchIcon}>{renderText}</div>
          ) : (
            renderText
          )}
        </div>
      </button>
    )
  },
)

Switch.displayName = "Switch"
