package com.adealink.weparty.joinus.viewmodel

import androidx.lifecycle.LiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.App
import com.adealink.weparty.joinus.data.JoinUsData
import com.adealink.weparty.joinus.data.JoinUsStep
import com.adealink.weparty.joinus.datasource.remote.JoinUsHttpService
import com.adealink.weparty.module.joinus.viewmodel.IJoinUsViewModel
import kotlinx.coroutines.launch

class JoinUsViewModel : BaseViewModel(), IJoinUsViewModel {

    private val joinUsHttpService by lazy {
        App.instance.networkService.getHttpService(JoinUsHttpService::class.java)
    }

    val stepLD = ExtMutableLiveData<JoinUsStep>()

    fun goStep(step: JoinUsStep) {
        stepLD.send(step)
    }

    val joinProgressLD = ExtMutableLiveData<JoinUsData?>()

    fun setJoinProgress(data: JoinUsData?) {
        joinProgressLD.send(data)
    }

    fun loadApplyProgress(): LiveData<Rlt<JoinUsData?>> {
        val liveData = OnceMutableLiveData<Rlt<JoinUsData?>>()
        viewModelScope.launch {
            val rlt = joinUsHttpService.loadJoinUsProgress()
            when (rlt) {
                is Rlt.Failed -> {
                    //Ntd.
                    liveData.send(rlt)
                }

                is Rlt.Success -> {
                    val progressData = rlt.data.data
                    joinProgressLD.send(progressData)
                    liveData.send(Rlt.Success(progressData))
                }
            }
        }
        return liveData
    }

    fun updateApplyProgress(data: JoinUsData?) {
        joinProgressLD.send(data)
    }

}