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

import android.content.res.Configuration
import androidx.compose.animation.*
import androidx.compose.foundation.layout.size
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
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 com.pixelized.biblib.R
import com.pixelized.biblib.model.search.FilterType
import com.pixelized.biblib.ui.theme.BibLibTheme
import com.pixelized.biblib.utils.extention.bibLib
import java.util.*

@Stable
@Immutable
data class FilterChipUio(
    val id: FilterType,
    val title: String,
    val label: String?,
    val selected: Boolean,
    val closable: Boolean,
    val openable: Boolean,
)

@OptIn(ExperimentalMaterialApi::class, ExperimentalAnimationApi::class)
@Composable
fun FilterChip(
    modifier: Modifier = Modifier,
    filter: State<FilterChipUio>,
    onClick: () -> Unit,
) {
    val context = LocalContext.current
    val color by animateColorAsState(
        targetValue = when (filter.value.selected) {
            true -> MaterialTheme.bibLib.colors.base.primary
            else -> MaterialTheme.bibLib.colors.typography.medium
        }
    )
    val label by remember(filter) {
        derivedStateOf {
            if (filter.value.label != null) {
                context.getString(
                    R.string.search_filter_param,
                    filter.value.title,
                    filter.value.label
                )
            } else {
                filter.value.title
            }
        }
    }

    FilterChip(
        modifier = modifier,
        selected = filter.value.selected,
        onClick = onClick,
        trailingIcon = {
            when {
                filter.value.openable -> Icons.Default.KeyboardArrowDown
                filter.value.closable -> Icons.Default.Close
                else -> null
            }?.let {
                Icon(
                    modifier = Modifier.size(12.dp),
                    tint = color,
                    imageVector = it,
                    contentDescription = null
                )
            }
        },
    ) {
        AnimatedContent(
            targetState = label,
            transitionSpec = { fadeIn() with fadeOut() },
        ) {
            Text(
                color = color,
                style = MaterialTheme.bibLib.typography.base.caption,
                text = label,
            )
        }
    }
}

@Composable
@Preview(uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
private fun FilterPreview(
    @PreviewParameter(FilterPreviewProvider::class) preview: State<FilterChipUio>
) {
    BibLibTheme {
        FilterChip(
            filter = preview,
            onClick = {},
        )
    }
}

class FilterPreviewProvider : PreviewParameterProvider<State<FilterChipUio>> {
    override val values: Sequence<State<FilterChipUio>> = filterPreview().asSequence()
}

fun filterPreview() = listOf(
    FilterChipUio(
        id = FilterType.New,
        title = "New",
        label = null,
        selected = false,
        closable = false,
        openable = false
    ),
    FilterChipUio(
        id = FilterType.Author,
        title = "Authors",
        label = "Asimov",
        selected = true,
        closable = true,
        openable = false
    ),
    FilterChipUio(
        id = FilterType.Series,
        title = "Series",
        label = null,
        selected = false,
        closable = false,
        openable = true
    ),
    FilterChipUio(
        id = FilterType.Genre,
        title = "Genres",
        label = null,
        selected = false,
        closable = false,
        openable = true
    ),
    FilterChipUio(
        id = FilterType.Language,
        title = "Languages",
        label = null,
        selected = false,
        closable = false,
        openable = true
    ),
).map { mutableStateOf(it) }