// 此文件不要写具体逻辑，只调用函数
// 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,
    loginSuccessHandle,
    showJoinDialog,
    showTwitterPost,
    setTwitterTextarea,
    showGroupTip,
    setTabGroupIframeStyle,
    pageJumpHandler,
    getTweetProfileNavTop,
    setGroupInfo,
    refreshTabGroup,
    groupTipsSelectGroupTab,
    TwitterApiUserByScreenName,
    showPublishDialog
} from "@/logic/content/twitter.js";

import { httpBackToContentCallBack } from '@/uilts/chromeExtension.js'
import {
    hideNFTGroupList,
    setNFTGroupContent,
    setJoinedGroupIframeStyle
} from '@/logic/content/nft';

import {
    initFacebookContent
} from "@/logic/content/facebook.js"
import { toolBox, toolbox_fixed_tweetId } from '@/logic/content/ToolBox.js'

import {
    injectDenet
} from "@/logic/content/denet.js";

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

window.onload = () => {
    injectDenet();
    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':
            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 = '', showJoinGroupFinish } = req.data || {};
            showPopupPage({ path: '/NFT', from, showJoinGroupFinish });
            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':
            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;
        case 'IFRAME_SHOW_JOIN_DIALOG':

            showJoinDialog(req.data)
            break
        case 'IFRAME_SHOW_GROUP_TIP':

            showGroupTip()
            break
        case 'IFRAME_SHOW_POST_DIALOG':

            showTwitterPost(req.data)
            break
        case 'BACK_TWITTER_NFT_POST_PRE':
            setTwitterTextarea(req.data)
            break
        case 'SWITCH_GROUP_STATUS':
            sendResponse('ok')
            groupTipsSelectGroupTab(req.data);
            break
        case 'IFRAME_GROUP_BANNER_GROUP_INFO':

            setGroupInfo(req.data)
            break;
        case 'IFRAME_JOINED_GROUP_SET_STYLE':

            setJoinedGroupIframeStyle(req.data)
            break
        case 'BACK_NFT_PUBLISH_DONE':

            refreshTabGroup()
            break
        case 'IFRAME_API_GET_TWEET_USER_INFO_REQ':

            TwitterApiUserByScreenName(req.data)
            break;
        // 回掉参数
        case 'HTTP_BACK_TO_CONTENT':
            httpBackToContentCallBack(req)
            break
        case 'BG_SHOW_DENET_PUBLISH_DIALOG':
            showPublishDialog();
            break;
        case 'Get_ToolBox_Fixed_TweetId':
            sendResponse(toolbox_fixed_tweetId)
            break
        case 'Set_ToolBox_Fixed':
            toolBox.switchStatus(req)
            break
    }
})