<!-- 页面组件 -->

<script setup lang="ts">
    import { defaultLegendConfig } from './config'
    import { useShare } from '@/hooks/page/useShare'
    import type { MapboxConfig } from '@/components/Map/Mapbox'
    import { LegendWidget, useLegendWidget } from '@/components/Map/Widgets/Legend'
    import { TimeBarWidget, formatTime, useTimeBarWidget } from '@/components/Map/Widgets/TimeBarWidget'
    import { BottomBarWidget, useBottomBarWidget } from '@/components/Map/Widgets/BottomBar'
    import { SwitchControlWidget, useSwitchControlWidget } from '@/components/Map/Widgets/SwitchControl'

    useShare()

    // 页面参数
    // const param = reactive({
    //     query: {},
    //     filter: {},
    // })

    // 地图配置
    const config: MapboxConfig = reactive({
        // 说明: 地图数据来源标注展示
        attribution: {
            text: '湖南省气象台',
            // align: 'bottom-left',
        },
        style: {
            // 说明: 根据每个页面的 widget 布局情况，可能需要适当调整地图的中心位置，让界面显示效果更好
            center: [111.6, 26.170844],
        },
    })

    // 顶部时间轴小部件
    const [registerTimeBarWidget, { getTime, setTime, getCheckedOption }] = useTimeBarWidget({
        show: true,
        align: 'left',
        readonly: false,
        label: {
            options: [
                {
                    text: '小时级',
                    value: 'hour',
                    format: 'M月D日H时',
                    timeType: 4,
                    checked: true,
                },
                {
                    text: '分钟级',
                    value: 'minute',
                    format: 'M月D日H时m分',
                    timeType: 5,
                },
            ],
            onChange: ({ option, value }) => {
                console.log(
                    '[TimeBarWidget] ChangeOption',
                    option,
                    value.map((item) => formatTime(item)),
                )
            },
        },
        time: {
            type: 'range',
            timeType: 4,
            format: 'M月D日H时',
            onChange: ({ option, value }) => {
                console.log(
                    '[TimeBarWidget] ChangeTime',
                    option,
                    value.map((item) => formatTime(item)),
                )
            },
        },
        buttons: [
            {
                icon: 'order',
                label: '列表',
                onClick: (e, { option, value }) => {
                    console.log(
                        '[TimeBarWidget] Button Click',
                        e,
                        option,
                        value.map((item) => formatTime(item)),
                    )
                },
            },
        ],
    })

    // 前后切换小部件
    const [registerSwitchControlWidget] = useSwitchControlWidget({
        show: true,
        prev: () => {
            const option = getCheckedOption()
            const time = getTime()
            console.log(
                '[SwitchControlWidget] prev',
                option,
                time.map((item) => formatTime(item)),
            )

            setTime(time.map((item) => item.subtract(1, 'hours')))
            Message.toast('prev')
        },
        next: () => Message.toast('next'),
    })

    // 图例小部件
    const [registerLegendWidget, { toggleExpand: toggleLegendWidgetExpand }] = useLegendWidget({
        show: true,
        expand: true,
        title: defaultLegendConfig.title,
        option: defaultLegendConfig.option,
    })

    // 底部 Bar 小部件
    const [registerBottomBarWidget, { height }] = useBottomBarWidget({
        show: true,
        expand: true,
        showExpandButton: true,
        height: 150,
        maxHeight: 240,
    })

    function testPackUp() {
        toggleLegendWidgetExpand()
    }
</script>

<template>
    <view class="page h-100vh bg-white">
        <!-- 地图组件 -->
        <Mapbox :config="config" />

        <!-- 地图上方所有小部件 -->
        <view class="widgets">
            <!--  -->
            <TimeBarWidget @register="registerTimeBarWidget" />

            <!-- 前后切换小部件 -->
            <SwitchControlWidget @register="registerSwitchControlWidget" />

            <!-- 图例小部件 -->
            <LegendWidget @register="registerLegendWidget" />

            <!-- 底部 Bar 小部件 -->
            <BottomBarWidget @register="registerBottomBarWidget">
                <!-- 内容 Slot -->
                <view class="c-coolGray" @tap="testPackUp">底部交互控件/展示内容</view>
            </BottomBarWidget>
        </view>
    </view>
</template>

<style lang="scss" scoped>
    .widgets {
        // 覆盖图例小部件的默认样式
        :deep(.legend) {
            bottom: calc(30rpx + v-bind(height));

            // 如果底部使用 bottom-left attribution 则需要加上 80rpx，默认显示在右侧，不会冲突
            // bottom: calc(30rpx + 80rpx + v-bind(height));
        }

        // 覆盖底部 Bar 小部件的默认样式
        // :deep(.bottom-bar) {
        // }

        // 前后切换小部件
        // :deep(.switch-control) {
        // }
    }

    .page {
        // #ifdef H5
        :deep(.mapboxgl-ctrl-bottom-right) {
            bottom: calc(85rpx + v-bind(height));
        }
        :deep(.mapboxgl-ctrl-bottom-left) {
            bottom: calc(85rpx + v-bind(height));
        }
        // #endif
        // #ifdef APP-PLUS
        :deep(.mapboxgl-ctrl-bottom-right) {
            bottom: calc(5rpx + v-bind(height));
        }
        :deep(.mapboxgl-ctrl-bottom-left) {
            bottom: calc(5rpx + v-bind(height));
        }
        // #endif
    }
</style>
