package com.adealink.weparty.joinus.step3.viewmodel

import androidx.lifecycle.LiveData
import com.adealink.frame.base.CommonDataNullError
import com.adealink.frame.base.IError
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.JoinStep3
import com.adealink.weparty.joinus.datasource.remote.JoinUsHttpService
import com.adealink.weparty.joinus.step3.data.CategoryFormReq
import com.adealink.weparty.joinus.step3.data.Step3Page
import com.adealink.weparty.joinus.step3.data.SubmitFormReq
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.module.widget.form.BaseWidgetView
import com.adealink.weparty.module.widget.form.data.StepFieldValue
import kotlinx.coroutines.launch

class Step3ViewModel : BaseViewModel() {


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

    val pageLD = ExtMutableLiveData<Step3Page>()

    fun showPage(page: Step3Page) {
        pageLD.send(page)
    }

    private var selectCategory: PlaymateCategoryData? = null
    val selectCategoryLD = ExtMutableLiveData<PlaymateCategoryData?>()

    val categoryFormLD = ExtMutableLiveData<JoinStep3>()

    fun selectCategory(selectCategory: PlaymateCategoryData): LiveData<Rlt<JoinStep3>> {
        val liveData = OnceMutableLiveData<Rlt<JoinStep3>>()
        viewModelScope.launch {
            val rlt = joinUsHttpService.getCategoryFormConfig(CategoryFormReq(selectCategory.code))
            when (rlt) {
                is Rlt.Failed -> {
                    this@Step3ViewModel.selectCategory = null
                    selectCategoryLD.send(null)
                    liveData.send(rlt)
                    return@launch
                }

                is Rlt.Success -> {
                    val data = rlt.data.data
                    if (data == null) {
                        liveData.send(Rlt.Failed(CommonDataNullError()))
                        return@launch
                    }

                    this@Step3ViewModel.selectCategory = selectCategory
                    selectCategoryLD.send(selectCategory)
                    categoryFormLD.send(data)
                    liveData.send(Rlt.Success(data))
                }
            }
        }
        return liveData
    }

    fun submitForm(formList: List<BaseWidgetView<*>>): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val selectCategory = selectCategory
            if (selectCategory == null) {
                liveData.send(Rlt.Failed(IError()))
                return@launch
            }

            //执行校验
            for (formItem in formList) {
                val rlt = formItem.validate()
                if (rlt is Rlt.Failed) {
                    liveData.send(rlt)
                    return@launch
                }
            }

            //提交前的检查
            for (formItem in formList) {
                val rlt = formItem.beforeSubmit()
                if (rlt is Rlt.Failed) {
                    liveData.send(rlt)
                    return@launch
                }
            }

            val formValueList = mutableListOf<StepFieldValue>()
            for (formItem in formList) {
                val fieldValue = formItem.getFieldValue() ?: continue
                formValueList.add(fieldValue)
            }

            val submitRlt = joinUsHttpService.submitPlaymateApplyForm(
                SubmitFormReq(
                    selectCategory.code,
                    formValueList
                )
            )
            liveData.send(submitRlt)
        }
        return liveData
    }

}