<template>
    <div class="denet-toolbox" @click.stop="clickHead" ref="dom_toolbox" :class="{ 'pre-view': pre_view }">
        <div class="head" @click.stop="clickHead">
            <span></span>
            <div v-show="state.show_btn && state.status == ''">
                <icon-svg :icon="'固定'" @clickStop="clickFixed" :class="'fixed'"></icon-svg>
                <icon-svg :icon="'放大'" @clickStop="clickFull" :class="'full'"></icon-svg>
            </div>
        </div>
        <div class="content" v-if="pre_view">
            <iframe :src="iframe_url" frameborder="0"></iframe>
        </div>
        <div class="content" v-else>
            <iframe :src="state.iframe_url" v-show="state.status == ''" ref="dom_iframe" frameborder="0"
                allow="camera *;microphone *"></iframe>
            <!-- 网页错误 -->
            <div class="state" v-show="state.status == '网页错误'">
                <img :src="require('@/assets/img/icon-page-fail.png')" alt />
                <div>Oops, this link is invalid</div>
            </div>

            <!-- 加载 -->
            <div class="state" v-show="state.status == '加载'">
                <img :src="require('@/assets/img/icon-loading-gray.png')" alt class="icon-loading" />
            </div>

            <!-- 关闭 -->
            <div class="state" v-show="state.status == '关闭'">

            </div>
            <!-- 固定右上角 -->
            <div class="state" v-show="state.status == '固定右上角'">
                <img :src="require('@/assets/img/icon-fixed-gray.png')" alt />
                <div>Pinned to the top right</div>
            </div>
        </div>
        <!-- alert -->
        <div class="alert" v-show="state.show_alert">
            <div class="back" @click.stop="clickCancel"></div>
            <div class="confirm">
                <div class="check">
                    <input :id="state.checkbox_id" type='checkbox' v-model="state.checkbox" />
                    <label :for="state.checkbox_id">Don't remind</label>
                </div>

                <div class="title">Web Page Progress May Reset</div>
                <div class="handle">
                    <div class="cancel" @click.stop="clickCancel">Cancel</div>
                    <div class="continue" @click.stop="clickContinue">Continue</div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup>
import { getChromeStorage, setChromeStorage, httpContentToBack, defineProps } from "@/uilts/chromeExtension";
import { guid } from "@/uilts/help";
import { sendEventInfo } from "@/uilts/event";
import { onMounted, reactive, ref } from "vue";
import { $ } from "@/uilts/help";
import IconSvg from '@/view/components/icon-svg.vue'
let dom_toolbox = ref(null)
let dom_iframe = ref(null)
let state = reactive({
    status: '', //
    show_alert: false,
    show_btn: true,
    list: [],
    checkbox: false,
    checkbox_id: `denet-${guid()}`,
    postId: '',
    tweetId: '',
    detail: {},
    handle_type: ''
})

let dom = {}

let props = defineProps({
    pre_view: {
        type: Boolean,
        default: false,
    },
    iframe_url: {
        type: String,
        default: ''
    }
})

window.addEventListener("onEvent", e => {
    let info = e.detail
    switch (info.event_type) {
        // 事件传输
        case 'ToolBox_Fixed_Close':
            if (info.data.tweetId == state.tweetId) {
                state.show_btn = true
                state.status = ''
                state.iframe_url = info.data.url
            }
            break
    }
});
const clickHead = () => {
    // 
}

const clickContinue = () => {
    if (state.checkbox) {
        setChromeStorage({ fullCheck: JSON.stringify({ fullCheck: 1 }) })
    }
    if (state.handle_type == '全屏') {
        handleFull()
    } else {
        handleFixed()
    }
    state.show_alert = false
}
onMounted(() => {
    if (props.pre_view) {
        return
    }
    if (dom_toolbox.value) {
        state.dom_root = dom_toolbox.value.closest('div[data-tweet-id]')
        if (state.dom_root) {
            state.postId = state.dom_root.dataset.postId || ''
            state.tweetId = state.dom_root.dataset.tweetId || ''
        }
    }
    try {
        dom.fixed = $('#denet-tool-box-fixed')
        if (dom.fixed && dom.fixed.style.display == 'block') {
            if (dom.fixed.dataset.tweetId == state.tweetId) {
                state.status = '固定右上角'
                return
            }
        }
    } catch (error) {
        console.log(error)
    }
    getDetail()
})

// detail函数
const getDetail = () => {
    state.status = '加载'
    httpContentToBack({
        url: `/post/getDetail`,
        params: {
            postId: state.postId
        }
    }, (res) => {
        if (res && res.code == 0) {
            state.detail = JSON.parse(res.data.postBizData)
            console.log('postBizData', state.detail)
            // 加载iframe
            let iframe = dom_iframe.value
            // state.detail.convertUrl = 'https://www.bilibili.com'
            // iframe.onerror = () => {
            //     state.status = '网页错误'
            // }
            // iframe.onload = function () {
            //     if (state.status != '固定右上角') {
            //         state.status = ''
            //     }
            // }
            setTimeout(() => {
                state.iframe_url = state.detail.convertUrl
                state.status = ''
            }, 1000)
        } else {
            state.status = '网页错误'
        }
    })
}

const clickCancel = () => {
    state.show_alert = false
}

const clickFixed = () => {
    state.handle_type = '固定'
    getChromeStorage('fullCheck', (res) => {
        if (res && res.fullCheck) {
            // 固定
            handleFixed()
        } else {
            state.show_alert = true
        }
    })
}

// 固定
const handleFull = () => {
    if (state.status || !state.iframe_url) {
        return
    }
    // 切换状态
    state.status = '关闭'
    state.status = '固定右上角'
    // 操作全屏dom
    dom.fixed.style.cssText = `
        width:100%;
        height: 100%;
        position: fixed;
        right: 0px;
        top: 0px;
        display:block;`
    dom.fixed.dataset.tweetId = state.tweetId
    state.show_btn = false
    sendEventInfo({
        event_type: 'ToolBox_To_Full',
        data: {
            iframe_url: state.iframe_url,
            tweetId: state.tweetId,
        }
    })
    // 清除当前iframe src
    state.iframe_url = ''
}

// 全屏
const handleFixed = () => {
    // 切换状态
    state.show_btn = false
    state.status = '固定右上角'
    // 操作全屏dom
    dom.fixed.style.cssText = `
        display:block;
        width: 505px;
        height: 544px;
        position: fixed;
        right: 10px;
        top: 10px;`
    dom.fixed.dataset.tweetId = state.tweetId
    sendEventInfo({
        event_type: 'ToolBox_To_Fixed',
        data: {
            iframe_url: state.iframe_url,
            tweetId: state.tweetId
        }
    })
    // 清除当前iframe src
    state.iframe_url = ''
}

const clickFull = () => {
    state.handle_type = '全屏'
    getChromeStorage('fullCheck', (res) => {
        if (res && res.fullCheck) {
            // 全屏
            handleFull()
        } else {
            state.show_alert = true
        }
    })
}

</script>

<style lang="scss">
.pre-view {
    pointer-events: none;
    cursor: default;
}

.denet-toolbox {
    width: 100%;
    height: 100%;
    filter: drop-shadow(0px 4px 20px rgba(0, 0, 0, 0.2));
    border-radius: 12px;
    overflow: hidden;
    position: relative;

    .alert {
        text-align: center;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;

        .back {
            background: #000000;
            opacity: 0.8;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            cursor: auto;
        }

        .confirm {
            position: absolute;
            width: 355px;
            height: 180px;
            background: #FFFFFF;
            border-radius: 20px;
            top: 173px;
            left: 50%;
            margin-left: -180px;

            .title {
                font-weight: 600;
                font-size: 18px;
                color: #000000;
                margin-bottom: 34px;
            }

            .check {
                color: #899099;
                font-weight: 400;
                font-size: 14px;
                margin: 12px 15px 32px 0;
                text-align: right;
                align-content: center;
                justify-content: flex-end;
                display: flex;
                line-height: 17px;

                input {
                    margin-right: 8px;
                }

                label {
                    line-height: 19px;
                }

            }

            .handle {
                display: flex;
                justify-content: center;
                align-items: center;

                div {
                    font-weight: 600;
                    font-size: 16px;
                    width: 156px;
                    height: 47px;
                    line-height: 47px;
                    cursor: pointer;
                    border-radius: 1000px;
                    user-select: none;
                }

                .cancel {
                    color: #000000;
                    background: rgba(56, 154, 255, 0.01);
                    border: 1px solid #E6E6E6;
                }

                .continue {
                    background: #1D9BF0;
                    font-weight: 600;
                    margin-left: 11px;
                    color: #FFFFFF;
                }
            }
        }
    }

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

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

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

        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%;
        }

        .state {
            text-align: center;

            img {
                margin-bottom: 14px;
            }

            .icon-loading {
                animation: loading 1s infinite linear;
            }

            div {
                margin-bottom: 40px;
                color: #8E8E8E;
                text-align: center;
                font-weight: 500;
                font-size: 22px;
            }
        }

    }
}

@keyframes loading {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}
</style>