package com.abbidot.tracker.base

import android.content.Context
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.bean.FencesBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.ui.fragment.map.baidumap.BaseBaiduMapFragment
import com.abbidot.tracker.ui.fragment.map.googlemap.BaseGoogleMapFragment
import com.abbidot.tracker.util.Util
import com.baidu.mapapi.utils.DistanceUtil
import com.google.android.gms.maps.model.LatLng

/**
 *Created  by .yzq on 2024/5/10/010.
 * @link
 * @description:重复调用方法写在这个类中
 */
open class BaseMapCommon {

    /**
     * 地图是否加载好了，准备好了
     */
    fun isMapLoadOk(
        baiduMapFragment: BaseBaiduMapFragment?, googleMapFragment: BaseGoogleMapFragment?
    ): Boolean {
        var loadOK = false
        if (null != baiduMapFragment) {
            loadOK = baiduMapFragment.isMapLoadOk
        } else if (null != googleMapFragment) {
            loadOK = googleMapFragment.isMapLoadOk
        }
        return loadOK
    }

    /**
     * 开始每隔几秒刷新用户位置
     */
    fun startRefreshUserLocation(
        baiduMapFragment: BaseBaiduMapFragment?, googleMapFragment: BaseGoogleMapFragment?
    ) {
        googleMapFragment?.getLastLocation()
    }

    /**
     * 切换地图类型
     */
    fun switchSatelliteAndNormalMapType(
        baiduMapFragment: BaseBaiduMapFragment?, googleMapFragment: BaseGoogleMapFragment?
    ) {
        val mapTypeSp = Util.getMapTypeSp()
        if (mapTypeSp == ConstantInt.Type0) {
            MMKVUtil.putInt(MMKVKey.MapType, ConstantInt.Type1)
        } else {
            MMKVUtil.putInt(MMKVKey.MapType, ConstantInt.Type0)
        }
        if (null != baiduMapFragment) {
            baiduMapFragment.switchSatelliteAndNormalMapType()
        } else googleMapFragment?.switchSatelliteAndNormalMapType()
    }

    /**
     * 设置宠物当前经纬度和头像
     */
    fun setPetHeadAndCurrentLocation(
        petHeadUrl: String,
        petType: Int,
        lat: Double,
        lon: Double,
        baiduMapFragment: BaseBaiduMapFragment?,
        googleMapFragment: BaseGoogleMapFragment?
    ) {
        if (null != googleMapFragment) {
            googleMapFragment.setPetCurrentLocation(petHeadUrl, lat, lon, petType)
        } else baiduMapFragment?.setPetCurrentLocation(petHeadUrl, lat, lon)
    }

    /**
     * 切换用户和宠物的位置居中显示
     */
    fun switchShowLocationType(
        @ConstantInt type: Int,
        baiduMapFragment: BaseBaiduMapFragment?,
        googleMapFragment: BaseGoogleMapFragment?
    ) {
        if (null != baiduMapFragment) {
            baiduMapFragment.switchShowLocation(type)
        } else googleMapFragment?.switchShowLocation(type)
    }

    /**
     * 切换用户、宠物和第三方的位置(围栏或者wifi zone)居中显示
     */
    fun switchShowLocationType(
        @ConstantInt type: Int,
        lat: Double,
        lon: Double,
        baiduMapFragment: BaseBaiduMapFragment?,
        googleMapFragment: BaseGoogleMapFragment?
    ) {
        if (null != baiduMapFragment) {
            if (type == ConstantInt.OtherLocationType) {
                baiduMapFragment.moveCameraLocation(lat, lon)
            } else {
                baiduMapFragment.switchShowLocation(type)
            }
        } else if (null != googleMapFragment) {
            if (type == ConstantInt.OtherLocationType) {
                googleMapFragment.moveCameraUpdate(lat, lon)
            } else {
                googleMapFragment.switchShowLocation(type)
            }
        }
    }


    /**
     * 根据预览围栏类型设置地图等级
     * @param padding 距离屏幕边缘的距离,px单位
     */
    fun restoreFencesSetMapZoom(
        context: Context,
        fencesBean: FencesBean,
        baiduMapFragment: BaseBaiduMapFragment?,
        googleMapFragment: BaseGoogleMapFragment?,
        padding: Int
    ) {
        fencesBean.apply {
            when (fenceShapeType) {
                ConstantInt.CircleShapeType -> {
                    googleMapFragment?.apply {
                        setLatLngZoom(
                            context,
                            padding,
                            LatLng(ancillaryLatitude, ancillaryLongitude),
                            LatLng(ancillaryOtherLatitude, ancillaryOtherLongitude)
                        )
                    }
                    baiduMapFragment?.apply {
                        setBaiduMapZoom(getDistanceZoom(radius.toDouble()) + 1)
                    }
                }

                ConstantInt.RectangleShapeType -> {
                    googleMapFragment?.setLatLngZoom(
                        context,
                        padding,
                        LatLng(latitudeA, longitudeA),
                        LatLng(latitudeB, longitudeB),
                        LatLng(latitudeC, longitudeC),
                        LatLng(latitudeD, longitudeD)
                    )
                    baiduMapFragment?.apply {
                        setBaiduMapZoom(
                            getDistanceZoom(
                                maxOf(
                                    shortDistance, longDistance
                                ).toDouble()
                            ) + 1
                        )
                    }
                }

                ConstantInt.PolygonShapeType -> {
                    googleMapFragment?.setLatLngZoom(
                        context,
                        padding,
                        LatLng(latitudeA, longitudeA),
                        LatLng(latitudeB, longitudeB),
                        LatLng(latitudeC, longitudeC),
                        LatLng(latitudeD, longitudeD),
                        LatLng(latitudeE, longitudeE),
                        LatLng(latitudeF, longitudeF),
                    )
                    baiduMapFragment?.apply {
                        val minLat =
                            minOf(latitudeA, latitudeB, latitudeC, latitudeD, latitudeE, latitudeF)
                        val maxLat =
                            minOf(latitudeA, latitudeB, latitudeC, latitudeD, latitudeE, latitudeF)
                        val minLon = minOf(
                            longitudeA, longitudeB, longitudeC, longitudeD, longitudeE, longitudeF
                        )
                        val maxLon = minOf(
                            longitudeA, longitudeB, longitudeC, longitudeD, longitudeE, longitudeF
                        )
                        val distance = DistanceUtil.getDistance(
                            getBaiduMapLatLng(minLat, minLon), getBaiduMapLatLng(maxLat, maxLon)
                        )
                        setBaiduMapZoom(getDistanceZoom(distance))
                    }
                }
            }
        }
    }
}