package com.adealink.weparty.playmatesetting.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.commonui.ext.isSuccess
import com.adealink.weparty.module.playmate.data.PlaymateDetailData
import com.adealink.weparty.playmatesetting.data.PlaymateSettingFormData
import com.adealink.weparty.playmatesetting.data.PlaymateSettingFormReq
import com.adealink.weparty.playmatesetting.data.PlaymateSettingFormSubmitReq
import com.adealink.weparty.playmatesetting.datasource.remote.PlaymateSettingHttpService
import com.adealink.weparty.module.widget.form.BaseWidgetView
import com.adealink.weparty.module.widget.form.data.StepFieldValue
import kotlinx.coroutines.launch

class PlaymateSettingViewModel : BaseViewModel() {


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


    private var playmateDetailData: PlaymateDetailData? = null
    val selectCategoryLD = ExtMutableLiveData<PlaymateDetailData?>()

    val categoryFormLD = ExtMutableLiveData<PlaymateSettingFormData>()
    fun selectCategory(playmateData: PlaymateDetailData): LiveData<Rlt<PlaymateSettingFormData>> {
        val liveData = OnceMutableLiveData<Rlt<PlaymateSettingFormData>>()
        viewModelScope.launch {
            val rlt = settingHttpService.getEditForm(
                PlaymateSettingFormReq(playmateData.playmateId)
            )
            when (rlt) {
                is Rlt.Failed -> {
                    this@PlaymateSettingViewModel.playmateDetailData = 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@PlaymateSettingViewModel.playmateDetailData = playmateData
                    selectCategoryLD.send(playmateData)
                    categoryFormLD.send(data)
                    liveData.send(Rlt.Success(data))
                }
            }
        }
        return liveData
    }

    val goUnderReviewPageLD = ExtMutableLiveData<Unit>()
    val submitFinishLD = ExtMutableLiveData<Unit>()
    fun submitForm(formList: List<BaseWidgetView<*>>): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val playmateData = playmateDetailData
            if (playmateData == 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
                }
            }

            var needReview = false
            val formValueList = mutableListOf<StepFieldValue>()
            for (formItem in formList) {
                val fieldValue = formItem.getFieldValue() ?: continue
                formValueList.add(fieldValue)
                if (formItem.widgetData?.fieldData?.isFieldNeedReview() == true) {
                    //需要审核项目有改动, 要设置为true
                    if (formItem.hasEditFieldValue()) {
                        needReview = true
                    }
                }
            }

            val submitRlt = settingHttpService.submitEdit(
                PlaymateSettingFormSubmitReq(
                    playmateId = playmateData.playmateId,
                    needReview = needReview,
                    formValueList
                )
            )
            liveData.send(submitRlt)
            //提交审核成功,跳转审核页面
            if (submitRlt.isSuccess) {
                if (needReview) {
                    goUnderReviewPageLD.send(Unit)
                } else {
                    submitFinishLD.send(Unit)
                }
            }
        }
        return liveData
    }

}