package com.abbidot.tracker.ui.common.map

import android.content.Context
import androidx.appcompat.widget.AppCompatSeekBar
import androidx.fragment.app.Fragment
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.baselibrary.util.LogUtil
import com.abbidot.tracker.base.BaseMapCommon
import com.abbidot.tracker.bean.HistoryDataBean
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.ui.fragment.map.baidumap.HistoryDataBaiduMapFragment
import com.abbidot.tracker.ui.fragment.map.googlemap.HistoryDataGoogleMapFragment
import com.abbidot.tracker.vm.GeoCoderViewModel
import com.abbidot.tracker.widget.MapMarkerInfoView

/**
 *Created  by .yzq on 2022/7/5/005.
 * @link
 * @description:
 */
class HistoryDataMapCommon : BaseMapCommon() {

    private var mHistoryDataGoogleMapFragment: HistoryDataGoogleMapFragment? = null
    private var mHistoryDataBaiduMapFragment: HistoryDataBaiduMapFragment? = null

    fun getMapFragment(
        context: Context,
        markerInfoView: MapMarkerInfoView,
        geoCoderViewModel: GeoCoderViewModel,
        verticalTopToBottomSeekBar: AppCompatSeekBar,
        mapLoadOk: () -> Unit
    ): Fragment {
        return if (AppUtils.isChina(AppUtils.SWITCH_MAP_TYPE)) {
            mHistoryDataBaiduMapFragment =
                HistoryDataBaiduMapFragment.newInstance(context, geoCoderViewModel, mapLoadOk)
            mHistoryDataBaiduMapFragment!!
        } else {
            mHistoryDataGoogleMapFragment = HistoryDataGoogleMapFragment.newInstance(
                context, markerInfoView, geoCoderViewModel, verticalTopToBottomSeekBar, mapLoadOk
            )
            mHistoryDataGoogleMapFragment!!
        }
    }

    /**
     * 切换地图类型
     */
    fun switchSatelliteAndNormalMapType() {
        switchSatelliteAndNormalMapType(mHistoryDataBaiduMapFragment, mHistoryDataGoogleMapFragment)
    }

    suspend fun setPetBean(petBean: PetBean) {
        if (null != mHistoryDataGoogleMapFragment) {
            mHistoryDataGoogleMapFragment!!.setPetBean(petBean)
        }
        if (null != mHistoryDataBaiduMapFragment) {
        }
    }

    /**
     * 地图是否加载好了，准备好了
     */
    fun isMapLoadOk() = isMapLoadOk(mHistoryDataBaiduMapFragment, mHistoryDataGoogleMapFragment)

    fun slideChanged(progress: Int) {
        if (null != mHistoryDataBaiduMapFragment) {
            mHistoryDataBaiduMapFragment!!.slideChanged(progress)
        } else if (null != mHistoryDataGoogleMapFragment) {
            mHistoryDataGoogleMapFragment!!.slideChanged(progress)
        }
    }

    fun slideStopChanged(progress: Int) {
        if (null != mHistoryDataBaiduMapFragment) {
            mHistoryDataBaiduMapFragment!!.slideStopChanged(progress)
        } else if (null != mHistoryDataGoogleMapFragment) {
            mHistoryDataGoogleMapFragment!!.slideStopChanged(progress)
        }
    }

    fun addSetLine(isMoveCamera: Boolean = true) {
        if (null != mHistoryDataBaiduMapFragment) {
            mHistoryDataBaiduMapFragment!!.addSetLine()
        } else if (null != mHistoryDataGoogleMapFragment) {
            mHistoryDataGoogleMapFragment!!.addSetLine(isMoveCamera)
        }
    }

    fun addSetNumber(isMoveCamera: Boolean = true) {
        if (null != mHistoryDataBaiduMapFragment) {
            mHistoryDataBaiduMapFragment!!.addSetNumber()
        } else if (null != mHistoryDataGoogleMapFragment) {
            mHistoryDataGoogleMapFragment!!.addSetNumber(isMoveCamera)
        }
    }

    fun clearAllMarker() {
        if (null != mHistoryDataBaiduMapFragment) {
            mHistoryDataBaiduMapFragment!!.clearAllMarker()
        } else if (null != mHistoryDataGoogleMapFragment) {
            mHistoryDataGoogleMapFragment!!.clearAllMarker()
        }
    }

    fun setLatLngData(data: MutableList<HistoryDataBean>) {
        if (data.size == 0) return
        try {
            if (null != mHistoryDataBaiduMapFragment) {
                mHistoryDataBaiduMapFragment!!.setLatLngData(data)
            } else if (null != mHistoryDataGoogleMapFragment) {
                mHistoryDataGoogleMapFragment!!.setLatLngData(data)
            }
        } catch (e: Exception) {
            LogUtil.e("setLatLngData异常:${e.message}")
        }
    }

    fun showInfoWindow(historyDataBean: HistoryDataBean) {
        if (null != mHistoryDataBaiduMapFragment) {
            mHistoryDataBaiduMapFragment!!.showMarkerInfoWindow(historyDataBean)
        } else if (null != mHistoryDataGoogleMapFragment) {
            mHistoryDataGoogleMapFragment!!.showMarkerInfoWindow(historyDataBean)
        }
    }

    fun resetMarkerInfoViewOffset() {
        mHistoryDataGoogleMapFragment?.resetMarkerInfoViewOffset()
    }

    fun setPetUserLatLng() {
        if (null != mHistoryDataBaiduMapFragment) {
        } else if (null != mHistoryDataGoogleMapFragment) {
            mHistoryDataGoogleMapFragment?.apply {
                mUserLatLng?.let {
                    moveCameraLocation(it)
                    setPetMarkerLatLng(it)
                }
            }
        }
    }
}