package com.abbidot.tracker.ui.fragment.map.googlemap

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.HistoryDataBean
import com.abbidot.tracker.widget.TypefaceTextView
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.Marker
import com.mapbox.core.utils.TextUtils

/**
 *Created  by .yzq on 2022/1/14/014.
 * @link
 * @description:自定义点击头像顶部出现布局 https://developers.google.cn/maps/documentation/android-sdk/infowindows
 *
 * 该接口包含两个供您实现的方法：getInfoWindow(Marker) 和 getInfoContents(Marker)。
 * API 将首先调用 getInfoWindow(Marker)，如果系统返回 null，则会接着调用 getInfoContents(Marker)。
 * 如果仍返回 null，则系统会使用默认信息窗口。
 */
class MarkerInfoWindowAdapter(context: Context) : GoogleMap.InfoWindowAdapter {

    private val mContext = context

    override fun getInfoContents(marker: Marker): View? {
        val view = LayoutInflater.from(mContext).inflate(R.layout.marker_info_contents, null)

        // 1. Get tag
//        val place = marker.tag as? Place ?: return null
//        view.findViewById<TextView>(R.id.text_view_title).text = place.name
//        view.findViewById<TextView>(R.id.text_view_address).text = place.address
//        view.findViewById<TextView>(R.id.text_view_rating).text =
//            "Rating: %.2f".format(place.rating)

        LogUtil.d("MarkerInfoWindowAdapter------------>getInfoContents")
        initData(view, marker)

        return view
    }

    override fun getInfoWindow(marker: Marker): View? {
        val view = LayoutInflater.from(mContext).inflate(R.layout.marker_info_contents, null)
        LogUtil.d("MarkerInfoWindowAdapter------------>getInfoWindow")
        initData(view, marker)
        return view
    }

    /**
     * 初始化
     */
    private fun initData(view: View, marker: Marker) {
        val locationTextView = view.findViewById<TypefaceTextView>(R.id.map_pet_current_location)
        val locationUpdateTextView =
            view.findViewById<TypefaceTextView>(R.id.map_pet_location_update_time)

        val tag = marker.tag as HistoryDataBean?

        LogUtil.d("$tag-----------")
        if (null == tag) return

        if (TextUtils.isEmpty(tag.dayTime)) {
            locationUpdateTextView.visibility = View.GONE
        } else {
            locationUpdateTextView.visibility = View.VISIBLE
            locationUpdateTextView.text = tag.dayTime
        }
        locationTextView.text = tag.address
    }
}