// 此文件不要写具体逻辑，只调用函数
// var port = chrome.runtime.connect({
//     name: "hello",
// });

import {
    showGiveDialogHandler,
    showIframeHandler,
    hideIframeHandler,
    showTwitterPublishDialogHandler,
    twitterPublishHandler,
    showPinTips,
    init,
    initExecuteScript,
    changeQueueNum,
    replyHandle,
    noticeBindTweet,
    hideNoticeBindTweet,
    getTweetAuthorByDom,
    facebookReplyTweet,
    doTaskTwitterAPI,
    onTweetReplyClick,
    showNFTSale,
    showBuyNFT,
    hideBuyNFT,
    showPopupPage,
    setPopupConfByPopupPage,
    setTabGroupIframeStyle,
    loginSuccessHandle,
    pageJumpHandler,
    getTweetProfileNavTop
} from "@/logic/content/twitter.js";

import { 
    hideNFTGroupList,
    setNFTGroupContent,
} from '@/logic/content/nft';

import {
    initFacebookContent
} from "@/logic/content/facebook.js"

chrome.storage.onChanged.addListener(changes => {
    initExecuteScript(changes)
})

window.onload = () => {
    init();
    initFacebookContent();
    chrome.runtime.sendMessage({ 
        actionType: "CONTENT_WINDOW_LOADED_SET_POPUP_PAGE", 
        data: { } 
    }, () => { });
};

window.onmessage = (res) => {
    if (res.data && res.data.actionType) {
        switch (res.data.actionType) {
            case "IFRAME_SHOW_IFREME":
                showIframeHandler();
                break;
            case "IFRAME_HIDE_IFREME":
                hideIframeHandler();
                break;
            case "IFRAME_SHOW_TWITTER_PUBLISH_DIALOG":
                showTwitterPublishDialogHandler(res.data.publishRes);
                break;
            case "IFRAME_RED_PACKET_REPLY_CLICK":
                replyHandle(res.data.data || {});
                break;
            case "IFRAME_RED_PACKET_SHOW_BIND_TWEET_NOTICE":
                noticeBindTweet(res.data.data || {});
                break;
            case "IFRAME_CLOSE_BIND_TWEET":
                hideNoticeBindTweet();
                break;
            case "IFRAME_RED_PACKET_GET_TWEET_AUTHOR":
                getTweetAuthorByDom(res.data.data || {});
                break;
            case "IFRAME_RED_PACKET_CHECK_FACEBOOK_REPLY":
                facebookReplyTweet(res.data.data || {});
                break;
            case "IFRAME_RED_PACKET_ON_TWEET_REPLY_CLICK":
                onTweetReplyClick(res.data.data || {});
                break;
            // case 'IFRAME_TWITTER_API_DO_TASK':
            //     doTaskTwitterAPI(res.data)
            //     break
            // case "IFRAME_DO_TASK":
            //     findTweetByIdDoTask(res.data.task_data, res.data.task_type)
            //     break
        }
    }
};


chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
    switch (req.actionType) {
        case 'BG_SHOW_PIN_TIPS':
            showPinTips()
            break
        case "BACK_TWITTER_LOGIN_SUCCESS":
            showGiveDialogHandler();
            break
        case 'BACK_TWITTER_SHORT_URL':
            console.log('BACK_TWITTER_SHORT_URL')
            changeQueueNum(1)
            break
        case 'IFRAME_TWITTER_API_DO_TASK':
            doTaskTwitterAPI(req)
            break
        case 'IFRAME_NFT_SHOW_SALE':
            showNFTSale()
            break
        case 'IFRAME_NFT_GROUP_LIST_HIDE':
            hideNFTGroupList()
            break;
        case 'IFRAME_NFT_GROUP_SET_CONTENT':
            setNFTGroupContent(req.publishRes);
            break;
        case "IFRAME_TWITTER_PUBLISH":
            twitterPublishHandler(req.publishRes);
            break;
        case 'IFRAME_TWITTER_SHOW_POPUP_PAGE':
            let {from = ''} = req.data || {};
            showPopupPage({path: '/NFT', from}); 
            break
        case "IFRAME_TWITTER_SHOW_BUY_NFT":
            showBuyNFT(req.data)
            break
        case 'IFRAME_TWITTER_HIDE_BUY_NFT':
            hideBuyNFT()
            break
        case 'BG_SET_POPUP_CONFIG':
            setPopupConfByPopupPage();
            break
        case 'IFREME_TAB_GROUP_SET_IFRAME_HEIGHT':
            console.log('IFREME_TAB_GROUP_SET_IFRAME_HEIGHT',req)
            setTabGroupIframeStyle(req.data);
            break
        case 'IFREME_TAB_GROUP_CONTENT_GET_NAV_TOP':
            getTweetProfileNavTop(req.data);
        case 'IFRAME_PAGE_JUMP':
            pageJumpHandler(req.data);
            break;
        case 'BG_LOGIN_SET_USERINFO_CB':
            loginSuccessHandle();
            break;
    }
})


