package com.zaojiao.app.feat.design

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.wrapContentHeight
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<out T> {
    object Loading : UiState<Nothing>

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

    data class Failure(val code: Int) : UiState<Nothing>
}

@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 -> {
            Box(
                modifier = Modifier.fillMaxSize(),
            ) {
                Column(
                    modifier = Modifier
                        .align(Alignment.Center)
                        .wrapContentHeight()
                        .fillMaxWidth(),
                    horizontalAlignment = Alignment.CenterHorizontally,
                ) {
                    Text(
                        text = "error state",
                        style = TextStyle(
                            fontSize = 18.sp,
                            color = Color.Red,
                        )
                    )

                    Text(
                        text = "code: ${uiState.code}",
                        style = TextStyle(
                            fontSize = 18.sp,
                            color = Color.Red,
                        )
                    )
                }
            }
        }
    }
}