<template>
    <div class="mode-switch">
        <div class="content">
            <p>{{content}}</p>
            <p>{{contentTip}}</p>
        </div>
        <div class="footer">
            <Button class="btn" type="info" @click="doSure" v-show="isShowSure">{{sureText}}</Button>
            <Button type="error" @click="doCancel">{{cancelText}}</Button>
        </div>
    </div>
</template>

<script setup>
    import { watch, ref } from 'vue'
    import { useVehicleStore } from '../store/VehicleStore';
    import { storeToRefs } from 'pinia';

    let modeTimer = null
    const CLOSE_MODE_TIME = 5 * 1000

    const props = defineProps({
        isShow: Boolean
    })
    const content = ref('')
    const contentTip = ref('')
    const isShowSure = ref(true)
    const sureText = ref('确定')
    const cancelText = ref('取消')
    const step = ref(0)
    const vehicleStore = useVehicleStore()
    const vehicleStoreToRefs = storeToRefs(vehicleStore)
    const { pattern,softDrivingPattern, patternAck, singleTrackAck } = vehicleStoreToRefs

    const emits = defineEmits(['handleSend', 'closeModal'])

    function doSure() {
        let type = null, iscontinue = null
        // 有人——无人
        if (softDrivingPattern.value === 0) {
            if (step.value === 1) {
                type = 0
                content.value = '正在等待机群批准模式切换请求'
                step.value = 2
            } else if (step.value === 3) {
                type = 2
                iscontinue = 1
                content.value = '模式切换审批成功，正在接管无人驾驶'
                step.value = 4
            }
        } else {
            // 有人——无人
            if (step.value === 1) {
                type = 2
                content.value = '正在取消无人驾驶接管'
                step.value = 4
            }
        }
        isShowSure.value = false
        cancelText.value = '取消'
        const changePattern = softDrivingPattern.value === 0 ? 1 : 0
        emits('handleSend', {
            type: '/pattern/req',
            msg: {
                type, 
                pattern: changePattern,
                iscontinue
            }
        })
    }

    function doCancel() {
        if ([2, 3, 4].includes(step.value)) {
            // type === 1:取消云端请求；type === 3:取消车端请求
            let type = step.value === 2 ? 1 : 3
            let iscontinue = null
            // 切换为无人驾驶，并且不继续上一次任务
            if (softDrivingPattern.value === 0 && step.value === 3) {
                iscontinue = 0
                type = 2
                content.value = '模式切换审批成功，正在接管无人驾驶'
                step.value = 4
                isShowSure.value = false
                cancelText.value = '取消'
            }
            const changePattern = softDrivingPattern.value === 0 ? 1 : 0
            emits('handleSend', {
                type: '/pattern/req',
                msg: {
                    type,
                    pattern: changePattern,
                    iscontinue
                }
            })
        } else {
            emits('closeModal')
        }
    }

    function doCloseMode() {
        emits('closeModal')
        if (modeTimer) {
            clearTimeout(modeTimer)
            modeTimer = null
        }
    }

    watch(() => props.isShow, (value) => {
        if (value) {
            step.value = 1
            isShowSure.value = true
            sureText.value = '确定'
            cancelText.value = '取消'
            // 有人——无人
            if (softDrivingPattern.value === 0) {
                const { singleTrackEnable } = singleTrackAck.value || {}
                if (singleTrackEnable) {
                    content.value = '当前处于单机循迹模式，是否切换无人模式'
                    contentTip.value = '(确保当前位置处于路线中)'
                } else {
                    content.value = '切换为无人驾驶模式需要发送机群确认'
                    contentTip.value = ''
                }
            } else {
                content.value = '是否切换为有人驾驶模式'
                contentTip.value = ''
            }
        }
    }, {
        immediate: true
    })

    watch(patternAck, (result) => {
        console.log(result)
        if (!result) return
        contentTip.value = ''
        const { singleTrackEnable } = singleTrackAck.value || {}
        if (modeTimer) {
            clearTimeout(modeTimer)
            modeTimer = null
        }
        const {type, pattern: resultPattern, status} = result
        if (step.value === 2) {
            isShowSure.value = false
            cancelText.value = '确定'
            if (type === 0) {
                switch (status) {
                    case 0:
                        if (singleTrackEnable) {
                            emits('handleSend', {
                                type: '/pattern/req',
                                msg: {
                                    type: 2,
                                    pattern: 1,
                                    iscontinue: 0
                                }
                            })
                        } else {
                            content.value = '是否继续上一次任务（确保车辆位置未变动）'
                            step.value = 3
                            isShowSure.value = true
                            sureText.value = '是'
                            cancelText.value = '否'
                        }
                        break
                    case 1:
                        content.value = '模式切换请求未获得批准'
                        break
                    case 2: 
                        content.value = '请先完成此设备的点检工作'
                        break
                    case 200:
                        content.value = '参数校验失败'
                        break
                }
            } else {
                switch (status) {
                    case 0:
                        content.value = '成功'
                        break
                    case 1:
                        content.value = '失败'
                        break
                    case 200:
                        content.value = '参数校验失败'
                        break
                }
                step.value = 0
                modeTimer = setTimeout(doCloseMode, CLOSE_MODE_TIME)
            }
        } else if (step.value === 4) {
            isShowSure.value = false
            cancelText.value = '确定'
            if (type === 2) {
                switch (status) {
                    case 0:
                        content.value = resultPattern === 1 ? '接管成功' : '取消接管成功'
                        break
                    case 1:
                        content.value = resultPattern === 1 ? '接管失败' : '取消接管失败'
                        break
                    case 200:
                        content.value = '参数校验失败'
                        break
                }
            } else {
                switch (status) {
                    case 0:
                        content.value = '成功'
                        break
                    case 1:
                        content.value = '失败'
                        break
                    case 200:
                        content.value = '参数校验失败'
                        break
                }
            }
            step.value = 0
            modeTimer = setTimeout(doCloseMode, CLOSE_MODE_TIME)
        }
    }, {
        deep: true
    })
</script>

<style lang="less" scoped>
    .mode-switch {
        .content {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-bottom: 30px;
        }
        .footer {
            text-align: end;
            .btn {
                margin-right: 10px;
            }
        }
    }
</style>