package com.example.pencilmuse.views.MainActivity

import androidx.compose.material3.Icon
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
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.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.BottomAppBarDefaults
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.FloatingActionButtonDefaults
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.currentCompositionLocalContext
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.pencilmuse.R
import com.example.pencilmuse.domain.utils.Routes
import com.example.pencilmuse.models.BottomNavItem
import com.example.pencilmuse.views.MainActivity.components.Catalogs
import com.example.pencilmuse.views.MainActivity.components.MainPage
import com.example.pencilmuse.views.MainActivity.components.Navigation
import com.example.pencilmuse.views.MainActivity.components.Notes
import com.example.pencilmuse.views.MainActivity.components.auth
import com.example.pencilmuse.views.theme.PencilmuseTheme



class MainActivity : ComponentActivity() {
    @SuppressLint("ResourceType")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            PencilmuseTheme {

                val navController = rememberNavController()
                BottomAppMenu(navController,this)
            }

        }
    }
}


@Composable
fun BottomAppMenu(navController: NavHostController, context: Context){
    var isAuthenticated by remember { mutableStateOf(false) }
    val initialRoute = if (isAuthenticated) "MainPage" else "auth"
    var currentRoute by remember { mutableStateOf("auth") }

    LaunchedEffect(Unit) {
        navController.addOnDestinationChangedListener { _, destination, _ ->
            currentRoute = destination.route ?: ""
        }
    }



    Scaffold(
            bottomBar = {
                if(currentRoute != "auth")
                {
                    BottomAppBar(modifier = Modifier.heightIn(40.dp),containerColor = Color.White,
                    actions = {
                        Column(Modifier.background(Color.White)) {
                            Row(
                                horizontalArrangement = Arrangement.SpaceAround,
                                verticalAlignment = Alignment.Top,
                                modifier = Modifier.fillMaxWidth()
                            ) {
                                Box(
                                    contentAlignment = Alignment.Center,
                                    modifier = Modifier.width(64.dp).height(48.dp)
                                ) {
                                    IconButton(onClick = { navController.navigate("MainPage") }) {
                                        Icon(
                                            modifier = Modifier.size(30.dp, 30.dp),
                                            painter = painterResource(id = R.drawable.home),
                                            contentDescription = "Главная"
                                        )
                                    }
                                    Text(
                                        text = "Главная",
                                        style = MaterialTheme.typography.bodySmall,
                                        color = Color.Black,
                                        modifier = Modifier.offset(y = 20.dp)
                                    )
                                }
                                Box(
                                    contentAlignment = Alignment.Center,
                                    modifier = Modifier.width(64.dp).height(48.dp)
                                ) {
                                    IconButton(onClick = { navController.navigate("Catalogs") }) {
                                        Icon(
                                            modifier = Modifier.size(30.dp, 30.dp),
                                            painter = painterResource(id = R.drawable.catalog),
                                            contentDescription = "Каталоги"
                                        )
                                    }
                                    Text(
                                        text = "Каталоги",
                                        style = MaterialTheme.typography.bodySmall,
                                        color = Color.Black,
                                        modifier = Modifier.offset(y = 20.dp)
                                    )
                                }
                                Box(
                                    contentAlignment = Alignment.Center,
                                    modifier = Modifier.width(64.dp).height(48.dp)
                                ) {
                                    IconButton(onClick = { navController.navigate("Notes") }) {
                                        Icon(
                                            modifier = Modifier.size(30.dp, 30.dp),
                                            painter = painterResource(id = R.drawable.notes),
                                            contentDescription = "Заметки"
                                        )
                                    }
                                    Text(
                                        text = "Заметки",
                                        style = MaterialTheme.typography.bodySmall,
                                        color = Color.Black,
                                        modifier = Modifier.offset(y = 20.dp)
                                    )
                                }
                            }
                        }
                    }
                )
            }
            }
        ) { innerPadding ->
            Column(
                modifier = Modifier
                    .padding(innerPadding),
                verticalArrangement = Arrangement.spacedBy(16.dp),
            ) {
                NavHost(
                    navController = navController,
                    startDestination = initialRoute
                )
                {
                    composable("auth") {
                        auth(navController)
                    }
                    composable("MainPage") {
                        MainPage(navController)
                    }


                    composable("Catalogs") {
                        Catalogs(navController)
                    }
                    composable("Notes") {
                        Notes(navController, context)
                    }
                }
            }
        }
    }

