package com.laseroptek.raman.navigation

import android.content.res.Configuration
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.NavigationRail
import androidx.compose.material3.NavigationRailItem
import androidx.compose.material3.NavigationRailItemDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.laseroptek.raman.ui.common.icon.mainIcon
import com.laseroptek.raman.utils.ext.px
import com.laseroptek.raman.utils.ext.toIntPx
import timber.log.Timber


@Composable
fun NavigationSideBar(
    items: List<NavigationItem>,
    currentRoute: String?,
    innerPadding: PaddingValues,
    onItemClick: (NavigationItem) -> Unit = {},
    modifier: Modifier = Modifier
) {
    // TestOnly {
    Timber.d("top: %d.dp", innerPadding.calculateTopPadding().toIntPx)          // top: 15.dp
    Timber.d("bottom: %d.dp", innerPadding.calculateBottomPadding().toIntPx)    // bottom: 0.dp
    // } TestOnly

    NavigationRail(
        containerColor = Color.Transparent, // #F7F7F7
        contentColor = Color.Transparent,  // #EBEBEB
        modifier = modifier
            .size(102.px.dp, 316.px.dp)
            .padding(
                start = 30.px.dp,
                //bottom = 60.px.dp
            ),
    ) {
        Column(
            modifier = Modifier
                .size(72.px.dp, 256.px.dp)
                .background(Color(21,21,21), RoundedCornerShape(40.px.dp)),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            items.filter { navigationItem ->
                navigationItem.show
            }.forEach { navigationItem ->
                NavigationRailItem(
                    selected = navigationItem.route == currentRoute,
                    onClick = { onItemClick(navigationItem) },
                    icon = {
                        Box(
                            modifier = Modifier
                                .size(width = 60.px.dp, height = 60.px.dp)
                                .background(if (navigationItem.route == currentRoute) Color(80,70,35) else Color.Transparent
                                    , RoundedCornerShape(36.px.dp)),
                            Alignment.Center
                        ) {
                            Image(
                                painter = painterResource(id = mainIcon(navigationItem.route)),
                                contentDescription = null,
                                modifier = Modifier.size(24.px.dp),
                                colorFilter = if (navigationItem.route == currentRoute) ColorFilter.tint(Color(222,173,11)) else ColorFilter.tint(Color.White)
                            )
                        }
                    },
                    modifier = Modifier.size (width = 60.px.dp, height = 60.px.dp)
                        .align(Alignment.CenterHorizontally)
                        .clip(RoundedCornerShape(36.px.dp))
                        .background(if (navigationItem.route == currentRoute) Color(80,70,35) else Color.Transparent)
                    ,
                    colors = NavigationRailItemDefaults.colors(
                        selectedIconColor = Color.White, // Color of the selected icon
                        unselectedIconColor = Color.Gray, // Color of the unselected icon
                        selectedTextColor = Color.White, // Color of the selected label
                        unselectedTextColor = Color.Gray, // Color of the unselected label
                        indicatorColor = Color.Transparent // Background color of the selected item
                    )
                )
            }
        }
    }
}



@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false,
    device = "spec:width=1280dp,height=720dp,dpi=150,isRound=false,chinSize=0dp,orientation=landscape"
)

@Composable
fun PreviewNavigationSideBar() {
    Column(
        modifier = Modifier
            .wrapContentSize()
            .background(Color.White),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        val rootNavController = rememberNavController()
        val navBackStackEntry by rootNavController.currentBackStackEntryAsState()
        val currentRoute by remember(navBackStackEntry) {
            derivedStateOf {
                navBackStackEntry?.destination?.route
            }
        }
        NavigationSideBar(
            items = navigationItemsLists,
            currentRoute = currentRoute,
            innerPadding = PaddingValues(0.dp),
        )
    }
}

