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.WiFiZoneBean
import com.abbidot.tracker.bean.WiFiZoneListBean
import com.abbidot.tracker.retrofit2.NetworkApi
import kotlinx.coroutines.launch

/**
 *Created  by .yzq on 2024/6/5/005.
 * @link
 * @description:
 */
class WiFiZoneViewModel : ViewModel() {

    val mWiFiZoneListLiveData = MutableLiveData<Result<WiFiZoneListBean>>()
    val mAddWiFiZoneLiveData = MutableLiveData<Result<String>>()
    val mUpdateWiFiZoneLiveData = MutableLiveData<Result<String>>()
    val mDeleteWiFiZoneLiveData = MutableLiveData<Result<String>>()

    /**
     *添加wifi区域
     */
    fun addWifiZone(activity: BaseActivity<*>, wiFiZoneBean: WiFiZoneBean) {
        activity.showLoading(true)
        viewModelScope.launch {
            wiFiZoneBean.apply {
                val result = NetworkApi.addWifiZone(
                    deviceId,
                    wifiZoneName,
                    address,
                    wifiMac,
                    wifiName,
                    longitude,
                    latitude,
                    wifiPass,
                    wifiAuth,
                    type
                )
                mAddWiFiZoneLiveData.value = result
            }
        }
    }

    /**
     *更新wifi区域
     */
    fun updateWifiZone(activity: BaseActivity<*>, wiFiZoneBean: WiFiZoneBean) {
        activity.showLoading(true)
        viewModelScope.launch {
            wiFiZoneBean.apply {
                val result = NetworkApi.updateWifiZone(
                    wifiZoneId,
                    deviceId,
                    wifiZoneName,
                    address,
                    wifiMac,
                    wifiName,
                    longitude,
                    latitude,
                    type
                )
                mUpdateWiFiZoneLiveData.value = result
            }
        }
    }

    /**
     *获取wifi区域列表
     */
    fun deleteWifiZone(activity: BaseActivity<*>, wiFiZoneBean: WiFiZoneBean) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.removeWifiZone(wiFiZoneBean.wifiZoneId)
            mDeleteWiFiZoneLiveData.value = result
        }
    }

    /**
     *获取wifi区域列表
     */
    fun getWifiZoneList(activity: BaseActivity<*>, deviceId: String) {
        activity.showLoading(true)
        viewModelScope.launch {
            val result = NetworkApi.getWifiZoneList(deviceId)
            mWiFiZoneListLiveData.value = result
        }
    }
}