<template>
    <div class="denet-toolbox">
        <div class="head">
            <span></span>
            <div>
                <!-- 缩小 -->
                <img v-if="state.status == '全屏'" :src="require('@/assets/svg/icon-iframe-small.svg')" alt class="full"
                    @click="clickFull" />
                <img v-else :src="require('@/assets/svg/icon-iframe-full.svg')" alt class="full" @click="clickFull" />
                <!-- 关闭 -->
                <img :src="require('@/assets/svg/icon-iframe-close.svg')" alt class="fixed" @click="clickClose" />
            </div>
        </div>
        <div class="content">
            <iframe :src="state.iframe_url" frameborder="0" allow="camera *;microphone *"></iframe>
        </div>
    </div>
</template>
<script setup>
import { reactive } from "vue";
import { sendChromeTabMessage } from "@/uilts/chromeExtension";

let state = reactive({
    status: '固定右上角', // 全屏
    iframe_url: '',
    tweetId: ''
})

chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {

    switch (req.actionType) {
        // 事件传输
        case 'Set_ToolBox_Fixed':
            if (req.data.type == '全屏' || req.data.type == '固定右上角') {
                state.status = req.data.type
                if (state.tweetId != req.data.tweetId) {
                    state.tweetId = req.data.tweetId
                }
                if (state.iframe_url != req.data.iframe_url) {
                    state.iframe_url = req.data.iframe_url
                }
            }

            break
    }
})


const clickFull = () => {
    if (state.status == '固定右上角') {
        state.status = '全屏'
        changeFull()
    } else {
        state.status = '固定右上角'
        changeFixed()
    }
}

const changeFull = () => {
    sendChromeTabMessage({
        actionType: 'Set_ToolBox_Fixed',
        data: {
            type: '全屏',
            iframe_url: state.iframe_url,
            tweetId: state.tweetId,
        }
    })
}

const changeFixed = () => {
    sendChromeTabMessage({
        actionType: 'Set_ToolBox_Fixed',
        data: {
            type: '固定右上角',
            iframe_url: state.iframe_url,
            tweetId: state.tweetId,
        }
    })
}

const clickClose = () => {
    sendClose()

}
const sendClose = () => {
    sendChromeTabMessage({
        actionType: 'Set_ToolBox_Fixed',
        data: {
            type: '关闭',
            iframe_url: state.iframe_url,
            tweetId: state.tweetId,
        }
    })
    state.iframe_url = ''
    state.tweetId = ''
}


</script>

<style lang="scss" scoped>
.denet-toolbox {
    width: 100%;
    height: 100%;
    filter: drop-shadow(0px 4px 20px rgba(0, 0, 0, 0.2));
    border-radius: 12px;
    overflow: hidden;

    .head {
        width: 100%;
        height: 40px;
        background: #373737;
        display: flex;
        align-items: center;
        justify-content: space-between;

        span {
            color: #FFFFFF;
            font-style: normal;
            font-weight: 500;
            font-size: 14px;
            margin-left: 16px;
        }

        div {
            display: flex;
            justify-content: center;
        }

        svg {
            width: 20px;
            height: 20px;
            cursor: pointer;

        }

        .full {
            margin-right: 16px;
        }

        .fixed {
            margin-right: 20px;
        }
    }

    .content {
        width: 100%;
        height: calc(100% - 40px);
        background: #686868;
        display: flex;
        align-items: center;
        justify-content: center;

        iframe {
            width: 100%;
            height: 100%;
        }
    }
}
</style>