package com.cr.map

import com.cr.common.CrSaveManager

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/4/13 15:50
 * 描述：地图图层管理器
 */
class LayerManager {
    /**
     * 图层分组枚举
     */
    enum class LayerGroup{
        /**
         * 常用图层
         */
        LAYER_NAME_DAILY,
        /**
         * 绘制图层
         */
        LAYER_NAME_DRAW,
        /**
         * 历史图层
         */
        LAYER_NAME_HISTORY,
        /**
         * 基础图层
         */
        LAYER_NAME_BASE,
        /**
         * 飞行图层
         */
        LAYER_NAME_AIR
    }

    // define: 2023/4/13 常量定义
    val LAYER_NAME_DAILY = "常用图层"
    val LAYER_NAME_DRAW = "绘制图层"
    val LAYER_NAME_HISTORY = "历史图层"
    val LAYER_NAME_BASE = "基础图层"
    val LAYER_NAME_AIR = "飞行图层"

    // define: 2023/4/13 图层集合
    private var mapLayers:HashMap<String,MutableList<LayerModel>> = hashMapOf()

    companion object {
        /**
         * 获取静态实例 单例
         * @return LayerManager
         */
        fun getInstance() = InstanceHelper.self
    }

    /**
     * 单例必备
     */
    object InstanceHelper{
        var self = LayerManager()
    }

    /**
     * 初始化
     */
    init {
        mapLayers[LAYER_NAME_DAILY] = mutableListOf()
        mapLayers[LAYER_NAME_BASE] = mutableListOf()
        mapLayers[LAYER_NAME_HISTORY] = mutableListOf()
        mapLayers[LAYER_NAME_DRAW] = mutableListOf()
        mapLayers[LAYER_NAME_AIR] = mutableListOf()
    }

    /**
     * 添加图层
     * @param layer LayerModel 图层模型
     * @param group LayerGroup 图层所属组
     * @return Boolean 默认是否可见
     */
    fun addLayer(layer:LayerModel,group:LayerGroup):Boolean{
        // todo: 2023/9/27 获取图层可见性
        val isVisible = getLayerVisible(layer.name!!,layer.isVisible)
        layer.isVisible = isVisible
        when(group){
            LayerGroup.LAYER_NAME_BASE->
                mapLayers[LAYER_NAME_BASE]?.add(layer)
            LayerGroup.LAYER_NAME_DRAW->
                mapLayers[LAYER_NAME_DRAW]?.add(layer)
            LayerGroup.LAYER_NAME_DAILY->
                mapLayers[LAYER_NAME_DAILY]?.add(layer)
            LayerGroup.LAYER_NAME_HISTORY->
                mapLayers[LAYER_NAME_HISTORY]?.add(layer)
            LayerGroup.LAYER_NAME_AIR->
                mapLayers[LAYER_NAME_AIR]?.add(layer)
        }
        // todo: 2023/9/27 添加图层模型的同时返回图层当前可见性
        return isVisible
    }

    /**
     * 获取图层集合
     * @return List<LayerModel>?
     */
    fun getLayers(): MutableList<LayerModel>? {
        val resList: MutableList<LayerModel> = mutableListOf()
        // TODO: 4/15/21 循环所有的数据
        for (key in mapLayers.keys) {
            val models: MutableList<LayerModel>? = mapLayers[key]
            for (i in models!!.indices) {
                val model = models[i]
                if (i == 0) {
                    model.groupName = key!!
                }
                resList.add(model)
            }
        }
        return resList
    }

    /**
     * 存储图层可见性
     * @param layerName String
     * @param isVisible Boolean
     */
    fun saveLayerVisible(layerName:String,isVisible:Boolean){
        // todo: 2023/9/27 可见存储1 不可见存储0
        CrSaveManager.getInstance().saveStringKey(layerName,if(isVisible) "1" else "0")
    }

    /**
     * 获取图层可见性
     * @param layerName String
     * @return Boolean
     */
    fun getLayerVisible(layerName: String,isVisible: Boolean):Boolean{
        val resVisible = CrSaveManager.getInstance().getStringKey(layerName)
        return resVisible?.equals("1") ?: isVisible
    }
}