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.AddressListModel
import com.casic.br.model.CommonResultModel
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.vm.BaseViewModel
import com.pengxh.kt.lite.vm.LoadState

class AddressViewModel : BaseViewModel() {

    private val gson by lazy { Gson() }
    val listModel = MutableLiveData<AddressListModel>()
    val deleteResult = MutableLiveData<CommonResultModel>()

    fun addAddress(
        account: String, province: String, city: String,
        area: String, street: String, detailAddress: String,
        postalCode: String
    ) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.addAddress(
            account,
            province,
            city,
            area,
            street,
            detailAddress,
            postalCode
        )
        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 obtainAddressListByPage(
        account: String, province: String, city: String, area: String, offset: Int
    ) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.obtainAddressListByPage(
            account, province, city, area, offset
        )
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            loadState.value = LoadState.Success
            listModel.value = gson.fromJson<AddressListModel>(
                response, object : TypeToken<AddressListModel>() {}.type
            )
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        loadState.value = LoadState.Fail
        it.printStackTrace()
    })

    fun deleteAddressById(id: String) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.deleteAddressById(id)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            loadState.value = LoadState.Success
            deleteResult.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 updateAddressById(
        id: String, account: String, province: String, city: String,
        area: String, street: String, detailAddress: String,
        postalCode: String
    ) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.updateAddressById(
            id,
            account,
            province,
            city,
            area,
            street,
            detailAddress,
            postalCode
        )
        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()
    })
}