package com.zaojiao.app.feat.design

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.sp

sealed interface UiState<T> {
    class Loading<T> : UiState<T>

    data class Success<T>(val state: T) : UiState<T>

    sealed interface Failure<T> : UiState<T> {
        class NetworkError<T> : Failure<T>
    }
}

@Composable
fun <T> StatePage(uiState: UiState<T>, onSuccess: @Composable T.() -> Unit) {
    when (uiState) {
        is UiState.Loading -> {
            Box(
                modifier = Modifier.fillMaxSize(),
            ) {
                Text(
                    text = "idle state",
                    modifier = Modifier.align(Alignment.Center),
                    style = TextStyle(
                        fontSize = 18.sp,
                        color = Color.Green,
                    )
                )
            }
        }

        is UiState.Success -> {
            onSuccess.invoke(uiState.state)
        }

        is UiState.Failure -> {
            when (uiState) {
                is UiState.Failure.NetworkError -> {
                    Box(
                        modifier = Modifier.fillMaxSize(),
                    ) {
                        Text(
                            text = "error state",
                            modifier = Modifier.align(Alignment.Center),
                            style = TextStyle(
                                fontSize = 18.sp,
                                color = Color.Red,
                            )
                        )
                    }
                }
            }
        }
    }
}