package com.pixelized.biblib.ui.screen.home.profile

import android.content.Intent
import android.content.res.Configuration.UI_MODE_NIGHT_NO
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.net.Uri
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.pixelized.biblib.R
import com.pixelized.biblib.network.client.IBibLibClient
import com.pixelized.biblib.ui.navigation.LocalScreenNavHostController
import com.pixelized.biblib.ui.navigation.navigateToAuthentication
import com.pixelized.biblib.ui.theme.BibLibTheme
import com.pixelized.biblib.utils.extention.placeholder

@Stable
@Immutable
data class UserUio(
    val username: String,
    val firstname: String?,
    val lastname: String?,
    val amazonEmails: List<String>,
)

@Composable
fun ProfilePage(
    viewModel: ProfileViewModel = hiltViewModel(),
) {
    val context = LocalContext.current
    val navigation = LocalScreenNavHostController.current

    Card {
        ProfileScreenContent(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            user = viewModel.user,
            onEditClick = {
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(IBibLibClient.EDIT_PROFILE))
                context.startActivity(intent)
            },
            onLogoutClick = {
                viewModel.logout()
                navigation.navigateToAuthentication()
            }
        )
    }
}

@Composable
private fun ProfileScreenContent(
    modifier: Modifier = Modifier,
    user: State<UserUio?>,
    onEditClick: () -> Unit,
    onLogoutClick: () -> Unit,
) {
    val placeholder: Boolean by remember(user) {
        derivedStateOf { user.value == null }
    }

    Column(
        modifier = modifier,
        verticalArrangement = Arrangement.spacedBy(4.dp),
    ) {
        Text(
            style = MaterialTheme.typography.body1,
            color = MaterialTheme.colors.onSurface,
            text = stringResource(id = R.string.profile_title)
        )

        Text(
            modifier = Modifier.placeholder { placeholder },
            style = MaterialTheme.typography.h6,
            color = MaterialTheme.colors.primary,
            text = user.value?.username ?: "DefinitelyNotARobot",
        )

        Row(
            horizontalArrangement = Arrangement.spacedBy(4.dp),
        ) {
            Text(
                modifier = Modifier.placeholder { placeholder },
                style = MaterialTheme.typography.body1,
                color = MaterialTheme.colors.onSurface,
                text = user.value?.firstname ?: if (placeholder) "R. Daneel" else "",
            )

            Text(
                modifier = Modifier.placeholder { placeholder },
                style = MaterialTheme.typography.body1,
                color = MaterialTheme.colors.onSurface,
                text = user.value?.lastname ?: if (placeholder) "Olivaw" else "",
            )
        }

        Text(
            modifier = Modifier.padding(top = 16.dp),
            style = MaterialTheme.typography.body1,
            color = MaterialTheme.colors.onSurface,
            text = stringResource(id = R.string.profile_emails)
        )

        LazyColumn {
            if (user.value?.amazonEmails?.isNotEmpty() == true) {
                items(
                    items = user.value?.amazonEmails ?: emptyList()
                ) {
                    Text(
                        style = MaterialTheme.typography.caption,
                        color = MaterialTheme.colors.onSurface,
                        text = it,
                    )
                }
            } else {
                item {
                    Text(
                        modifier = Modifier.placeholder { placeholder },
                        style = MaterialTheme.typography.caption,
                        color = MaterialTheme.colors.error,
                        text = stringResource(id = R.string.profile_emails_empty),
                    )
                }
            }
        }

        Row(
            modifier = Modifier
                .padding(top = 16.dp)
                .align(Alignment.End),
            horizontalArrangement = Arrangement.spacedBy(4.dp),
        ) {
            Button(
                colors = ButtonDefaults.outlinedButtonColors(),
                onClick = onEditClick,
            ) {
                Text(
                    text = stringResource(id = R.string.profile_edit_action)
                )
            }

            Button(
                colors = ButtonDefaults.buttonColors(),
                onClick = onLogoutClick,
            ) {
                Text(
                    text = stringResource(id = R.string.profile_logout_action)
                )
            }
        }
    }
}

@Composable
@Preview(showBackground = true, uiMode = UI_MODE_NIGHT_NO)
@Preview(showBackground = true, uiMode = UI_MODE_NIGHT_YES)
private fun ProfileScreenContentPreview(
    @PreviewParameter(ProfilePreviewProvider::class) preview: State<UserUio?>
) {
    BibLibTheme {
        Box {
            ProfileScreenContent(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(all = 16.dp),
                user = preview,
                onEditClick = { },
                onLogoutClick = { },
            )
        }
    }
}

class ProfilePreviewProvider : PreviewParameterProvider<State<UserUio?>> {
    override val values: Sequence<State<UserUio?>> = sequenceOf(
        mutableStateOf(null),
        mutableStateOf(
            UserUio(
                username = "DefinitelyNotARobot",
                firstname = null,
                lastname = null,
                amazonEmails = emptyList(),
            )
        ),
        mutableStateOf(
            UserUio(
                username = "DefinitelyNotARobot",
                firstname = "R. Daneel",
                lastname = "Olivaw",
                amazonEmails = listOf(
                    "r.daneel.olivaw@robot.com",
                    "r.daneel.olivaw@biblib.com",
                ),
            )
        ),
    )
}