package com.casic.br.vm

import androidx.lifecycle.MutableLiveData
import com.casic.br.base.BaseApplication
import com.casic.br.extensions.separateResponseCode
import com.casic.br.extensions.toErrorMessage
import com.casic.br.model.CommonResultModel
import com.casic.br.model.LoginResultModel
import com.casic.br.model.UserDetailModel
import com.casic.br.utils.LocaleConstant
import com.casic.br.utils.retrofit.RetrofitServiceManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.extensions.launch
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.utils.SaveKeyValues
import com.pengxh.kt.lite.vm.BaseViewModel
import com.pengxh.kt.lite.vm.LoadState

class UserViewModel : BaseViewModel() {

    private val gson by lazy { Gson() }
    val registerResult = MutableLiveData<CommonResultModel>()
    val loginResult = MutableLiveData<LoginResultModel>()
    val userDetailResult = MutableLiveData<UserDetailModel>()
    val outResultModel = MutableLiveData<CommonResultModel>()

    fun register(
        account: String,
        password: String,
        positionLng: Double,
        positionLat: Double,
        clientId: String,
        syncName: String,
        syncDeptid: String
    ) = launch({
        val response = RetrofitServiceManager.register(
            account, password, positionLng, positionLat, clientId, syncName, syncDeptid
        )
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            loadState.value = LoadState.Success
            registerResult.value = gson.fromJson<CommonResultModel>(
                response, object : TypeToken<CommonResultModel>() {}.type
            )
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        loadState.value = LoadState.Fail
        it.printStackTrace()
    })

    fun enter(sid: String, account: String, secretKey: String) = launch({
        //记录登录时间，用于判断token失效
        SaveKeyValues.putValue(LocaleConstant.LOGIN_TIME, System.currentTimeMillis())
        val response = RetrofitServiceManager.login(sid, account, secretKey)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            loadState.value = LoadState.Success
            loginResult.value = gson.fromJson<LoginResultModel>(
                response, object : TypeToken<LoginResultModel>() {}.type
            )
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        loadState.value = LoadState.Fail
        it.printStackTrace()
    })

    fun obtainUserDetail(account: String) = launch({
        val response = RetrofitServiceManager.obtainUserDetail(account)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            userDetailResult.value = gson.fromJson<UserDetailModel>(
                response, object : TypeToken<UserDetailModel>() {}.type
            )
        }
    }, {
        it.printStackTrace()
    })

    fun updateUserInfo(userData: UserDetailModel.DataModel) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.updateUserInfo(userData)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            loadState.value = LoadState.Success
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        loadState.value = LoadState.Fail
        it.printStackTrace()
    })

    fun out() = launch({
        val response = RetrofitServiceManager.loginOut()
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            outResultModel.value = gson.fromJson<CommonResultModel>(
                response, object : TypeToken<CommonResultModel>() {}.type
            )
        } else {
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        it.printStackTrace()
    })
}