package com.example.dtf

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navigation
import com.example.dtf.screens.EditPostScreen
import com.example.dtf.screens.LoginScreen
import com.example.dtf.screens.NewPostScreen
import com.example.dtf.screens.PostScreen
import com.example.dtf.screens.PostsScreen
import com.example.dtf.screens.ProfileScreen
import com.example.dtf.screens.RegisterScreen
import com.example.dtf.utils.ScreenPaths
import com.example.dtf.widgets.BottomNavBar
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter",
        "UnusedMaterialScaffoldPaddingParameter"
    )
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navController = rememberNavController()
            val navBackStackEntry by navController.currentBackStackEntryAsState()
            val currentRoute = navBackStackEntry?.destination?.route

            val includeBackButton = remember { mutableStateOf(false) }

            Scaffold(
                modifier = Modifier.fillMaxSize(),
                backgroundColor = Color.Transparent,
                topBar = {
                    if (currentRoute != ScreenPaths.Login.route && currentRoute != ScreenPaths.Register.route && includeBackButton.value) {
                        TopAppBar(
                            modifier = Modifier.height(60.dp),
                            title = {

                            },
                            navigationIcon = {
                                IconButton(
                                    onClick = {
                                        includeBackButton.value = false
                                        navController.popBackStack()
                                    }
                                ) {
                                    Icon(
                                        imageVector = Icons.Default.ArrowBack,
                                        contentDescription = "Назад",
                                    )
                                }

                            },
                        )
                    }
                },
                bottomBar = {
                    if (currentRoute != ScreenPaths.Login.route && currentRoute != ScreenPaths.Register.route) {
                        BottomNavBar(navController)
                    }
                }
            ) {
                NavHost(navController, ScreenPaths.Login.route) {
                    composable(ScreenPaths.Login.route) {
                        LoginScreen(navController)
                    }
                    composable(ScreenPaths.Register.route) {
                        RegisterScreen(navController)
                    }
                    navigation(ScreenPaths.Profile.route + "/show", ScreenPaths.Profile.route) {
                        composable(ScreenPaths.Profile.route + "/show") {
                            includeBackButton.value = false
                            ProfileScreen(navController)
                        }
                    }
                    navigation(ScreenPaths.Posts.route + "/all", ScreenPaths.Posts.route) {
                        composable(ScreenPaths.Posts.route + "/all") {
                            includeBackButton.value = false
                            PostsScreen(navController)
                        }
                        composable(ScreenPaths.NewPost.route) {
                            includeBackButton.value = true
                            NewPostScreen(navController)
                        }
                        composable(ScreenPaths.Post.route) { navBackStackEntry ->
                            includeBackButton.value = true
                            navBackStackEntry.arguments?.getString("post")?.let { postId ->
                                PostScreen(
                                    postId.toInt(),
                                    navController
                                )
                            }
                        }
                        composable(ScreenPaths.EditPost.route) { navBackStackEntry ->
                            includeBackButton.value = true
                            navBackStackEntry.arguments?.getString("post")?.let { postId ->
                                EditPostScreen(
                                    navController,
                                    postId.toInt()
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}

