// 此文件不要写具体逻辑，只调用函数
import {
    twitterShortUrl,
    twitterPinLoginToken,
    twitterPinLoginCode,
    onInstalledMid,
    onInstalledUserSet,
    onInstalledCreateTab,
    checkPined,
    popupRePublish,
    setBadgeInfo,
    hideBadge,
    setMessageCount,
    discordLoginCode,
    saveDiscordAuthWindowId,
    onDisconnectHandler,
    getMessageInfo,
    injectExtensionPopup,
    setPopupConfig,
    windwoLoadSetPopupPage,
    setActionPopup
} from "@/logic/background/twitter";

import { PingPong } from "@/logic/background/help";

import {
    facebookShareSuccess
} from "@/logic/background/facebook";

//加载bg.js 执行
setMessageCount();

// 消息通讯
chrome.runtime.onConnect.addListener(function (port) {
    port.onDisconnect.addListener(function () {
        onDisconnectHandler(port);
    });
});

// 当有可用更新时触发
chrome.runtime.onUpdateAvailable.addListener(() => {
    chrome.runtime.reload()
})

chrome.runtime.onInstalled.addListener(onInstalledMethod);

chrome.runtime.onMessage.addListener(onMessageMethod);

// 定时器
chrome.alarms.onAlarm.addListener(function (alarm) {
    // console.log("onAlarm-", alarm);
    switch (alarm.name) {
        case 'denetChromeAlarm':
            getMessageInfo();
            break
        case 'PingPong':
            PingPong()
            break;
    }
});

chrome.action.onClicked.addListener(function(tab) { 
    injectExtensionPopup(tab);
});

chrome.tabs.onActivated.addListener(function(activeInfo) {
    setPopupConfig(activeInfo);
})

function onInstalledMethod() {
    onInstalledCreateTab()
    onInstalledMid()
    onInstalledUserSet()
    
    // pingpang
    chrome.alarms.create('PingPong', {
        //1分鐘之後開始(該值不能小於1) 
        delayInMinutes: 2,
        //開始後每一分鐘執行一次(該值不能小于1) 
        periodInMinutes: 4
    });
}

function onMessageMethod(req, sender, sendResponse) {
    sendResponse('')
    if (req) {
        switch (req.actionType) {
            case "POPUP_LOGIN":
                twitterPinLoginToken();
                break;
            case "POPUP_PUBLISH_TWITTER_RED_PACK":
                popupRePublish(req);
                break;
            case 'CONTENT_GET_PINED':
                checkPined();
                break;
            case 'CONTENT_SET_BADGE':
                setBadgeInfo(req);
                break;
            case 'CONTENT_HIDE_BADGE':
                hideBadge();
                break
            case 'CONTENT_SEND_CODE':
                twitterPinLoginCode(sender, req.code);
            case 'CONTENT_TWITTER_LOGIN':
                if (req.data) {
                    twitterPinLoginToken()
                }
                break
            case 'CONTENT_TWITTER_SHORT_LINK':
                req.arr_url.forEach(item => {
                    if (item) {
                        twitterShortUrl(sender, item)
                    }
                });
                break
            case "CONTENT_SEND_DISCORD_AUTH_CODE":
                discordLoginCode(req, sender);
                break
            case 'RED_PACKET_SAVE_DISCORD_AUTH_WINDOW_ID':
                saveDiscordAuthWindowId(req);
                break;
            case 'CONTENT_FACEBOOK_SHARE_SUCCESS':
                facebookShareSuccess(req, sender);
                break;
            case 'CONTENT_PONG':
                console.log('CONTENT_PONG')
                break
            case 'CONTENT_WINDOW_LOADED_SET_POPUP_PAGE': 
                // windwoLoadSetPopupPage(req, sender);
                break;
            case 'CONTENT_SET_POPUP_CONFIG':
                setActionPopup(req, sender);
                break;
        }
    }
}
