package com.meowarex.rlmobile.ui.screens.about

import android.os.Parcelable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.koin.koinScreenModel
import com.meowarex.rlmobile.BuildConfig
import com.meowarex.rlmobile.R
import com.meowarex.rlmobile.ui.components.*
import com.meowarex.rlmobile.ui.screens.about.components.LeadContributor
import com.meowarex.rlmobile.ui.util.paddings.*
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize

@Parcelize
class AboutScreen : Screen, Parcelable {
    @IgnoredOnParcel
    override val key = "About"

    @Composable
    override fun Content() {
        val model = koinScreenModel<AboutModel>()

        AboutScreenContent(state = model.state.collectAsState())
    }
}

@Composable
fun AboutScreenContent(state: State<AboutScreenState>) {
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(stringResource(R.string.navigation_about)) },
                navigationIcon = { BackButton() },
            )
        }
    ) { paddingValues ->
        LazyColumn(
            verticalArrangement = Arrangement.spacedBy(2.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            contentPadding = paddingValues
                .exclude(PaddingValuesSides.Horizontal + PaddingValuesSides.Top)
                .add(PaddingValues(vertical = 16.dp)),
            modifier = Modifier
                .padding(paddingValues.exclude(PaddingValuesSides.Bottom))
                .padding(horizontal = 14.dp),
        ) {
            item(key = "PROJECT_HEADER") {
                ProjectHeader()
            }

            item(key = "HEADER_DIVIDER") {
                TextDivider(
                    text = stringResource(R.string.contributors_lead),
                    modifier = Modifier.padding(top = 18.dp, bottom = 20.dp),
                )
            }

            item(key = "MAIN_CONTRIBUTORS") {
                Row(
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.fillMaxWidth(),
                ) {
                    LeadContributor(BuildConfig.PATCHES_REPO_OWNER, "Radiant Lyrics")
                }
            }

            item(key = "CONTRIBUTORS_DIVIDER") {
                TextDivider(
                    text = stringResource(R.string.contributors),
                    modifier = Modifier.padding(top = 16.dp, bottom = 6.dp)
                )
            }

            when (val state = state.value) {
                AboutScreenState.Loading -> item(key = "CONTRIBUTIONS_LOADING") {
                    Box(
                        contentAlignment = Alignment.Center,
                        content = { CircularProgressIndicator() },
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(vertical = 38.dp),
                    )
                }

                AboutScreenState.Failure -> item(key = "LOAD_FAILURE") {
                    LoadFailure(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(vertical = 38.dp)
                    )
                }

                is AboutScreenState.Loaded -> {
                    val others = state.contributors.filter { it.username != BuildConfig.PATCHES_REPO_OWNER }
                    items(others, key = { it.username }) { user ->
                        ContributorCommitsItem(user)
                    }
                }
            }
        }
    }
}
