package com.lanyun.wanglaotou.viewModel

import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.lanyun.common.base.BaseViewModel
import com.lanyun.net.ext.onBizOK
import com.lanyun.net.ext.onFailure
import com.lanyun.net.ext.onSuccess
import com.lanyun.net.ext.serverData
import com.lanyun.wanglaotou.model.GuestPlan
import com.lanyun.wanglaotou.model.UserSubModel
import com.lanyun.wanglaotou.public.PageState
import com.lanyun.wanglaotou.repo.GuestRepo

class WebViewModel : BaseViewModel() {

    var viewStates by mutableStateOf(WebViewState())
        private set

    fun dispatch(action: WebViewAction) {
        when (action) {
            is WebViewAction.FetchData -> getGuest(action.url)
        }
    }

    fun getGuest(url: String) = serverAwait{
        viewStates = viewStates.copy(pageState = PageState.Loading)
        GuestRepo.getGuestRedirect(url).serverData().onSuccess {
            onBizOK<String> { code, data, error_message ->

                data?.let {
                    viewStates = viewStates.copy(
                        url = it,
                        pageState = PageState.Success(it.isEmpty())
                    )
                }

            }
        }.onFailure {
            hidLoading()
            viewStates = viewStates.copy(pageState = PageState.Error(it.message ?: ""))
        }
    }

}


data class WebViewState(
    val url: String = "",
    val pageState: PageState = PageState.Loading,

)


sealed class WebViewAction() {
    data class FetchData(val url: String) : WebViewAction()
}
