import React from 'react';
import { LoginView } from '@/components/loginView';
import { DenetWebview } from '@/components/webview';
import { useLoginCom } from '@/components/useLogin';
import messageCenter from '@/utils/messageCenter';

interface Props {
    navigation: any;
}

let webref: any;

const onMessage = async (event: any) => {
    let { info, data } = (event.nativeEvent.data && JSON.parse(event.nativeEvent.data)) || {};
    switch (info.actionType) {
        case 'IFRAME_GET_EXTENSION_STORGE_DATA':
            info.target = webref;
            messageCenter.send({ info, data });
            break;
    }
}

const Message = (props: Props) => {
    const { isLogin, goTwitterAuthorize } = useLoginCom(props);

    if (isLogin === false) {
        return (
            <LoginView goLogin={goTwitterAuthorize}></LoginView>
        );
    } else {
        return (
            <DenetWebview
                onMessageHandle={onMessage}
                refHandle={r => (webref = r)}
                uri="http://localhost:3000/nav-message">
            </DenetWebview>
        );
    }
};

export default Message;
