package com.example.libary

import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.libary.theme.AccentColor
import com.example.libary.theme.IconsColor
import com.example.libary.theme.TabBarShad

data class TabBarData(
    val icon: Int,
    val name: String
)

val defaultTabBarData = listOf(
    TabBarData(
        icon = R.drawable.bot_1,
        name = "Главная"
    ),
    TabBarData(
        icon = R.drawable.bot_2,
        name = "Каталог"
    ),
    TabBarData(
        icon = R.drawable.bot_3,
        name = "Проекты"
    ),
    TabBarData(
        icon = R.drawable.bot_4,
        name = "Профиль"
    )
)

@Composable
fun TabBar(
    modifier: Modifier = Modifier,
    value: MutableState<TabBarData>,
    data: List<TabBarData> = defaultTabBarData
) {
    Column(
        modifier = modifier
            .shadow(10.dp, spotColor = TabBarShad, ambientColor = TabBarShad)
    ) {
        HorizontalDivider(thickness = 1.dp, color = TabBarShad)

        NavigationBar(
            containerColor = Color.White
        ) {
            data.forEach {
                NavigationBarItem(
                    selected = value.value == it,
                    modifier = Modifier.testTag(it.name),
                    colors = NavigationBarItemDefaults.colors(
                        indicatorColor = Color.Transparent
                    ),
                    icon = {
                        Icon(
                            painter = painterResource(it.icon),
                            contentDescription = null,
                            tint = if (value.value == it) AccentColor else IconsColor
                        )
                    },
                    label = {
                        Text(
                            text = it.name,
                            color = if (value.value == it) AccentColor else IconsColor,
                            fontWeight = FontWeight.W400,
                            fontSize = 12.sp
                        )
                    },
                    onClick = {
                        value.value = it
                    }
                )
            }
        }
    }
}

@Preview
@Composable
private fun TabBarPrev() {
    val value = remember { mutableStateOf(defaultTabBarData[0]) }
    Column {
        Spacer(Modifier.height(100.dp))
        TabBar(
            value = value
        )
    }
}