package com.xhly.manageapp.ui.main.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.xhly.corelib.base.viewmodel.CommonBaseViewModel
import com.xhly.corelib.network.RetrofitService
import com.xhly.manageapp.bean.AppModel
import com.xhly.manageapp.bean.ListAppBean
import com.xhly.manageapp.bean.log.LogdOperateBean
import com.xhly.manageapp.bean.strategy.StrategyBean
import com.xhly.manageapp.network.app.AppService
import com.xhly.manageapp.network.log.LogService
import com.xhly.manageapp.network.strategy.StrategyService
import kotlinx.coroutines.launch

class MainViewModel : CommonBaseViewModel() {
    private val appService by lazy { RetrofitService.create<AppService>() }
    private val strategyService by lazy { RetrofitService.create<StrategyService>() }
    private var logService = RetrofitService.create<LogService>()
    private val listData = MutableLiveData<ArrayList<AppModel>>()
    val listAppData: LiveData<ArrayList<AppModel>> = listData
    private val strategy=MutableLiveData<StrategyBean>()
    val strategyData=strategy
    fun getAppList() {
        launchUI {
            val postListApp = appService.postListApp(ListAppBean())
            if (postListApp.code == 0) {
                if (postListApp.obj != null && postListApp.obj.isNotEmpty()) {
                    listData.value = postListApp.obj
                } else {
                    listData.value = arrayListOf()
                }
            } else {
                if (!postListApp.msg.isNullOrEmpty()) {
                    netMessage.value = postListApp.msg
                }
            }
        }
    }

    fun updateStrategy() {
          launchUI {
              val postDetailPad = strategyService.postDetailPad()
              if (postDetailPad.code == 0) {
                  if (postDetailPad.obj != null) {
                     strategy.value=postDetailPad.obj
                  }
              } else {
                  if (!postDetailPad.msg.isNullOrEmpty()) {
                      netMessage.value = postDetailPad.msg
                  }
              }
          }
    }

     fun eventLog(operateBean: LogdOperateBean) {
         launchUI {
             logService.postLogdoperateAdd(operateBean)
         }
    }
}