package com.example.what_the_fantasy.ui.components

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.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.IconButtonColors
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
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.painter.Painter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.rememberAsyncImagePainter
import coil.compose.rememberImagePainter
import com.example.what_the_fantasy.R
import com.example.what_the_fantasy.ui.states.CurrentUserState
import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel

@Composable
fun NavBar(onProfile : Boolean = false,
           onFavorite : Boolean = false,
           onAccueil : Boolean = false,
           onQuiz : Boolean = false,
           currentUserVM : CurrentUserViewModel,
           currentUserState : CurrentUserState,
           navControllerProfil: () -> Unit = {},
           navControllerFavorite:() -> Unit= {},
           navControllerAccueil: () -> Unit= {},
           navControllerQuiz: () -> Unit= {},
           navControllerSearch: () -> Unit = {},
           content : @Composable ()-> Unit ) {

    Column(
        modifier = Modifier
            .fillMaxSize()
    ) {
        NavigationBar(
            modifier = Modifier
                .fillMaxWidth(),
            containerColor = MaterialTheme.colorScheme.onPrimary
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth(),
                Arrangement.SpaceBetween,
                verticalAlignment = Alignment.Bottom
            ) {
                ButtonIconVectorInt(currentUserState.imagePath,"Profile",navControllerProfil,onProfile)

                ButtonIconVector(Icons.Rounded.Search,stringResource(R.string.NavSearch),navControllerSearch,false)
            }
        }

        Box(modifier = Modifier
            .background(Color.Black)
            .fillMaxHeight(0.90f)){
            content()
        }

        BottomAppBar (modifier = Modifier
            .fillMaxSize(),
            containerColor = MaterialTheme.colorScheme.onPrimary
        ) {
            Row(modifier = Modifier
                .fillMaxSize(),
                horizontalArrangement = Arrangement.SpaceAround,
                verticalAlignment = Alignment.CenterVertically
            ) {

                ButtonIconPainter(painterResource(
                    if(onFavorite)R.drawable.favorite_button_full
                    else R.drawable.favorite_button_empty
                ), stringResource(R.string.NavFavorite) ,navControllerFavorite,onFavorite)

                ButtonIconPainter(painterResource(
                    if(onAccueil)R.drawable.home_button_full
                    else R.drawable.home_button_empty
                ),stringResource(R.string.NavHome),navControllerAccueil,onAccueil)

                ButtonIconPainter(painterResource(
                    if(onQuiz)R.drawable.quiz_button_full
                    else R.drawable.quiz_button_empty
                ),stringResource(R.string.NavQuiz),navControllerQuiz,onQuiz)

            }
        }
    }
}

@Composable
fun ButtonIconVectorInt(img : String, name : String, nav : ()->Unit ,onPage : Boolean){
    IconButton(onClick = {nav()},
        enabled = !onPage,
        colors = IconButtonColors(Color.Transparent, MaterialTheme.colorScheme.onBackground,//couleur quand il n'est pas selectionné
                                Color.Transparent, MaterialTheme.colorScheme.primary),//couleur quand il est selectionné
        modifier = Modifier
            .size(60.dp)
    ) {

        Image(
            painter = rememberAsyncImagePainter(img),
            contentDescription = name,
            modifier = Modifier
                .fillMaxSize()
                .clip(CircleShape) // Pour rendre l'image circulaire
        )
    }
}
@Composable
fun ButtonIconVector(img : ImageVector, name : String, nav : ()->Unit ,onPage : Boolean){
    IconButton(onClick = {nav()},
        enabled = !onPage,
        colors = IconButtonColors(Color.Transparent, MaterialTheme.colorScheme.onBackground,//couleur quand il n'est pas selectionné
            Color.Transparent, MaterialTheme.colorScheme.primary),//couleur quand il est selectionné
        modifier = Modifier
            .size(60.dp)
    ) {

        Image(img,
            contentDescription = name,
            modifier = Modifier
                .fillMaxSize()
                .clip(CircleShape) // Pour rendre l'image circulaire
        )
    }
}

@Composable
fun ButtonIconPainter(img : Painter, name : String, nav : ()->Unit,onPage : Boolean){
    IconButton(onClick = {nav()},
        enabled = !onPage,
        colors = IconButtonColors(Color.Transparent,MaterialTheme.colorScheme.onBackground,//couleur quand il n'est pas selectionné
                                Color.Transparent, MaterialTheme.colorScheme.primary),//couleur quand il est selectionné
        modifier = Modifier
            .size(60.dp)
    ) {
        Icon(img,
            contentDescription = name,
            modifier = Modifier
                .size(50.dp)
        )
    }
}