package com.abbidot.tracker.vm

import android.content.Context
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import com.abbidot.tracker.R
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.google.android.gms.maps.model.LatLng

/**
 *Created  by .yzq on 2022/3/2/002.
 * @link
 * @description:
 */
class FencesMapViewModel : ViewModel() {
    /**
     * 设置地图围栏数据
     */
    fun setFencesData(
        context: Context, fencesList: MutableList<FencesBean>?, mapFragment: Fragment
    ) {
        if (mapFragment is BaseGoogleMapFragment) {
            mapFragment.clearFenceList()
        } else if (mapFragment is BaseBaiduMapFragment) {
            mapFragment.clearFenceList()
        }

        if (null == fencesList) return
        if (fencesList.size == 0) return

        val saveFillColor = ContextCompat.getColor(context, R.color.select_color4)
        val saveStrokeColor = ContextCompat.getColor(context, R.color.select_color2)
        val noGoFillColor = ContextCompat.getColor(context, R.color.red_color3)
        val noGoStrokeColor = ContextCompat.getColor(context, R.color.red_color2)

        fencesList.forEach {
            if (it.isOn == ConstantInt.Open) {
                if (mapFragment is BaseGoogleMapFragment) {
                    if (it.fenceShapeType == ConstantInt.CircleShapeType) {
                        if (it.fenceType == ConstantInt.SafeZone) mapFragment.addCircleFence(
                            LatLng(it.latitudeCenter, it.longitudeCenter),
                            it.radius.toDouble(),
                            saveStrokeColor,
                            saveFillColor
                        )
                        else mapFragment.addCircleFence(
                            LatLng(it.latitudeCenter, it.longitudeCenter),
                            it.radius.toDouble(),
                            noGoStrokeColor,
                            noGoFillColor
                        )
                    } else if (it.fenceShapeType == ConstantInt.RectangleShapeType) {
                        if (it.fenceType == ConstantInt.SafeZone) mapFragment.addRectFence(
                            it, saveStrokeColor, saveFillColor
                        )
                        else mapFragment.addRectFence(
                            it, noGoStrokeColor, noGoFillColor
                        )
                    } else if (it.fenceShapeType == ConstantInt.PolygonShapeType) {
                        if (it.fenceType == ConstantInt.SafeZone) mapFragment.addPolygonFence(
                            it, saveStrokeColor, saveFillColor
                        )
                        else mapFragment.addPolygonFence(
                            it, noGoStrokeColor, noGoFillColor
                        )
                    }
                } else if (mapFragment is BaseBaiduMapFragment) {
                    if (it.fenceShapeType == ConstantInt.CircleShapeType) {
                        if (it.fenceType == ConstantInt.SafeZone) mapFragment.addCircleFence(
                            it.latitudeCenter,
                            it.longitudeCenter,
                            it.radius.toDouble(),
                            saveStrokeColor,
                            saveFillColor
                        )
                        else mapFragment.addCircleFence(
                            it.latitudeCenter,
                            it.longitudeCenter,
                            it.radius.toDouble(),
                            noGoStrokeColor,
                            noGoFillColor
                        )
                    } else if (it.fenceShapeType == ConstantInt.RectangleShapeType) {
                        if (it.fenceType == ConstantInt.SafeZone) mapFragment.addRectFence(
                            it, saveStrokeColor, saveFillColor
                        )
                        else mapFragment.addRectFence(
                            it, noGoStrokeColor, noGoFillColor
                        )
                    } else if (it.fenceShapeType == ConstantInt.PolygonShapeType) {
                        if (it.fenceType == ConstantInt.SafeZone) mapFragment.addPolygonFence(
                            it, saveStrokeColor, saveFillColor
                        )
                        else mapFragment.addPolygonFence(
                            it, noGoStrokeColor, noGoFillColor
                        )
                    }
                }
            }
        }
    }

    /**
     * 设置地图虚实线围栏和图片类型
     */
    fun setImageFencesData(
        context: Context,
        fencesList: MutableList<FencesBean>?,
        mapFragment: Fragment,
        currentFencesId: String = "",
        isSatelliteMap: Boolean = false
    ) {
        if (mapFragment is BaseGoogleMapFragment) {
            mapFragment.clearAllMarker()
        } else if (mapFragment is BaseBaiduMapFragment) {
            mapFragment.clearAllMarker()
        }

        if (null == fencesList) return
        if (fencesList.size == 0) return

        val saveFillColor = ContextCompat.getColor(context, R.color.select_color4)
        var saveStrokeColor = ContextCompat.getColor(context, R.color.select_color)
        val noGoFillColor = ContextCompat.getColor(context, R.color.red_color3)
        val noGoStrokeColor = ContextCompat.getColor(context, R.color.red_color2)
        val closeFillColor = ContextCompat.getColor(context, R.color.black5)
        val closeStrokeColor = ContextCompat.getColor(context, R.color.black20)

        if (isSatelliteMap) saveStrokeColor = ContextCompat.getColor(context, R.color.select_color2)

        fencesList.forEach {
            //是否画虚线
            var isDashed = false
            val imageTypeResId = if (it.fenceId == currentFencesId) {
                isDashed = true
                if (it.fenceType == ConstantInt.NoGoZone) R.drawable.icon_fences_zone_gps_red_svg
                else if (isSatelliteMap) R.drawable.icon_fences_zone_gps_green_svg
                else R.drawable.icon_fences_zone_gps_svg
            } else getFencesImageTypeResId(
                it.fenceNameType
            )
            val strokeColor: Int
            val fillColor: Int
            if (it.isOn == ConstantInt.Close) {
                strokeColor = closeStrokeColor
                fillColor = closeFillColor
            } else if (it.fenceType == ConstantInt.SafeZone) {
                strokeColor = saveStrokeColor
                fillColor = saveFillColor
            } else {
                strokeColor = noGoStrokeColor
                fillColor = noGoFillColor
            }

            if (mapFragment is BaseGoogleMapFragment) {
                if (it.fenceShapeType == ConstantInt.CircleShapeType) {
                    val centerLatLng = LatLng(it.latitudeCenter, it.longitudeCenter)
                    mapFragment.addCircleImageFence(
                        centerLatLng,
                        it.radius.toDouble(),
                        imageTypeResId,
                        strokeColor,
                        fillColor,
                        isDashed
                    )
                } else if (it.fenceShapeType == ConstantInt.RectangleShapeType) {
                    if (it.fenceId == currentFencesId) {
                        mapFragment.addRectImageEditFence(
                            it, imageTypeResId, strokeColor, fillColor
                        )
                    } else {
                        mapFragment.addRectImageFence(
                            it, imageTypeResId, strokeColor, fillColor, false
                        )
                    }
                } else if (it.fenceShapeType == ConstantInt.PolygonShapeType) {
                    if (it.fenceId == currentFencesId) {
                        mapFragment.addPolygonImageEditFence(
                            it, imageTypeResId, strokeColor, fillColor
                        )
                    } else {
                        mapFragment.addPolygonImageFence(
                            it, imageTypeResId, strokeColor, fillColor, false
                        )
                    }
                }
            } else if (mapFragment is BaseBaiduMapFragment) {
                if (it.fenceShapeType == ConstantInt.CircleShapeType) {
                    mapFragment.addCircleDashedAndImageFence(
                        it.latitudeCenter,
                        it.longitudeCenter,
                        it.radius.toDouble(),
                        imageTypeResId,
                        strokeColor,
                        fillColor
                    )
                } else if (it.fenceShapeType == ConstantInt.RectangleShapeType) {
                    if (it.fenceId == currentFencesId) {
                        mapFragment.addRectDashedAndImageEditFence(
                            it, imageTypeResId, strokeColor, fillColor
                        )
                    } else {
                        mapFragment.addRectDashedAndImageFence(
                            it, imageTypeResId, strokeColor, fillColor
                        )
                    }
                } else if (it.fenceShapeType == ConstantInt.PolygonShapeType) {
                    if (it.fenceId == currentFencesId) {
                        mapFragment.addPolygonDashedAndImageEditFence(
                            it, imageTypeResId, strokeColor, fillColor
                        )
                    } else {
                        mapFragment.addPolygonDashedAndImageFence(
                            it, imageTypeResId, strokeColor, fillColor
                        )
                    }
                }
            }
        }
    }

    private fun getFencesImageTypeResId(type: Int): Int {
        var imageTypeResId = 0
        when (type) {
            1 -> imageTypeResId = R.drawable.icon_save_zone_type1_small_svg
            2 -> imageTypeResId = R.drawable.icon_save_zone_type2_small_svg
            3 -> imageTypeResId = R.drawable.icon_save_zone_type3_small_svg
            4 -> imageTypeResId = R.drawable.icon_save_zone_type4_small_svg
            5 -> imageTypeResId = R.drawable.icon_save_zone_type5_small_svg
            6 -> imageTypeResId = R.drawable.icon_no_zone_type1_small_svg
            7 -> imageTypeResId = R.drawable.icon_no_zone_type2_small_svg
            8 -> imageTypeResId = R.drawable.icon_no_zone_type3_small_svg
        }
        return imageTypeResId
    }
}