package it.vfsfitvnm.vimusic.ui.components.themed

import android.annotation.SuppressLint
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.VisibilityThreshold
import androidx.compose.animation.core.spring
import androidx.compose.animation.with
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import it.vfsfitvnm.vimusic.LocalPlayerAwarePaddingValues
import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance

@SuppressLint("ModifierParameter")
@ExperimentalAnimationApi
@Composable
fun Scaffold(
    topIconButtonId: Int,
    onTopIconButtonClick: () -> Unit,
    tabIndex: Int,
    onTabChanged: (Int) -> Unit,
    tabColumnContent: @Composable (@Composable (Int, String, Int) -> Unit) -> Unit,
    primaryIconButtonId: Int? = null,
    onPrimaryIconButtonClick: () -> Unit = {},
    modifier: Modifier = Modifier,
    content: @Composable AnimatedVisibilityScope.(Int) -> Unit
) {
    val (colorPalette) = LocalAppearance.current

    Box(
        modifier = modifier
            .background(colorPalette.background0)
            .fillMaxSize()
    ) {
        Row(
            modifier = modifier
                .fillMaxSize()
        ) {
            VerticalBar(
                topIconButtonId = topIconButtonId,
                onTopIconButtonClick = onTopIconButtonClick,
                tabIndex = tabIndex,
                onTabChanged = onTabChanged,
                tabColumnContent = tabColumnContent,
//                primaryIconButtonId = primaryIconButtonId,
//                onPrimaryIconButtonClick = onPrimaryIconButtonClick,
                modifier = Modifier
                    .padding(LocalPlayerAwarePaddingValues.current)
            )

            AnimatedContent(
                targetState = tabIndex,
                transitionSpec = {
                    val slideDirection = when (targetState > initialState) {
                        true -> AnimatedContentScope.SlideDirection.Up
                        false -> AnimatedContentScope.SlideDirection.Down
                    }

                    val animationSpec = spring(
                        dampingRatio = 0.9f,
                        stiffness = Spring.StiffnessLow,
                        visibilityThreshold = IntOffset.VisibilityThreshold
                    )

                    slideIntoContainer(slideDirection, animationSpec) with
                            slideOutOfContainer(slideDirection, animationSpec)
                },
                content = content,
            )
        }

        primaryIconButtonId?.let {
            Box(
                modifier = Modifier
                    .align(Alignment.BottomEnd)
                    .padding(all = 16.dp)
                    .padding(LocalPlayerAwarePaddingValues.current)
                    .clip(RoundedCornerShape(16.dp))
                    .clickable(onClick = onPrimaryIconButtonClick)
                    .background(colorPalette.background2)
                    .size(62.dp)
            ) {
                Image(
                    painter = painterResource(primaryIconButtonId),
                    contentDescription = null,
                    colorFilter = ColorFilter.tint(colorPalette.text),
                    modifier = Modifier
                        .align(Alignment.Center)
                        .size(20.dp)
                )
            }
        }
    }
}

@SuppressLint("ModifierParameter")
@ExperimentalAnimationApi
@Composable
fun SimpleScaffold(
    topIconButtonId: Int,
    onTopIconButtonClick: () -> Unit,
    title: String = "",
    primaryIconButtonId: Int? = null,
    primaryIconButtonEnabled: Boolean = true,
    onPrimaryIconButtonClick: () -> Unit = {},
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit
) {
    val (colorPalette) = LocalAppearance.current

    Row(
        modifier = modifier
            .background(colorPalette.background0)
            .fillMaxSize()
    ) {
        VerticalTitleBar(
            topIconButtonId = topIconButtonId,
            onTopIconButtonClick = onTopIconButtonClick,
            title = title,
            primaryIconButtonId = primaryIconButtonId,
            primaryIconButtonEnabled = primaryIconButtonEnabled,
            onPrimaryIconButtonClick = onPrimaryIconButtonClick,
            modifier = Modifier
                .padding(LocalPlayerAwarePaddingValues.current)
        )

        content()
    }
}
