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

import android.content.res.Configuration
import androidx.compose.animation.*
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.pixelized.biblib.R
import com.pixelized.biblib.ui.theme.BibLibTheme
import com.pixelized.biblib.ui.theme.color.BibLibColorPalette
import com.pixelized.biblib.ui.theme.color.ShadowPalette
import com.pixelized.biblib.utils.extention.bibLib
import kotlinx.coroutines.flow.Flow

@Composable
fun DetailScreenSendLoader(
    loadingVisibility: Flow<Boolean>,
    successVisibility: Flow<Boolean>,
) {
    val isLoadingVisible by successVisibility.collectAsState(initial = false)
    val isSuccessVisible by loadingVisibility.collectAsState(initial = false)

    DetailScreenSendLoaderContent(
        modifier = Modifier.fillMaxSize(),
        isLoadingVisible = isLoadingVisible,
        isSuccessVisible = isSuccessVisible,
    )
}

@Composable
fun DetailScreenSendLoaderContent(
    modifier: Modifier = Modifier,
    isLoadingVisible: Boolean,
    isSuccessVisible: Boolean,
) {
    Box(modifier = modifier) {
        AnimatedVisibility(
            visible = isLoadingVisible,
            enter = fadeIn(),
            exit = fadeOut(),
        ) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(color = ShadowPalette.scrim)
                    .padding(all = 16.dp),
            )
        }

        AnimatedVisibility(
            modifier = Modifier.align(Alignment.Center),
            visible = isLoadingVisible,
            enter = fadeIn() + slideInVertically { it },
            exit = fadeOut() + slideOutVertically { it },
        ) {
            CircularProgressIndicator(
                modifier = Modifier.size(MaterialTheme.bibLib.dimen.progress.iconSize),
            )
        }

        AnimatedVisibility(
            modifier = Modifier.align(alignment = Alignment.BottomCenter),
            visible = isSuccessVisible,
            enter = fadeIn() + slideInVertically { it },
            exit = fadeOut(),
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .navigationBarsPadding()
                    .padding(all = 16.dp)
                    .background(
                        color = BibLibColorPalette.LightGreen,
                        shape = MaterialTheme.shapes.small,
                    )
                    .padding(
                        vertical = 16.dp,
                        horizontal = 24.dp,
                    ),
                horizontalArrangement = Arrangement.spacedBy(16.dp)
            ) {
                Icon(
                    painter = painterResource(id = R.drawable.ic_baseline_auto_stories_24),
                    tint = Color.White,
                    contentDescription = null
                )

                Text(
                    style = MaterialTheme.typography.body1,
                    color = Color.White,
                    text = stringResource(id = R.string.detail_send_success)
                )
            }
        }
    }
}

@Composable
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
fun DetailScreenSendLoaderPreview() {
    BibLibTheme {
        DetailScreenSendLoaderContent(
            isLoadingVisible = true,
            isSuccessVisible = true,
        )
    }
}