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

import android.content.res.Configuration
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.pixelized.biblib.ui.theme.BibLibTheme
import com.pixelized.biblib.utils.extention.bibLib

@Stable
@Immutable
data class OptionsUio(
    val sorting: Boolean = false,
    val micro: Boolean,
    val small: Boolean,
    val large: Boolean,
)

@Composable
fun Options(
    modifier: Modifier = Modifier,
    options: OptionsUio,
    onUp: () -> Unit,
    onSort: () -> Unit,
    onMicro: () -> Unit,
    onSmall: () -> Unit,
    onLarge: () -> Unit,
) {
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween,
    ) {
        Row(
            horizontalArrangement = Arrangement.spacedBy(4.dp),
        ) {
            IconButton(
                onClick = onUp
            ) {
                Icon(
                    tint = MaterialTheme.bibLib.colors.typography.medium,
                    imageVector = Icons.Default.ArrowUpward,
                    contentDescription = null,
                )
            }
            IconButton(
                onClick = onSort
            ) {
                val color by animateColorAsState(
                    targetValue = when (options.sorting) {
                        true -> MaterialTheme.bibLib.colors.base.primary
                        else -> MaterialTheme.bibLib.colors.typography.medium
                    }
                )
                Icon(
                    tint = color,
                    imageVector = Icons.Default.SortByAlpha,
                    contentDescription = null,
                )
            }
        }
        Row(
            horizontalArrangement = Arrangement.spacedBy(4.dp),
        ) {
            IconButton(
                onClick = onMicro
            ) {
                val color by animateColorAsState(
                    targetValue = when (options.micro) {
                        true -> MaterialTheme.bibLib.colors.base.primary
                        else -> MaterialTheme.bibLib.colors.typography.medium
                    }
                )
                Icon(
                    tint = color,
                    imageVector = Icons.Default.List,
                    contentDescription = null,
                )
            }
            IconButton(
                onClick = onSmall
            ) {
                val color by animateColorAsState(
                    targetValue = when (options.small) {
                        true -> MaterialTheme.bibLib.colors.base.primary
                        else -> MaterialTheme.bibLib.colors.typography.medium
                    }
                )
                Icon(
                    tint = color,
                    imageVector = Icons.Default.FormatListBulleted,
                    contentDescription = null,
                )
            }
            IconButton(
                onClick = onLarge
            ) {
                val color by animateColorAsState(
                    targetValue = when (options.large) {
                        true -> MaterialTheme.bibLib.colors.base.primary
                        else -> MaterialTheme.bibLib.colors.typography.medium
                    }
                )
                Icon(
                    tint = color,
                    imageVector = Icons.Default.GridOn,
                    contentDescription = null,
                )
            }
        }
    }
}

@Composable
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
private fun OptionsPreview() {
    BibLibTheme {
        Options(
            modifier = Modifier.fillMaxWidth(),
            options = OptionsUio(
                sorting = false,
                micro = false,
                small = false,
                large = true,
            ),
            onUp = { },
            onSort = { },
            onMicro = { },
            onSmall = { },
            onLarge = { },
        )
    }
}