import React, { Component, ReactElement, isValidElement } from 'react';
import cls from 'classnames';
import { CommonProps } from '../types';
import { MessageQuickProps, MessageProps as NextMessageProps } from '@alifd/next/types/message';
import { Message as NextMessage } from '@alifd/next';

interface MessageProps extends NextMessageProps, CommonProps {
  bgcolor?: string;
}

function isMessageQuickProps(config: any): config is MessageQuickProps {
  return typeof config === 'object' && !isValidElement(config);
}

class Message extends Component<MessageProps> {
  static wrapMethod = (type?: 'notice' | 'success' | 'warning' | 'error' | 'help' | 'loading' | 'hide') => (
    config?: string | MessageQuickProps | ReactElement,
  ) => {
    if (isMessageQuickProps(config)) {
      const { closeable, content, className } = config;
      if (type) {
        // @ts-ignore
        config.type = type;
      }

      config.className = cls(className, {
        'next-close-add': closeable === true,
        'next-with-content': content,
      });
    }
    const ret = NextMessage[type](config);
    return ret;
  };

  static notice = Message.wrapMethod('notice');
  static show = Message.wrapMethod();
  static success = Message.wrapMethod('success');
  static warning = Message.wrapMethod('warning');
  static error = Message.wrapMethod('error');
  static help = Message.wrapMethod('help');
  static loading = Message.wrapMethod('loading');
  static hide = Message.wrapMethod('hide');

  classNameDefine = () => {
    let className_new;
    const {
      shape,
      prefix = 'next-',
      bgcolor = 'colorful',
      className,
    } = this.props;

    if (shape === 'toast') {
      const { bgcolor = 'white' } = this.props;
      className_new = cls(className, {
        [`${prefix}bg-white`]: bgcolor === 'white',
        [`${prefix}bg-colorful`]: bgcolor === 'colorful',
      });
    }

    className_new = cls(className, {
      [`${prefix}bg-white`]: bgcolor === 'white',
      [`${prefix}bg-colorful`]: bgcolor === 'colorful',
    });

    return className_new;
  };

  render() {
    const {
      shape,
      prefix = 'next-',
      ...otherProps
    } = this.props;

    return (
      <NextMessage
        {...otherProps}
        shape={shape}
        prefix={prefix}
        className={this.classNameDefine()}
      />
    );
  }
}

export default Message;
