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

import android.content.Context
import android.view.View
import androidx.fragment.app.Fragment
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.base.BaseMapCommon
import com.abbidot.tracker.bean.FencesBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.ui.fragment.map.baidumap.FencesAddEditBaiduMapFragment
import com.abbidot.tracker.ui.fragment.map.googlemap.FencesAddEditGoogleMapFragment
import com.abbidot.tracker.util.LonAndLatUtil
import com.abbidot.tracker.vm.GeoCoderViewModel
import com.abbidot.tracker.widget.FencesCircleView
import com.abbidot.tracker.widget.FencesPolygonView
import com.abbidot.tracker.widget.FencesRectView
import com.google.android.gms.maps.model.LatLng

/**
 *Created  by .yzq on 2024/4/23/023.
 * @link
 * @description:
 */
class FencesAddEditMapCommon : BaseMapCommon() {
    private var mFencesGoogleMapFragment: FencesAddEditGoogleMapFragment? = null
    private var mFencesBaiduMapFragment: FencesAddEditBaiduMapFragment? = null

    fun getMapFragment(
        context: Context,
        fencesBean: FencesBean,
        fencesCircleView: FencesCircleView,
        fencesRectView: FencesRectView,
        fencesPolygonView: FencesPolygonView,
        isEdit: Boolean,
        onActivityCallBack: OnActivityCallBack?,
        mapLoadOk: () -> Unit
    ): Fragment {
        return if (AppUtils.isChina(AppUtils.SWITCH_MAP_TYPE)) {
            mFencesBaiduMapFragment = FencesAddEditBaiduMapFragment.newInstance(
                context,
                fencesBean,
                fencesCircleView,
                fencesRectView,
                fencesPolygonView,
                isEdit,
                onActivityCallBack,
                mapLoadOk
            )
            mFencesBaiduMapFragment!!
        } else {
            mFencesGoogleMapFragment = FencesAddEditGoogleMapFragment.newInstance(
                context,
                fencesBean,
                fencesCircleView,
                fencesRectView,
                fencesPolygonView,
                isEdit,
                onActivityCallBack,
                mapLoadOk
            )
            mFencesGoogleMapFragment!!
        }
    }

    /**
     * 经纬度转换地址
     */
    fun getLatLonAddress(geoCoderViewModel: GeoCoderViewModel, fencesBean: FencesBean?) {
        if (null != mFencesGoogleMapFragment) {
            fencesBean?.apply {
                geoCoderViewModel.getLatLonAddress(latitudeCenter, longitudeCenter)
            }
        } else if (null != mFencesBaiduMapFragment) {
            fencesBean?.apply {
                val convertLonLat =
                    LonAndLatUtil.convertFromGCJ02ToWGS84(latitudeCenter, longitudeCenter)
                geoCoderViewModel.getLatLonAddress(convertLonLat[0], convertLonLat[1])
            }
        }
    }

    /**
     * 根据预览围栏类型设置地图等级
     * @param padding 距离屏幕边缘的距离,px单位
     */
    fun restoreEditFencesSetMapZoom(
        context: Context, fencesBean: FencesBean, padding: Int = AppUtils.dpToPx(80)
    ) {
        restoreFencesSetMapZoom(
            context, fencesBean, mFencesBaiduMapFragment, mFencesGoogleMapFragment, padding
        )
    }

    /**
     * 上滑，下滑手势，布局绘制，围栏重新设置中心
     */
    fun resetFencesCentre(activity: BaseActivity<*>, view: View, fencesBean: FencesBean) {
        fencesBean.let {
            mFencesGoogleMapFragment?.apply {
                view.postDelayed({
                    if (activity.isFinishing) return@postDelayed
                    resetFencesViewCentre(LatLng(it.latitudeCenter, it.longitudeCenter))
                }, 100)
            }

            mFencesBaiduMapFragment?.apply {
                view.postDelayed({
                    if (activity.isFinishing) return@postDelayed
                    resetFencesViewCentre(getBaiduMapLatLng(it.latitudeCenter, it.longitudeCenter))
                }, 600)
            }
        }
    }

    fun setMapZoom(zoom: Float) {
        if (null != mFencesGoogleMapFragment) {
            mFencesGoogleMapFragment!!.setGoogleMapZoom(zoom)
        } else if (null != mFencesBaiduMapFragment) {
            mFencesBaiduMapFragment!!.setBaiduMapZoom(19f)
        }
    }

    fun setZoomTo(zoomLevel: Float) {
        if (null != mFencesGoogleMapFragment) {
            mFencesGoogleMapFragment!!.setZoomTo(zoomLevel)
        } else if (null != mFencesBaiduMapFragment) {
        }
    }

    fun calculateDistanceAndGetPointLatLng(
        activity: BaseActivity<*>? = null, delayMillis: Long = 600
    ) {
        activity?.showLoading(true)
        if (null != mFencesGoogleMapFragment) {
            mFencesGoogleMapFragment?.mapOkAndDistancePointLatLng(delayMillis)
        } else if (null != mFencesBaiduMapFragment) {
            mFencesBaiduMapFragment?.mapOkAndDistancePointLatLng()
        }
    }

    /**
     * 切换地图类型
     */
    fun switchSatelliteAndNormalMapType() {
        switchSatelliteAndNormalMapType(mFencesBaiduMapFragment, mFencesGoogleMapFragment)
    }

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

    /**
     * 设置宠物当前经纬度和头像
     */
    fun setPetHeadAndCurrentLocation(petHeadUrl: String, petType: Int, lat: Double, lon: Double) {
        setPetHeadAndCurrentLocation(
            petHeadUrl, petType, lat, lon, mFencesBaiduMapFragment, mFencesGoogleMapFragment
        )
    }

    /**
     * 切换用户、宠物和围栏的位置居中显示
     */
    fun switchShowLocation(@ConstantInt type: Int, lat: Double, lon: Double) {
        switchShowLocationType(type, lat, lon, mFencesBaiduMapFragment, mFencesGoogleMapFragment)
    }

    interface OnActivityCallBack {
        fun calculationDistance(distances: Array<String>)
    }
}