<template>
    <AppHeader v-show="!fscChecked" :project="project" :show-adjust="showAdjust" @update="onPointsUpdate" />
    <article>
        <main>
            <div class="split" v-if="source">
                <iframe ref="sourceFrame" :src="sourceURL" frameborder="0" @load="onLoadSource"></iframe>
                <div class="tools" v-show="!showAdjust && !fscChecked && (dbsChecked || (!target && !bimChecked))">
                    <div class="item-date">
                        <calendar name="source" :count="scenes.length" :value="sourceDate" :highlighted="sourceDays" @selected="onSelected" @pick="onPickDate" @prev="onPrevDate" @next="onNextDate"></calendar>
                    </div>
                    <div class="item-mode" v-if="source.type == 2">
                        <div class="iconfont icon-show_roaming" :class="{ active: mode == 0 }" @click="onModeChange(0)"></div>
                        <div class="iconfont icon-show_plane" :class="{ active: mode == 1 }" @click="onModeChange(1)"></div>
                    </div>
                </div>
                <div class="points" v-if="showAdjust">
                    <div :class="{ active: points.p1 }">
                        <i class="iconfont" :class="[points.p1 ? 'icon-positioning01' : 'icon-positioning02']"></i>
                        <span>P1</span>
                    </div>
                    <div :class="{ active: points.p2 }">
                        <i class="iconfont" :class="[points.p2 ? 'icon-positioning01' : 'icon-positioning02']"></i>
                        <span>P2</span>
                    </div>
                </div>
            </div>
            <div class="split" v-if="target">
                <iframe ref="targetFrame" :src="targetURL" frameborder="0" @load="onLoadTarget"></iframe>
                <div class="tools" v-show="!fscChecked && !bimChecked">
                    <div class="item-date target">
                        <calendar name="target" :value="targetDate" :highlighted="targetDays" @selected="onSelected" @pick="onPickDate" @prev="onPrevDate" @next="onNextDate"></calendar>
                    </div>
                </div>
                <div class="points" v-if="showAdjust">
                    <div :class="{ active: points.p1 }">
                        <i class="iconfont" :class="[points.p1 ? 'icon-positioning01' : 'icon-positioning02']"></i>
                        <span>P1</span>
                    </div>
                    <div :class="{ active: points.p2 }">
                        <i class="iconfont" :class="[points.p2 ? 'icon-positioning01' : 'icon-positioning02']"></i>
                        <span>P2</span>
                    </div>
                </div>
            </div>
            <div class="model" v-show="!showAdjust">
                <div class="bim" :class="{ active: bimChecked, disable: project && !project.bimData }" v-show="!fscChecked">
                    <div @click="onBimChecked">
                        <i class="iconfont icon-BIM"></i>
                        <span>BIM</span>
                    </div>
                </div>
                <div class="dbs" :class="{ active: dbsChecked, disable: scenes.length < 2 && !bimChecked }" @click="onDbsChecked" v-show="!fscChecked">
                    <i class="iconfont icon-split_screen"></i>
                    <span>分屏</span>
                </div>
                <div class="fsc" :class="{ active: fscChecked }" @click="onFscChecked">
                    <i class="iconfont" :class="[fscChecked ? 'icon-full_screen_selected' : 'icon-full_screen_normal']"></i>
                    <span>全屏</span>
                </div>
            </div>
        </main>
        <Toast v-if="showBimTips" type="warn" content="未发现BIM文件" :close="() => (showBimTips = false)" />
        <Toast v-if="showDbsTips" type="warn" content="未发现对比场景" :close="() => (showDbsTips = false)" />
    </article>
</template>

<script setup>
import { ref, onMounted, computed, nextTick } from 'vue'
import { http } from '@/utils/request'
import browser from '@/utils/browser'
import Toast from '@/components/dialog/Toast'
import AppHeader from '@/components/header'
import Calendar from '@/components/calendar'
import sync, { loadSourceScene, loadTargetScene, setPanoWithBim} from '@/utils/sync'

// 是否BIM模式
const showBim = ref(browser.urlHasValue('bim'))
// 是否校准模式
const showSplit = ref(browser.urlHasValue('split'))
const showAdjust = ref(browser.urlHasValue('adjust'))

const bimChecked = ref()
const dbsChecked = ref(null)
const fscChecked = ref(null)

const datepickName = ref(null)

const sourceFrame = ref(null)
const targetFrame = ref(null)

const mode = ref(0)
const source = ref(null)
const target = ref(null)
const project = ref(null)
const points = ref({ p1: false, p2: false })

const showBimTips = ref(false)
const showDbsTips = ref(false)

const scenes = computed(() => {
    if (!project.value) {
        return []
    }
    return project.value.sceneList.map(item => {
        return {
            num: item.num,
            type: item.type,
            createTime: item.createTime,
        }
    })
})
const sourceURL = computed(() => {
    if (bimChecked.value && !dbsChecked.value) {
        return `smart-bim.html?m=${project.value.bimData.bimOssFilePath}`
    }

    if (source.value.type < 2) {
        // 看看、看见场景
        return `smart-kankan.html?m=${source.value.num}&dev`
    } else {
        // 深时场景
        return `smart-laser.html?m=${source.value.num}&dev`
    }
})
const targetURL = computed(() => {
    if (bimChecked.value) {
        return `smart-bim.html?m=${project.value.bimData.bimOssFilePath}`
    }

    if (source.value.type < 2) {
        // 看看、看见场景
        return `smart-kankan.html?m=${target.value.num}&dev`
    } else {
        // 深时场景
        return `smart-laser.html?m=${target.value.num}&dev`
    }
})
const sourceDate = computed(() => {
    if (source.value) {
        console.log(source.value.createTime.toDate())
        return source.value.createTime.toDate()
    }
})

const targetDate = computed(() => {
    if (target.value) {
        return target.value.createTime.toDate()
    }
})

const sourceDays = computed(() => {
    let dates = []
    if (datepickName.value == 'source') {
        if (dbsChecked.value) {
            // 分屏模式
            if (bimChecked.value) {
                // BIM模式
                dates = scenes.value.map(item => item.createTime.toDate())
            } else {
                // 非BIM模式
                dates = scenes.value.filter(item => item.createTime != target.value.createTime).map(item => item.createTime.toDate())
            }
        } else {
            // 非分屏模式
            dates = scenes.value.map(item => item.createTime.toDate())
        }
    }
    return {
        dates: dates,
    }
})

const targetDays = computed(() => {
    let dates = []
    if (datepickName.value == 'target') {
        dates = scenes.value.filter(item => item.createTime != source.value.createTime).map(item => item.createTime.toDate())
    }
    return {
        dates: dates,
    }
})

const onLoadSource = () => {
    if (bimChecked.value && !dbsChecked.value) {
        // BIM单屏模式
        return
    }

    loadSourceScene(sourceFrame, source.value.type < 2 ? 'kankan' : 'laser')
}
const onLoadTarget = () => {
    if (bimChecked.value) {
        loadTargetScene(targetFrame, 'bim')
    } else {
        loadTargetScene(targetFrame, target.value.type < 2 ? 'kankan' : 'laser', mode.value)
    }
}

const onModeChange = targetMode => {
    window.Log('changeMode:' + targetMode, '#3cffff')
    if (sync.sourceInst) {
        sync.sourceInst.loaded.then(sdk => sdk.scene.changeMode(targetMode))
        mode.value = targetMode
    }
}
const onPickDate = name => {
    datepickName.value = name
}

const onSelected = data => {
    if (!data.payload) {
        return
    }
    let { name, payload } = data
    let date = payload.format('YYYY-mm-dd')
    let dates = (name == 'source' ? sourceDays : targetDays).value.dates.map(item => item.format('YYYY-mm-dd'))

    if (dates.indexOf(date) != -1) {
        let time = payload.format('YYYY-mm-dd HH:MM')
        let find = scenes.value.find(c => c.createTime.indexOf(time) != -1)
        if (find) {
            if (name == 'source') {
                if (find.num != source.value.num) {
                    source.value = find
                }
            } else {
                if (find.num != target.value.num) {
                    target.value = find
                }
            }
        }
    }

    datepickName.value = null
}

const onPrevDate = name => {
    let scene = null
    if (name == 'source') {
        scene = source
    } else {
        scene = target
    }
    let index = scenes.value.findIndex(item => item.num == scene.value.num)
    if (index == -1) {
        return
    }
    if (--index == -1) {
        index = scenes.value.length - 1
    }

    if (target.value) {
        // 分屏模式判断
        if (name == 'source') {
            if (scenes.value[index].createTime == target.value.createTime) {
                index--
            }
        } else {
            if (scenes.value[index].createTime == source.value.createTime) {
                index--
            }
        }
        if (index == -1) {
            index = scenes.value.length - 1
        }
    }

    scene.value = scenes.value[index]
}
const onNextDate = name => {
    let scene = null
    if (name == 'source') {
        scene = source
    } else {
        scene = target
    }
    let index = scenes.value.findIndex(item => item.num == scene.value.num)
    if (index == -1) {
        return
    }
    if (++index > scenes.value.length - 1) {
        index = 0
    }

    if (target.value) {
        // 分屏模式判断
        if (name == 'source') {
            if (scenes.value[index].createTime == target.value.createTime) {
                index++
            }
        } else {
            if (scenes.value[index].createTime == source.value.createTime) {
                index++
            }
        }

        if (index > scenes.value.length - 1) {
            index = 0
        }
    }

    scene.value = scenes.value[index]
}

// bim点击
const onBimChecked = () => {
    if (!project.value || !project.value.bimData) {
        showBimTips.value = true
        return
    }
    if (bimChecked.value) {
        bimChecked.value = false
        if (dbsChecked.value) {
            // 如果没有多场景数据，取消分屏
            if (scenes.value.length < 2) {
                onDbsChecked()
                return
            }

            // 判断是否分屏状态
            let index = scenes.value.findIndex(item => item.num == source.value.num)
            if (index == -1) {
                return
            }
            if (++index > scenes.value.length - 1) {
                index = 0
            }
            target.value = scenes.value[index]
        }
    } else {
        bimChecked.value = true
    }
}

// 分屏点击
const onDbsChecked = () => {
    if (!dbsChecked.value && scenes.value.length < 2 && !bimChecked.value) {
        showDbsTips.value = true
        return
    }
    dbsChecked.value = !dbsChecked.value
    if (dbsChecked.value) {
        if (bimChecked.value) {
            // BIM分屏
            source.value = scenes.value[0]
            target.value = project.value.bimData
        } else {
            // 四维看看、激光场景分屏
            let index = scenes.value.findIndex(item => item.num == source.value.num)
            if (index == -1) {
                return
            }
            if (++index > scenes.value.length - 1) {
                index = 0
            }
            target.value = scenes.value[index]
        }
    } else {
        target.value = null
        //targetApp = null
        views.clear()
    }
}

// 全屏点击
const onFscChecked = () => {
    let element = document.documentElement

    fscChecked.value = !fscChecked.value

    if (fscChecked.value) {
        if (element.requestFullscreen) {
            element.requestFullscreen()
        } else if (element.webkitRequestFullScreen) {
            element.webkitRequestFullScreen()
        } else if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen()
        } else if (element.msRequestFullscreen) {
            element.msRequestFullscreen()
        }
    } else {
        if (document.exitFullscreen) {
            document.exitFullscreen()
        } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen()
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen()
        } else if (document.msExitFullscreen) {
            document.msExitFullscreen()
        }
    }
}

const onPointsUpdate = type => {
    points.value[type] = true
}

onMounted(() => {
    const num = browser.valueFromUrl('m') || ''
    const projectId = browser.valueFromUrl('projectId') || 1
    http.get(`smart-site/project/info?projectId=${projectId}`)
        .then(response => {
            if (response.success) {
                if (response.data) {
                    if (response.data.panos) {
                        try {
                            response.data.panos = JSON.parse(response.data.panos)
                            points.value.p1 = true
                            points.value.p2 = true
  
                            setPanoWithBim(response.data.panos)
                        
                        } catch (error) {
                            console.error(error)
                        }
 
                    }
                    project.value = response.data
                    if (project.value.sceneList.length) {
                        if (num) {
                            source.value = project.value.sceneList.find(c => c.num == num)
                        }
                        if (!source.value) {
                            source.value = project.value.sceneList[0]
                        }
                        if (showAdjust.value || showSplit.value) {
                            onBimChecked()
                            nextTick(() => onDbsChecked())
                        } else if (showBim.value) {
                            onBimChecked()
                        }
                    }
                }
            } else {
                alert('获取数据失败')
            }
        })
        .catch(() => {
            alert('服务器连接失败')
        })
})
</script>

<style lang="scss" scoped>
article {
    display: flex;
    width: 100%;
    height: 100%;
    overflow: hidden;
}
aside {
    width: 160px;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
    h4 {
        font-size: 16px;
        text-align: center;
    }
    ul {
        margin-top: 20px;
    }
    li {
        margin: 0;
        padding: 0;
        font-size: 16px;
        margin-left: 20px;
        cursor: pointer;
        &:hover,
        &.active {
            color: #00c8af;
        }
    }
}
main {
    flex: 1;
    width: 100%;
    height: 100%;
    position: relative;
    display: flex;
    &.full {
        .split {
            width: 50%;
        }
    }
    iframe {
        position: absolute;
        left: 0;
        top: 0;
        z-index: 1000;
        width: 100%;
        height: 100%;
    }
    .split {
        margin-left: 2px;
        width: 100%;
        height: 100%;
        overflow: hidden;
        position: relative;
        &:first-child,
        &:last-child {
            margin-left: 0;
        }

        .points {
            position: absolute;
            left: 50%;
            top: 64px;
            z-index: 9999;
            display: flex;
            transform: translateX(-50%);

            div {
                margin-left: 20px;
                width: 70px;
                height: 88px;
                background: rgba(27, 27, 28, 0.8);
                box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.25), inset 0px 0px 0px 2px rgba(255, 255, 255, 0.1);
                border-radius: 8px 8px 8px 8px;
                opacity: 1;
                border: 1px solid #000000;
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                &.active {
                    color: #0076f6;
                }
                i {
                    font-size: 24px;
                }
                span {
                    font-size: 16px;
                    margin-top: 10px;
                }
            }
        }
    }
    .model {
        position: absolute;
        left: 50px;
        bottom: 40px;
        z-index: 1000;
        display: flex;
        flex-direction: column;
        > div {
            cursor: pointer;
            width: 50px;
            height: 50px;
            margin-top: 16px;
            background: rgba(27, 27, 28, 0.8);
            box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.25);
            border-radius: 47px 47px 47px 47px;
            border: 1px solid #000000;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;

            > div {
                width: 100%;
                height: 100%;
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
            }
            &.active {
                color: #0076f6;
            }
            &.disable {
                opacity: 0.5;
            }
            span {
                font-size: 12px;
                padding-top: 1px;
                transform: scale(0.8);
            }
        }
    }
    .tools {
        position: absolute;
        width: 100%;
        bottom: 40px;
        z-index: 2000;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #fff;
        pointer-events: none;
        > div {
            pointer-events: all;
        }

        .item-mode {
            height: 50px;
            background: rgba(27, 27, 28, 0.8);
            box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.25);
            border-radius: 47px 47px 47px 47px;
            border: 1px solid #000000;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-left: 10px;
            margin-right: 10px;
            font-size: 16px;
            padding: 0 16px;
            div {
                cursor: pointer;
                font-size: 18px;
            }
            div:last-child {
                margin-left: 20px;
            }
            div.active {
                color: #0076f6;
            }
        }
    }
}
</style>
<style lang="scss">
#app {
    background-color: rgba(0, 0, 0, 0.8);
    display: flex;
    flex-direction: column;
}
.vuejs3-datepicker__calendar {
    background: rgba(27, 27, 28, 0.8);
    box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.25);
    border-radius: 4px 4px 4px 4px;
    opacity: 1;
    border: 1px solid #000000;
    filter: blur(undefinedpx);
    color: #fff;
}
.vuejs3-datepicker__calendar-topbar {
    display: none !important;
}
.vuejs3-datepicker__calendar header .up:not(.disabled):hover {
    background: rgba(0, 0, 0, 0.3);
    color: #fff;
}

.vuejs3-datepicker__calendar header .prev:after {
    border-left: 1px solid #fff;
    border-bottom: 1px solid #fff;
}
.vuejs3-datepicker__calendar header .prev:not(.disabled):hover {
    background: rgba(0, 0, 0, 0.3);
}

.vuejs3-datepicker__calendar header .next:after {
    border-top: 1px solid #fff;
    border-right: 1px solid #fff;
}
.vuejs3-datepicker__calendar header .next:not(.disabled):hover {
    background: rgba(0, 0, 0, 0.3);
}
.highlighted {
    background: #4ebde1 !important;
}
.selected {
    background: #0076f6 !important;
}
</style>
