import React, { createRef } from 'react';
import ReactDOM from 'react-dom';
import MessageHub from '../message/message';

let ref: any;

const mount = () => {
    let kwaiNotificationMount = document.getElementById('kwaiNotificationMount');
    ref = createRef();
    if (!kwaiNotificationMount) {
        document.body.insertAdjacentHTML('beforeend', '<div id="kwaiNotificationMount"></div>');
        kwaiNotificationMount = document.getElementById('kwaiNotificationMount');
        ReactDOM.render(<MessageHub ref={ref} type="notification" />, kwaiNotificationMount);
    } else {
        ReactDOM.unmountComponentAtNode(kwaiNotificationMount);
        ReactDOM.render(<MessageHub ref={ref} type="notification" />, kwaiNotificationMount);
    }
};

mount();

export default {
    info: function(title: string, desc: string, timeout = 3000, showProgress = false) {
        return (ref.current as any)(desc, { timeout, type: 'info', title, showProgress });
    },
    error: function(title: string, desc: string, timeout = 3000, showProgress = false) {
        return (ref.current as any)(desc, { timeout, type: 'error', title, showProgress });
    },
    success: function(title: string, desc: string, timeout = 3000, showProgress = false) {
        return (ref.current as any)(desc, { timeout, type: 'success', title, showProgress });
    },
    warning: function(title: string, desc: string, timeout = 3000, showProgress = false) {
        return (ref.current as any)(desc, { timeout, type: 'warning', title, showProgress });
    },
    plain: function(title: string, desc: string, timeout = 3000, showProgress = false) {
        return (ref.current as any)(desc, { alternativeStyle: true, timeout, title, showProgress });
    },
    info2: function(title: string, desc: string, timeout = 3000, showProgress = false) {
        return (ref.current as any)(desc, { alternativeStyle: true, timeout, type: 'info', title, showProgress });
    },
    error2: function(title: string, desc: string, timeout = 3000, showProgress = false) {
        return (ref.current as any)(desc, { alternativeStyle: true, timeout, type: 'error', title, showProgress });
    },
    success2: function(title: string, desc: string, timeout = 3000, showProgress = false) {
        return (ref.current as any)(desc, { alternativeStyle: true, timeout, type: 'success', title, showProgress });
    },
    warning2: function(title: string, desc: string, timeout = 3000, showProgress = false) {
        return (ref.current as any)(desc, { alternativeStyle: true, timeout, type: 'warning', title, showProgress });
    },
};
