/**
 * 设备总览主入口文件
 * 负责整个设备总览模块的初始化和全局函数暴露
 */
var DeviceOverview = (function() {
    'use strict';
    
    // 全局变量（从原HTML中提取）
    var deviceList = [];
    
    /**
     * 初始化设备总览模块
     * @param {Array} devices - 设备列表数据
     * @param {Object} options - 配置选项
     */
    function init(devices, options) {
        deviceList = devices || [];
        
        // 设置全局变量供其他模块使用
        window.deviceList = deviceList;
        window.userRole = options && options.role ? options.role : 'USER';
        
        // 等待layui加载完成后初始化
        layui.use(['form'], function(){
            var form = layui.form;

            // 绑定表单事件
            form.on('select(mapTypeNew)', function(data){
                MapCore.switchMapType(data.value);
            });

            // 初始化地图核心
            MapCore.initialize(deviceList);
            
            // 默认显示所有设备
            document.getElementById('warningFilter').value = 'all';
            SearchFilter.showAllDevices();
        });
    }
    
    // 暴露给HTML使用的全局函数
    
    /**
     * 地图类型变化处理
     */
    function onMapTypeChange() {
        return MapCore.onMapTypeChange();
    }
    
    /**
     * 搜索设备
     */
    function searchDeviceNew() {
        return MapCore.searchDeviceNew();
    }
    
    /**
     * 告警过滤变化处理
     */
    function onWarningFilterChange() {
        return MapCore.onWarningFilterChange();
    }
    
    /**
     * 切换地图功能菜单
     */
    function toggleMapFunctionsMenu() {
        return MapCore.toggleMapFunctionsMenu();
    }
    
    /**
     * 切换设备ID显示
     */
    function toggleDeviceId() {
        return MapCore.toggleDeviceId();
    }
    
    /**
     * 切换集群显示
     */
    function toggleCluster() {
        return MapCore.toggleCluster();
    }
    
    /**
     * 切换测距功能
     */
    function toggleMeasureDistance() {
        return MeasureTools.toggleMeasureDistance();
    }
    
    /**
     * 完成测量
     */
    function finishMeasuring() {
        return MeasureTools.finishMeasuring();
    }
    
    /**
     * 清除测距
     */
    function clearMeasure() {
        return MeasureTools.clearMeasure();
    }
    
    /**
     * 切换天气预报功能
     */
    function toggleWeatherForecast() {
        return WeatherForecast.toggleWeatherForecast();
    }
    
    /**
     * 关闭天气卡片
     */
    function closeWeatherCard() {
        return WeatherForecast.closeWeatherCard();
    }
    
    /**
     * 显示上一个天气预报
     */
    function showPrevForecast() {
        return WeatherForecast.showPrevForecast();
    }
    
    /**
     * 显示下一个天气预报
     */
    function showNextForecast() {
        return WeatherForecast.showNextForecast();
    }
    
    /**
     * 查询设备
     * @param {string} statusType - 状态类型
     */
    function queryDevices(statusType) {
        return SearchFilter.queryDevices(statusType);
    }
    
    /**
     * 定位设备到地图
     * @param {string} deviceId - 设备ID
     * @param {number} latitude - 纬度
     * @param {number} longitude - 经度
     */
    function locateDeviceOnMap(deviceId, latitude, longitude) {
        return SearchFilter.locateDeviceOnMap(deviceId, latitude, longitude);
    }
    
    /**
     * 直接定位设备
     * @param {string} deviceId - 设备ID
     */
    function locateDeviceDirectly(deviceId) {
        return SearchFilter.locateDeviceDirectly(deviceId);
    }
    
    /**
     * 切换地图类型
     * @param {string} mapType - 地图类型
     */
    function switchMapType(mapType) {
        return MapCore.switchMapType(mapType);
    }
    
    // 公开API
    return {
        init: init,
        
        // 地图相关
        onMapTypeChange: onMapTypeChange,
        switchMapType: switchMapType,
        
        // 搜索和过滤
        searchDeviceNew: searchDeviceNew,
        onWarningFilterChange: onWarningFilterChange,
        queryDevices: queryDevices,
        locateDeviceOnMap: locateDeviceOnMap,
        locateDeviceDirectly: locateDeviceDirectly,
        
        // 地图功能
        toggleMapFunctionsMenu: toggleMapFunctionsMenu,
        toggleDeviceId: toggleDeviceId,
        toggleCluster: toggleCluster,
        
        // 测距工具
        toggleMeasureDistance: toggleMeasureDistance,
        finishMeasuring: finishMeasuring,
        clearMeasure: clearMeasure,
        
        // 天气预报
        toggleWeatherForecast: toggleWeatherForecast,
        closeWeatherCard: closeWeatherCard,
        showPrevForecast: showPrevForecast,
        showNextForecast: showNextForecast
    };
})();

// 将主要函数暴露到全局作用域，供HTML中的onclick等使用
window.DeviceOverview = DeviceOverview;
window.onMapTypeChange = DeviceOverview.onMapTypeChange;
window.searchDeviceNew = DeviceOverview.searchDeviceNew;
window.onWarningFilterChange = DeviceOverview.onWarningFilterChange;
window.toggleMapFunctionsMenu = DeviceOverview.toggleMapFunctionsMenu;
window.toggleDeviceId = DeviceOverview.toggleDeviceId;
window.toggleCluster = DeviceOverview.toggleCluster;
window.toggleMeasureDistance = DeviceOverview.toggleMeasureDistance;
window.finishMeasuring = DeviceOverview.finishMeasuring;
window.clearMeasure = DeviceOverview.clearMeasure;
window.toggleWeatherForecast = DeviceOverview.toggleWeatherForecast;
window.closeWeatherCard = DeviceOverview.closeWeatherCard;
window.showPrevForecast = DeviceOverview.showPrevForecast;
window.showNextForecast = DeviceOverview.showNextForecast;
window.queryDevices = DeviceOverview.queryDevices;
window.locateDeviceOnMap = DeviceOverview.locateDeviceOnMap;
window.locateDeviceDirectly = DeviceOverview.locateDeviceDirectly; 