package com.cr.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.CompoundButton.OnCheckedChangeListener
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import com.cr.cruav.R
import com.cr.map.LayerModel
import com.cr.map.LayerType
import com.esri.arcgisruntime.layers.ArcGISMapImageSublayer
import com.esri.arcgisruntime.layers.ArcGISTiledLayer
import com.esri.arcgisruntime.layers.FeatureLayer
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/4/14 14:27
 * 描述：图层控制适配器
 */
class LayerAdapter@JvmOverloads constructor(
    context: Context,
    dataList:MutableList<LayerModel>
) : CrAdapter<LayerModel>(context, dataList) {
    companion object {
        class ViewHolder {
            var lblGroupName: TextView? = null
            var imgIco: ImageView? = null
            var lblName: TextView? = null
            var sVisible: Switch? = null
            var sLabel: Switch? = null
            var lblLabel: TextView? = null
        }
    }

    /**
     * 重写获取视图方法
     * @param position Int
     * @param convertView View?
     * @param viewGroup ViewGroup?
     * @return View?
     */
    override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View? {
        var holder:ViewHolder ?=null
        var view = convertView
        if(convertView == null){
            holder = ViewHolder()
            view = inflater?.inflate(R.layout.item_layercontrol,null)
            holder.lblGroupName = view?.findViewById(R.id.item_layer_group_name)
            holder.imgIco = view?.findViewById(R.id.item_layer_img)
            holder.lblName = view?.findViewById(R.id.item_layer_name)
            holder.sVisible = view?.findViewById(R.id.item_layer_on_visible)
            holder.sLabel = view?.findViewById(R.id.item_layer_on_label)
            holder.lblLabel = view?.findViewById(R.id.item_layer_lbl)
            view?.tag = holder
        }else{
            holder = view?.tag as ViewHolder
        }
        val model = dataList!![position]
        // todo: 2023/4/14 设置组标题
        if (model.groupName == "") {
            holder.lblGroupName!!.visibility = View.GONE
        } else {
            holder.lblGroupName!!.visibility = View.VISIBLE
            holder.lblGroupName!!.text = model.groupName
        }
        // todo: 2023/4/14 设置名称
        holder.lblName?.text = model.name
        // todo: 2023/4/14 设置是否可见
        holder.sVisible!!.isChecked = getLayerVisible(position)
        holder.sLabel!!.isChecked = getLayerLabelVisible(position)
        // todo: 2023/4/14 设置标注是否显示
        if (model.lyrType === LayerType.LAYER_TYPE_FEATURE || model.lyrType === LayerType.LAYER_TYPE_SERVER) {
            holder.lblLabel!!.visibility = View.VISIBLE
            holder.sLabel!!.visibility = View.VISIBLE
        } else {
            holder.lblLabel!!.visibility = View.GONE
            holder.sLabel!!.visibility = View.GONE
        }
        // todo: 2023/4/14 设置监听
        holder.sLabel!!.tag = position
        holder.sVisible!!.tag = position
        holder.sVisible!!.setOnCheckedChangeListener(checkedChangeListener)
        holder.sLabel!!.setOnCheckedChangeListener(checkedChangeListener)

        return view
    }

    /**
     * 监听
     */
    private var checkedChangeListener = object:OnCheckedChangeListener{
        override fun onCheckedChanged(view: CompoundButton?, isChecked: Boolean) {
            // todo: 2023/4/14 这段代码是为了保证不会通过setChecked方法激活该方法
            if (!view!!.isPressed) return
            val index: Int = Integer.valueOf(view.tag.toString())
            when (view.id) {
                R.id.item_layer_on_label -> setLabelVisible(index)
                R.id.item_layer_on_visible -> setLayerVisible(index)
            }
        }

    }

    /**
     * 设置图层是否显示
     * @param index Int
     */
    private fun setLayerVisible(index: Int) {
        val layerModel = dataList!![index]
        when (layerModel.lyrType) {
            LayerType.LAYER_TYPE_TILED -> {
                val tLayer = layerModel.layer as ArcGISTiledLayer
                layerModel.isVisible = !layerModel.isVisible
                tLayer.isVisible = layerModel.isVisible
            }
            LayerType.LAYER_TYPE_FEATURE, LayerType.LAYER_TYPE_FEATURE_EDIT -> {
                val fLayer = layerModel.layer as FeatureLayer
                layerModel.isVisible = !layerModel.isVisible
                fLayer.isVisible = layerModel.isVisible
            }
            LayerType.LAYER_TYPE_GRAPHIC -> {
                val gLayer = layerModel.layer as GraphicsOverlay
                layerModel.isVisible = !layerModel.isVisible
                gLayer.isVisible = layerModel.isVisible
            }
            LayerType.LAYER_TYPE_SERVER -> {
                val sLayer = layerModel.layer as ArcGISMapImageSublayer
                layerModel.isVisible = !layerModel.isVisible
                sLayer.isVisible = layerModel.isVisible
            }
        }
    }

    /**
     * 获取图层是否显示
     * @param index Int 图层Index
     * @return Boolean
     */
    private fun getLayerVisible(index: Int): Boolean {
        val layerModel = dataList!![index]
        return when (layerModel.lyrType) {
            LayerType.LAYER_TYPE_TILED -> {
                val tLayer = layerModel.layer as ArcGISTiledLayer
                tLayer.isVisible
            }
            LayerType.LAYER_TYPE_FEATURE, LayerType.LAYER_TYPE_FEATURE_EDIT -> {
                val fLayer = layerModel.layer as FeatureLayer
                fLayer.isVisible
            }
            LayerType.LAYER_TYPE_GRAPHIC -> {
                val gLayer = layerModel.layer as GraphicsOverlay
                gLayer.isVisible
            }
            LayerType.LAYER_TYPE_SERVER -> {
                val sLayer = layerModel.layer as ArcGISMapImageSublayer
                sLayer.isVisible
            }
            else -> false
        }
    }

    /**
     * 获取图层标注是否显示
     * @param index Int 图层Index
     * @return Boolean
     */
    private fun getLayerLabelVisible(index: Int): Boolean {
        val layerModel = dataList!![index]
        return when (layerModel.lyrType) {
            LayerType.LAYER_TYPE_FEATURE -> {
                val fLayer = layerModel.layer as FeatureLayer
                fLayer.isLabelsEnabled
            }
            LayerType.LAYER_TYPE_SERVER -> {
                val sLayer = layerModel.layer as ArcGISMapImageSublayer
                sLayer.isLabelsEnabled
            }
            else -> false
        }
    }

    /**
     * 控制图层是否显示标注
     * @param index Int 图层Index
     */
    private fun setLabelVisible(index: Int) {
        val layerModel: LayerModel = dataList!![index]
        when (layerModel.lyrType) {
            LayerType.LAYER_TYPE_FEATURE -> {
                val fLayer = layerModel.layer as FeatureLayer
                fLayer.isLabelsEnabled = !fLayer.isLabelsEnabled
            }
            LayerType.LAYER_TYPE_SERVER -> {
                val sLayer = layerModel.layer as ArcGISMapImageSublayer
                sLayer.isLabelsEnabled = !sLayer.isLabelsEnabled
            }
        }
    }
}