var DeviceOverview = (function() {
    'use strict';

    var deviceList = [];

    function init(devices, options) {
        deviceList = devices || [];

        window.deviceList = deviceList;
        window.userRole = options && options.role ? options.role : 'USER';

        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();
        });
    }

    function onMapTypeChange() {
        return MapCore.onMapTypeChange();
    }

    function searchDeviceNew() {
        return MapCore.searchDeviceNew();
    }

    function onWarningFilterChange() {
        return MapCore.onWarningFilterChange();
    }

    function toggleMapFunctionsMenu() {
        return MapCore.toggleMapFunctionsMenu();
    }

    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();
    }

    function queryDevices(statusType) {
        return SearchFilter.queryDevices(statusType);
    }

    function locateDeviceOnMap(deviceId, latitude, longitude) {
        return SearchFilter.locateDeviceOnMap(deviceId, latitude, longitude);
    }

    function locateDeviceDirectly(deviceId) {
        return SearchFilter.locateDeviceDirectly(deviceId);
    }

    function switchMapType(mapType) {
        return MapCore.switchMapType(mapType);
    }

    function locateMyLocation() {
        return DeviceMarkers.locateMyLocation();
    }
    
    function clearDeviceFilter() {
        return SearchFilter.clearDeviceFilter();
    }

    return {
        init: init,

        onMapTypeChange: onMapTypeChange,
        switchMapType: switchMapType,

        searchDeviceNew: searchDeviceNew,
        onWarningFilterChange: onWarningFilterChange,
        queryDevices: queryDevices,
        locateDeviceOnMap: locateDeviceOnMap,
        locateDeviceDirectly: locateDeviceDirectly,
        locateMyLocation: locateMyLocation,
        clearDeviceFilter: clearDeviceFilter,

        toggleMapFunctionsMenu: toggleMapFunctionsMenu,
        toggleDeviceId: toggleDeviceId,
        toggleCluster: toggleCluster,

        toggleMeasureDistance: toggleMeasureDistance,
        finishMeasuring: finishMeasuring,
        clearMeasure: clearMeasure,

        toggleWeatherForecast: toggleWeatherForecast,
        closeWeatherCard: closeWeatherCard,
        showPrevForecast: showPrevForecast,
        showNextForecast: showNextForecast
    };
})();

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;
window.locateMyLocation = DeviceOverview.locateMyLocation;
window.clearDeviceFilter = DeviceOverview.clearDeviceFilter; 