import classNames from 'classnames';
import React from 'react';
import { IDiv, JDatomExtentionSet } from '../../types/interface';
import { IconSize } from '../../types/enum';
import { iconSizeClass, JDatomClasses } from '../../utils/autoClasses';
import { IMG_REPO } from '../../types/const';
import IconUploader, { IUploadIconProp } from "../iconUploader/IconUploader"

const DEFAULT_IMG = IMG_REPO + 'default/default_profile.jpg';

export interface Iprops extends IDiv, JDatomExtentionSet, IUploadIconProp {
  /** 이미지 URL*/
  img?: string;
  /** 사이즈*/
  size?: IconSize;
  /** 모드 edit 는 파일 업로드 같이 가능*/
  mode?: 'edit' | 'view';
  isError?: boolean;

}

export const Avatar: React.FC<Iprops> = ({
  img,
  className,
  uploader,
  mode = 'view',
  size,
  isError,
  hide,
  hover,
  mb,
  mr,
  z,
  ...props
}) => {
  if (!uploader && mode === 'edit') {
    console.warn('JDAvatar:: you should provide uploaderHOok');
  }

  const classes = classNames('avatar JDwaves-effect', className, {
    'avatar--error': isError,
    'avatar--edit': mode === 'edit',
    ...iconSizeClass('avatar', size),
    ...JDatomClasses({
      hide,
      hover,
      mb,
      mr,
      z
    }),
  });

  const localImg = uploader?.localFiles[0]?.base64;
  const urlImg = img;
  const profileImg = localImg || urlImg || DEFAULT_IMG;

  const circleSize = parseFloat(size || '1em') * 2 + 'em';

  const profileStyle = {
    width: circleSize,
    height: circleSize,
    backgroundImage: `url(${profileImg})`,
  };


  return (
    <div
      role="button"
      tabIndex={0}
      className={classes}
      style={profileStyle}
      {...props}
    >
      {mode === 'edit' && (
        <IconUploader uploader={uploader} />
      )}
    </div>
  );
};

export default Avatar;
