package com.cr.map

import android.content.Context
import android.view.MotionEvent
import com.cr.dialog.DialogNormal
import com.esri.arcgisruntime.ArcGISRuntimeException
import com.esri.arcgisruntime.data.Feature
import com.esri.arcgisruntime.geometry.Point
import com.esri.arcgisruntime.geometry.SpatialReference
import com.esri.arcgisruntime.layers.FeatureLayer
import com.esri.arcgisruntime.layers.Layer
import com.esri.arcgisruntime.mapping.GeoElement
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener
import com.esri.arcgisruntime.mapping.view.MapView

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/4/18 08:44
 * 描述：地图Touch
 */
class MapTouch constructor(context: Context, mapView: MapView) :
    DefaultMapViewOnTouchListener(context, mapView) {

    /**
     * Touch回调
     */
    interface TouchListener {
        // todo: 2023/4/18 涂鸦选择
        fun onDoodleSelect(feature: Feature?)
    }


    // define: 2023/4/18 定义变量
    private var context: Context? = null
    private var mapView: MapView? = null

    private var queryLayer: Layer? = null  // define: 2023/4/18 查询的图层
    private var action: MapAction? = null

    // todo: 2023/4/18 监听
    private var listener: TouchListener? = null

    // todo: 2023/4/13 静态
    companion object {
        // define: 2023/4/13 WGS84坐标系框架
        val wgs84SpatialReference: SpatialReference = SpatialReference.create(4326)
    }

    /**
     * 初始化
     */
    init {
        this.context = context
        this.mapView = mapView
    }


    // todo: 2023/4/18 重写旋转事件
    override fun onRotate(event: MotionEvent?, rotationAngle: Double): Boolean {
        return false
    }

    // todo: 2023/4/18 重写点击事件
    override fun onSingleTapUp(e: MotionEvent?): Boolean {
        when (action) {
            MapAction.MapTapSelectDoodle -> {
                identifyLayer(e!!.x.toInt(), e.y.toInt())
            }
        }
        return true
    }

    // todo: 2023/4/18 内部函数
    private fun identifyLayer(sx: Int, sy: Int) {
        var queryPoint = android.graphics.Point(sx, sy)
        var iAsync = mapView?.identifyLayerAsync(queryLayer, queryPoint, 5.0, false)
        iAsync?.addDoneListener(Runnable {
            var feature:Feature ? = null
            try {
                var result = iAsync.get()
                if (result.elements.size >= 1) {
                    feature = result.elements[0] as Feature
                } else {
                    showWarning("未查询到任何数据！")
                }
            } catch (e: java.lang.IllegalArgumentException) {
                showError("查询错误！")
            } catch (e: ArcGISRuntimeException) {
                showError("查询错误！")
            }finally {
                if (listener != null) listener?.onDoodleSelect(feature)
            }
        })
    }

    /**
     * 显示错误消息
     * @param error String 错误消息
     */
    private fun showError(error: String) {
        DialogNormal(context!!, "错误", error).show()
    }

    /**
     * 显示警告信息
     * @param warning String 警告信息
     */
    private fun showWarning(warning: String) {
        DialogNormal(context!!, "警告", warning).show()
    }

    /**
     * 设置查询图层
     * @param layer Layer 查询图层
     */
    fun setQueryLayer(layer: Layer) {
        this.queryLayer = layer
    }

    /**
     * 设置查询图层 用于点击查询
     * @param layer Layer 图层
     * @param action MapAction 动作
     */
    fun setQueryLayer(layer: Layer?, action: MapAction?) {
        this.queryLayer = layer
        this.action = action
    }

    /**
     * 设置监听
     * @param listener TouchListener 监听
     */
    fun setListener(listener: TouchListener) {
        this.listener = listener
    }
}