<!-- 气温页面组件 -->

<script setup lang="ts">
    import { hourTemLegendConfig } from './config'
    import { useShare } from '@/hooks/page/useShare'
    import { buildGeoJSONSourceDataUrl, useMapbox } from '@/components/Map/Mapbox/hook'
    import { LegendWidget, useLegendWidget } from '@/components/Map/Widgets/Legend'
    import { SwitchWidget, useSwitchWidget } from '@/components/Map/Widgets/Switch'
    import { TimeBarWidget, formatTime, useTimeBarWidget } from '@/components/Map/Widgets/TimeBar'
    import { ToolBoxWidget, useToolBoxWidget } from '@/components/Map/Widgets/ToolBox'
    import { BottomBarWidget, useBottomBarWidget } from '@/components/Map/Widgets/BottomBar'
    import type { MapboxInstance } from '@/components/Map/Mapbox'
    import { HandlerUtil } from '@/components/Map/Mapbox'

    useShare()

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

    const baseUrl = 'http://qks.straw.yiring.com:40051/tbm/api'
    const token = `68b919d2-2ea0-4ad3-9521-9e624e1563ac`

    // 地图组件
    const [registerMap, map] = useMapbox({
        style: {
            center: [111.6, 26.170844],
        },
        onLoaded: (map: MapboxInstance, data: Recordable) => {
            console.log('✨✨✨ Map Loaded', map, data)

            // 模拟色斑叠加数据源
            map.setGeoJSONSourceForRequest(
                'tem-splot',
                buildGeoJSONSourceDataUrl({
                    url: `${baseUrl}/live/tem/getTemStain`,
                    params: {
                        stationType: 1,
                        liveQueryType: 'ONE_HOUR_TEM',
                        Authorization: token,
                    },
                }),
                (response) => response.body.jsonObject,
            )

            // 模拟色斑叠加图层
            map.addLayer(
                {
                    id: 'tem-splot',
                    type: 'fill',
                    source: 'tem-splot',
                    paint: {
                        'fill-color': {
                            type: 'identity',
                            property: 'color',
                        },
                        'fill-opacity': 1,
                    },
                },
                'fill-placeholder',
            )

            // 模拟数值叠加数据源
            map.setGeoJSONSourceForRequest(
                'tem-text',
                buildGeoJSONSourceDataUrl({
                    url: `${baseUrl}/live/tem/getTemList`,
                    params: {
                        stationType: 1,
                        liveQueryType: 'ONE_HOUR_TEM',
                        Authorization: token,
                    },
                }),
                // 此处使用
                (response) => {
                    const features = response.body.liveVos.map(function (item) {
                        item.key = '气温'
                        item.suffix = '°C'
                        return HandlerUtil.createDefaultGeoJSONFeatureData(item)
                    })
                    return HandlerUtil.createDefaultGeoJSONSymbolSourceData(features)
                },
            )

            // 模拟数值叠加图层
            map.addLayer({
                id: 'tem-text',
                type: 'symbol',
                source: 'tem-text',
                layout: {
                    'text-field': '{value}',
                    'text-size': 14,
                    'text-offset': [0, 1.2],
                    'icon-image': 'point.normal',
                    'icon-anchor': 'top',
                    'icon-size': 0.4,
                },
                paint: {
                    'text-color': '#3766fd',
                    'text-halo-color': '#fff',
                    'text-halo-width': 1,
                },
            })

            map.on('click', 'tem-text', function (e) {
                console.log('🚀🚀🚀', e)
            })
        },
    })

    // 顶部时间轴小部件
    const [registerTimeBarWidget, { setTime, getTimeBarValue }] = useTimeBarWidget({
        show: true,
        align: 'left',
        readonly: false,
        time: {
            type: 'single',
            timeType: 4,
            format: 'MM月DD日HH时',
            onChange: ({ option, value }) => {
                console.log(
                    '[TimeBarWidget] ChangeTime',
                    option,
                    value.map((item) => formatTime(item)),
                )
            },
        },
    })

    // 左侧工具栏小部件
    const [registerToolBoxWidget, { setProps: setToolBoxProps }] = useToolBoxWidget({
        show: true,
        expand: true,
        showExpandButton: true,
        tools: [
            {
                key: 'filter',
                align: 'top',
                buttons: [
                    {
                        name: '过滤',
                        icon: '/static/icons/toolbox/filter.png',
                        type: 'filter',
                        handle: () => {},
                    },
                    {
                        name: '图层',
                        icon: '/static/icons/toolbox/menu.png',
                        type: 'select',
                        multiple: true,
                        options: [
                            {
                                text: '色斑',
                                value: 'contour',
                                checked: true,
                            },
                            {
                                text: '数值',
                                value: 'text',
                            },
                        ],
                        handle: () => {},
                    },
                    // todo 初始版本先定为湖南省
                    /* {
                        name: '区域',
                        icon: '/static/icons/toolbox/location.png',
                        type: 'select',
                        multiple: false,
                        options: [
                            {
                                text: '湖南省',
                                value: '430000',
                                checked: true,
                            },
                            {
                                text: '长沙市',
                                value: '430100',
                            },
                            {
                                text: '湘潭市',
                                value: '430300',
                            },
                        ],
                        handle: () => {},
                    }, */
                    {
                        name: '站点',
                        icon: '/static/icons/toolbox/station.png',
                        type: 'select',
                        multiple: false,
                        handle: () => {},
                    },
                ],
            },
            {
                key: 'action',
                align: 'bottom',
                buttons: [
                    {
                        name: '排名',
                        icon: '/static/icons/toolbox/rank.png',
                        type: 'button',
                        handle: () => {},
                    },
                ],
            },
        ],
    })

    // 前后切换小部件
    const [registerSwitchWidget, { setProps: setSwitchProps }] = useSwitchWidget({
        show: true,
        prev: () => {
            const { option, value } = getTimeBarValue()
            console.log(
                '[SwitchControlWidget] prev',
                option,
                value.map((item) => formatTime(item)),
            )

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

    // 图例小部件
    const [registerLegendWidget, { setProps: setLegendProps }] = useLegendWidget(hourTemLegendConfig)

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

    watch(
        () => height.value,
        (value) => {
            console.log('[useBottomBarWidget] height', value)

            setSwitchProps({ bottom: value })
            setLegendProps({ bottom: value })
            setToolBoxProps({ bottom: value })

            // 当底部 Bar 小部件高度变化时，重新设置地图的中心点，使界面显示效果更好
            if (map.isReady.value) {
                if (value === 150) {
                    map.flyTo({
                        center: [111.6, 26.770844],
                        speed: 0.2,
                        essential: true,
                    })
                } else {
                    map.flyTo({
                        center: [111.6, 26.170844],
                        speed: 0.2,
                        essential: true,
                    })
                }
            }
        },
    )
</script>

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

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

            <!-- 左侧工具栏小部件 -->
            <ToolBoxWidget @register="registerToolBoxWidget" />

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

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

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

<style lang="scss" scoped>
    // .widgets {
    // 覆盖图例小部件的默认样式
    // :deep(.legend) {
    // 现已调整为通过 watch 监听 height 值变化，动态设置底部 Bar 小部件的 bottom 值，这样方便由组件内部决定样式变化
    // 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>
