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

import android.content.Context
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.base.BaseMapCommon
import com.abbidot.tracker.bean.HistoryDataBean
import com.abbidot.tracker.bean.MapDeviceBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.databinding.LayoutPetLocationInfoBinding
import com.abbidot.tracker.ui.fragment.map.baidumap.HomeMapBaiduMapFragment
import com.abbidot.tracker.ui.fragment.map.googlemap.HomeMapGoogleMapFragmentV3
import com.google.android.gms.maps.model.LatLng
import javax.inject.Inject

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

    private var mHomeMapGoogleMapFragment: HomeMapGoogleMapFragmentV3? = null
    private var mHomeMapBaiduMapFragment: HomeMapBaiduMapFragment? = null

    fun getMapFragment(
        context: Context,
        locationLayoutViewBinding: LayoutPetLocationInfoBinding,
        deviceBatteryLayout: ViewGroup,
        petAddressTimeFormat: String,
        isGeocode: Boolean,
        mapLoadOk: () -> Unit
    ): Fragment {
        return if (AppUtils.isChina(AppUtils.SWITCH_MAP_TYPE)) {
            mHomeMapBaiduMapFragment = HomeMapBaiduMapFragment.newInstance(context, mapLoadOk)
            mHomeMapBaiduMapFragment!!
        } else {
            mHomeMapGoogleMapFragment = HomeMapGoogleMapFragmentV3.newInstance(
                context,
                locationLayoutViewBinding,
                deviceBatteryLayout,
                petAddressTimeFormat,
                isGeocode,
                mapLoadOk
            )
            mHomeMapGoogleMapFragment!!
        }
    }

    fun setMapDeviceBean(mapDeviceBean: MapDeviceBean?) {
        if (null != mHomeMapBaiduMapFragment) {
            mHomeMapBaiduMapFragment!!.setMapDeviceBean(mapDeviceBean)
        } else if (null != mHomeMapGoogleMapFragment) {
            mHomeMapGoogleMapFragment!!.setMapDeviceBean(mapDeviceBean)
        }
    }

    /**
     * 开始每隔几秒刷新用户位置
     */
    fun startRefreshUserLocation() {
        if (isMapLoadOk()) startRefreshUserLocation(
            mHomeMapBaiduMapFragment, mHomeMapGoogleMapFragment
        )
    }

    /**
     * 获取宠物反编译好的位置信息
     */
    fun getDecPetAddressData(): HistoryDataBean? {
        if (null != mHomeMapBaiduMapFragment) {
            return mHomeMapBaiduMapFragment!!.mDecPetAddressData
        } else if (null != mHomeMapGoogleMapFragment) {
            return mHomeMapGoogleMapFragment!!.mDecPetAddressData
        }
        return null
    }

    fun clearMarker() {
        if (null != mHomeMapBaiduMapFragment) {
            mHomeMapBaiduMapFragment!!.mBaiduMap?.clear()
        } else if (null != mHomeMapGoogleMapFragment) {
            mHomeMapGoogleMapFragment!!.clearAllMarker()
        }
    }

    fun refreshPetCurrentLocation(latitude: Double, longitude: Double, isMoveCamera: Boolean) {
        if (null != mHomeMapBaiduMapFragment) {
            mHomeMapBaiduMapFragment?.apply {
                refreshPetCurrentLocation(
                    latitude, longitude, isMoveCamera
                )
            }
        } else if (null != mHomeMapGoogleMapFragment) {
            MMKVUtil.putDouble(MMKVKey.MapShowDefaultLat, latitude)
            MMKVUtil.putDouble(MMKVKey.MapShowDefaultLon, longitude)
            mHomeMapGoogleMapFragment?.apply {
                refreshPetCurrentLocation(
                    LatLng(latitude, longitude), needMoveCamera = isMoveCamera
                )
            }
        }
    }

    /**
     * 重新设置宠物头像
     */
    fun setPetHeadIcon(imageUrl: String, petType: Int) {
        if (null != mHomeMapBaiduMapFragment) {
            mHomeMapBaiduMapFragment!!.setPetHeadIconUrl(imageUrl)
        } else if (null != mHomeMapGoogleMapFragment) {
            mHomeMapGoogleMapFragment!!.setPetHeadIconUrl(imageUrl, petType)
        }
    }

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

    /**
     * 切换地图类型
     */
    fun switchSatelliteAndNormalMapType() {
        switchSatelliteAndNormalMapType(mHomeMapBaiduMapFragment, mHomeMapGoogleMapFragment)
    }

    /**
     * 切换用户和宠物的位置居中显示
     */
    fun switchShowLocation(@ConstantInt type: Int) {
        switchShowLocationType(type, mHomeMapBaiduMapFragment, mHomeMapGoogleMapFragment)
    }

    /**
     * 画轨迹线
     */
    fun addTrackLine(latitude: Double, longitude: Double) {
        if (null != mHomeMapBaiduMapFragment) {
            mHomeMapBaiduMapFragment!!.addLine(latitude, longitude)
        } else if (null != mHomeMapGoogleMapFragment) {
            mHomeMapGoogleMapFragment!!.addLine(LatLng(latitude, longitude))
        }
    }

    /**
     * 画轨迹线
     */
    fun addTrackLines(trackList: MutableList<HistoryDataBean>) {
        if (null != mHomeMapBaiduMapFragment) {
        } else if (null != mHomeMapGoogleMapFragment) {
            mHomeMapGoogleMapFragment!!.addLines(trackList)
        }
    }

    /**
     * 计算用户和宠物的距离
     */
    fun getUserAndPetDistance(): Double {
        var distance = 0.0
        if (null != mHomeMapBaiduMapFragment) {
            distance = mHomeMapBaiduMapFragment!!.getUserAndPetDistance()
        } else if (null != mHomeMapGoogleMapFragment) {
            distance = mHomeMapGoogleMapFragment!!.getUserAndPetDistance()
        }
        return distance
    }

    /**
     * 开始水波纹动画
     */
    fun startRippleCircleAnim() {
        if (null != mHomeMapBaiduMapFragment) {
        } else if (null != mHomeMapGoogleMapFragment) {
            mHomeMapGoogleMapFragment!!.startRippleCircleAnim()
        }
    }

    fun greenRippleCircleAnim() {
        mHomeMapGoogleMapFragment?.greenRippleCircleAnim()
    }

    fun clearRippleCircleAnim() {
        mHomeMapGoogleMapFragment?.clearRippleCircleAnim()
    }

    fun setMarkerInfoViewOffset() {
        if (null != mHomeMapBaiduMapFragment) {
        } else if (null != mHomeMapGoogleMapFragment) {
            mHomeMapGoogleMapFragment!!.setMarkerInfoViewOffset()
        }
    }

    fun getUserGoogleLatLng(): LatLng? {
        return mHomeMapGoogleMapFragment?.mUserLatLng
    }

    fun addUserAndPetLine() {
        mHomeMapGoogleMapFragment?.addUserAndPetLine()
    }

    fun removeUserAndPetLine() {
        mHomeMapGoogleMapFragment?.removeUserAndPetLine()
    }

//    fun getAddressShowMarkerInfoWindow(historyDataBean: HistoryDataBean) {
//        if (null != mHomeMapBaiduMapFragment) {
//            mHomeMapBaiduMapFragment!!.showMarkerInfoWindow(historyDataBean)
//        } else if (null != mHomeMapGoogleMapFragment) {
//            mHomeMapGoogleMapFragment!!.showMarkerInfoWindow(historyDataBean)
//        }
//    }

}