<template>
    <div class="denet-toolbox">
        <div class="head">
            <span></span>
            <div>
                <!-- 缩小 -->
                <icon-svg v-if="state.status == '全屏'" :icon="'缩小'" @clickStop="clickFull" :class="'full'"></icon-svg>
                <icon-svg v-else :icon="'放大'" @clickStop="clickFull" :class="'full'"></icon-svg>

                <!-- 关闭 -->
                <icon-svg :icon="'关闭'" @clickStop="clickClose" :class="'fixed'"></icon-svg>
            </div>
        </div>
        <div class="content">
            <iframe :src="state.iframe_url" frameborder="0" allow="camera *;microphone *"></iframe>
        </div>
    </div>
</template>
<script setup>
import { onMounted, reactive, ref } from "vue";
import { sendEventInfo } from "@/uilts/event";
import IconSvg from '@/view/components/icon-svg.vue'

let state = reactive({
    status: '固定', // 全屏
    iframe_url: '',
    tweetId: ''
})
let dom_fixed = null

window.addEventListener("onEvent", e => {
    let info = e.detail
    switch (info.event_type) {
        // 固定
        case 'ToolBox_To_Fixed':
            // 替换
            if (state.iframe_url || state.tweetId) {
                sendClose()
            }
            state.iframe_url = info.data.iframe_url
            state.tweetId = info.data.tweetId
            break
        case 'ToolBox_To_Full':
            if (state.iframe_url || state.tweetId) {
                sendClose()
            }
            state.status = '全屏'
            state.iframe_url = info.data.iframe_url
            state.tweetId = info.data.tweetId
            break
    }
});


const clickFull = () => {
    if (state.status == '固定') {
        state.status = '全屏'
        changeFull()
    } else {
        state.status = '固定'
        changeFixed()
    }
}
onMounted(() => {
    dom_fixed = $('#denet-tool-box-fixed')
})

const changeFull = () => {
    dom_fixed.style.cssText = `
    width:100%;
    height: 100%;
    position: fixed;
    right: 0px;
    top: 0px;`
}

const changeFixed = () => {
    dom_fixed.style.cssText = `
    width: 505px;
    height: 545px;
    position: fixed;
    right: 10px;
    top: 10px;`
}

const clickClose = () => {
    dom_fixed.style.display = 'none'
    sendClose()

}
const sendClose = () => {
    let url = state.iframe_url
    let tweetId = state.tweetId
    sendEventInfo({
        event_type: 'ToolBox_Fixed_Close',
        data: {
            url,
            tweetId,
        }
    })
    state.iframe_url = ''
    state.tweetId = ''
}


</script>

<style lang="scss">
.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>