<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">
            <template v-if="showMask">
                <div class="mask" @click="confirmStatus">
                    <img class="luck" :src=" require('@/assets/svg/icon-post-lock.svg') " />
                    <div class="btn">
                        <img class="img" v-if="detail.nftProjectIcon" :src=" detail.nftProjectIcon " />
                        <div class="font">Available for holders of {{detail.nftProjectName}} NFT</div>
                    </div>
                </div>
                <img class="mask_bg" v-if="detail.linkImagePath" :src=" detail.linkImagePath " />
            </template>
            <iframe :src="state.iframe_url" frameborder="0" allow="camera *;microphone *"></iframe>
        </div>
    </div>
</template>
<script setup>
import { reactive, ref } from "vue";
import { ElMessage } from 'element-plus'
import { unlockNftCert } from '@/http/toolBoxApi'
import { getChromeStorage, sendChromeTabMessage } from "@/uilts/chromeExtension";
import "element-plus/es/components/message/style/css";

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

let detail = ref(null)
let nftAuthINfo = ref(null)
let showMask = ref(false)
let postId = ref('')

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
                nftAuthINfo.value = req.data.nftAuthINfo;
                showMask.value = req.data.showMask;
                detail.value = req.data.detail;
                postId.value = req.data.postId;
                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
        case 'FINISH_ToolBox_By_Nft':
            hideMask(req.data)
            break;
    }
    sendResponse && sendResponse();
})


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,
            nftAuthINfo: nftAuthINfo.value,
            showMask: showMask.value,
            detail: detail.value,
            postId: postId.value,
        }
    })
}

const clickClose = () => {
    sendClose()
}

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

const confirmStatus = () => {
    getChromeStorage('userInfo', (_userInfo) => {
        if (!_userInfo) {
            chrome.runtime.sendMessage(
                { actionType: "POPUP_LOGIN", data: "" },
                (response) => {
                    console.log("res", response);
                }
            )
        } else {
            if (nftAuthINfo.value && nftAuthINfo.value?.certStatus === 1) {
                succBack()
            } else {
                sendChromeTabMessage({
                    actionType: 'Set_ToolBox_By_Nft',
                    data: {
                        postId: postId.value,
                    }
                })
            }
        }
    })
}

const succBack = () => {
    ElMessage({
        message: `NFT validated!`,
        type: 'success'
    })
    showMask.value = false;
    // 记录解锁
    unlockNftCert({
        params: {
            postId: postId.value,
        }
    })
}

const hideMask = (data) => {
    if (data && data.post_Id && data.post_Id === postId.value) {
        succBack()
    }
}

</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 {
        position: relative;
        width: 100%;
        height: calc(100% - 40px);
        background: #686868;
        display: flex;
        align-items: center;
        justify-content: center;

        .mask {
            position: absolute;
            z-index: 2;
            display: flex;
            cursor: pointer;
            align-items: center;
            justify-content: center;
            flex-direction: column;
            width: 100%;
            height: 100%;
            background-color: rgba($color: #000000, $alpha: .8);
            .luck {
                width: 100px;
                height: 100px;
            }
            .btn {
                display: flex;
                align-items: center;
                justify-content: center;
                width: 345px;
                height: 70px;
                cursor: pointer;
                margin-top: 12px;
                border-radius: 100px;
                background: #1D9BF0;
                .img {
                    overflow: hidden;
                    width: 35px;
                    height: 35px;
                    margin-right: 16px;
                    border-radius: 4px;
                }
                .font {
                    width: 188px;
                    color: #fff;
                    font-weight: 700;
                    font-size: 16px;
                    line-height: 19px;
                    letter-spacing: 0.3px;
                }
            }
        }

        .mask_bg {
            position: absolute;
            z-index: 1;
            width: 100%;
            height: 100%;
            object-fit: contain;
            background-color: #000000;
        }

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