package com.abbidot.tracker.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.FencesBean
import com.abbidot.tracker.bean.GetFencesBean
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2022/3/1/001.
 * @link
 * @description:
 */
class FencesManageViewModel : ViewModel() {

    val mFencesAddLiveData = MutableLiveData<Result<FencesBean>>()
    val mDeleteFenceLiveData = MutableLiveData<Result<String>>()
    val mUpdateFenceLiveData = MutableLiveData<Result<String>>()
    val mFencesLiveData = MutableLiveData<Result<GetFencesBean>>()

    /**
     *获取围栏列表
     */
    fun getFenceList(activity: BaseActivity<*>, deviceId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getFenceList(deviceId)
            mFencesLiveData.value = result
        }
    }

    fun addCircleFence(activity: BaseActivity<*>, fencesBean: FencesBean) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.addCircleFence(fencesBean)
            mFencesAddLiveData.value = result
        }
    }

    fun addRectFence(activity: BaseActivity<*>, fencesBean: FencesBean) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.addRectFence(fencesBean)
            mFencesAddLiveData.value = result
        }
    }

    fun addPolygonFence(activity: BaseActivity<*>, fencesBean: FencesBean) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.addPolygonFence(fencesBean)
            mFencesAddLiveData.value = result
        }
    }

    fun updateCircleFence(
        activity: BaseActivity<*>, fencesBean: FencesBean
    ) {
        activity.showLoading(true)
        fencesBean.apply {
            viewModelScope.launch {
                val result = NetworkApi.updateCircleFence(
                    deviceId,
                    deviceServiceId,
                    name,
                    address,
                    imgurl,
                    isOn,
                    radius,
                    longitudeCenter,
                    latitudeCenter,
                    fenceNameType,
                    fenceType,
                    fenceId,
                    ancillaryLatitude,
                    ancillaryLongitude,
                    ancillaryOtherLatitude,
                    ancillaryOtherLongitude,
                    modeType
                )
                mUpdateFenceLiveData.value = result
            }
        }
    }

    fun updateRectFence(
        activity: BaseActivity<*>, fencesBean: FencesBean
    ) {
        activity.showLoading(true)
        fencesBean.apply {
            viewModelScope.launch {
                val result = NetworkApi.updateRectFence(
                    deviceId,
                    deviceServiceId,
                    name,
                    address,
                    imgurl,
                    isOn,
                    fenceNameType,
                    fenceType,
                    fenceId,
                    longitudeA,
                    latitudeA,
                    longitudeB,
                    latitudeB,
                    longitudeC,
                    latitudeC,
                    longitudeD,
                    latitudeD,
                    longitudeCenter,
                    latitudeCenter,
                    longDistance,
                    shortDistance,
                    modeType
                )
                mUpdateFenceLiveData.value = result
            }
        }
    }

    /**
     * 更新多边形围栏
     */
    fun updatePolygonFence(activity: BaseActivity<*>, fencesBean: FencesBean) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.updatePolygonFence(fencesBean)
            mUpdateFenceLiveData.value = result
        }
    }

    fun deleteFence(activity: BaseActivity<*>, fenceId: String, deviceServiceId: String,modeType:Int) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.removeFence(fenceId, deviceServiceId,modeType)
            mDeleteFenceLiveData.value = result
        }
    }
}