package de.arxsilex.maptools.converter.desktop

import de.arxsilex.maptools.converter.core.preview.PreviewFeatureClass
import de.arxsilex.maptools.converter.core.preview.PreviewLabel
import de.arxsilex.maptools.converter.core.preview.PreviewLabelClass

internal data class PreviewLayerVisibility(
    val majorRoads: Boolean = true,
    val regionalRoads: Boolean = true,
    val localRoads: Boolean = true,
    val paths: Boolean = true,
    val railways: Boolean = false,
    val places: Boolean = true,
    val water: Boolean = false,
    val landscape: Boolean = false,
    val buildings: Boolean = true,
    val poi: Boolean = true,
)

internal object PreviewLayerPolicy {
    fun isFeatureVisible(
        featureClass: PreviewFeatureClass,
        layers: PreviewLayerVisibility,
        zoom: Float,
        interactive: Boolean,
    ): Boolean {
        val enabled = when (featureClass) {
            PreviewFeatureClass.MAJOR_ROAD -> layers.majorRoads
            PreviewFeatureClass.MINOR_ROAD -> layers.regionalRoads
            PreviewFeatureClass.LOCAL_ROAD -> layers.localRoads
            PreviewFeatureClass.PATH -> layers.paths
            PreviewFeatureClass.RAILWAY -> layers.railways
            PreviewFeatureClass.WATERWAY, PreviewFeatureClass.WATER_AREA -> layers.water
            PreviewFeatureClass.LAND, PreviewFeatureClass.URBAN_AREA -> layers.landscape
            PreviewFeatureClass.BUILDING -> layers.buildings
            PreviewFeatureClass.POI -> layers.poi
        }
        if (!enabled) return false
        return when (featureClass) {
            PreviewFeatureClass.MINOR_ROAD -> zoom >= 1.8f
            PreviewFeatureClass.LOCAL_ROAD -> zoom >= 3.5f
            PreviewFeatureClass.PATH -> zoom >= 7f
            PreviewFeatureClass.RAILWAY -> zoom >= 1.8f
            PreviewFeatureClass.BUILDING -> !interactive && zoom >= 10f
            PreviewFeatureClass.POI -> !interactive && zoom >= 8f
            else -> true
        }
    }

    fun isLabelVisible(label: PreviewLabel, layers: PreviewLayerVisibility, zoom: Float): Boolean =
        when (label.labelClass) {
            PreviewLabelClass.CITY, PreviewLabelClass.TOWN -> layers.places
            PreviewLabelClass.VILLAGE -> layers.places && zoom >= 1.8f
            PreviewLabelClass.ROAD -> when {
                label.importance >= 50 -> layers.majorRoads
                label.importance >= 35 -> layers.regionalRoads && zoom >= 1.8f
                label.importance >= 25 -> layers.localRoads && zoom >= 3.5f
                else -> layers.paths && zoom >= 7f
            }
            PreviewLabelClass.POI -> layers.poi && zoom >= 8f
            PreviewLabelClass.WATER -> layers.water
            PreviewLabelClass.AREA -> layers.landscape
        }

    fun minimumLabelImportance(zoom: Float): Int = when {
        zoom < 1.8f -> 85
        zoom < 3.5f -> 68
        zoom < 7f -> 48
        zoom < 12f -> 34
        else -> 18
    }

    fun detailHintKey(zoom: Float): String = when {
        zoom < 1.8f -> "preview.overview"
        zoom < 3.5f -> "preview.regions"
        zoom < 7f -> "preview.places"
        zoom < 10f -> "preview.nearby"
        zoom < 20f -> "preview.details"
        else -> "preview.fine_details"
    }
}
