// Copyright (c) 2026 Gleb Zaharov. License: GPLv3 (see LICENSE).

package ru.sweetbread.unn.ui.layout

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountBox
import androidx.compose.material.icons.filled.DateRange
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.outlined.Info
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat
import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import coil.compose.AsyncImage
import kotlinx.coroutines.launch
import ru.sweetbread.unn.R
import ru.sweetbread.unn.api.ME
import ru.sweetbread.unn.api.portalURL
import ru.sweetbread.unn.ui.composes.About
import ru.sweetbread.unn.ui.composes.Blogposts
import ru.sweetbread.unn.ui.composes.Schedule
import ru.sweetbread.unn.ui.theme.UNNTheme
import splitties.resources.appStr
import splitties.toast.toast

class MainActivity : ComponentActivity() {

    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        WindowCompat.setDecorFitsSystemWindows(window, false)

        setContent {
            UNNTheme {
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    val navController = rememberNavController()
                    val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
                    val scope = rememberCoroutineScope()

                    ModalNavigationDrawer(
                        drawerState = drawerState,
                        drawerContent = {
                            DrawerContent(
                                Modifier
                                    .fillMaxHeight()
                                    .fillMaxWidth(.75f)
                                    .background(MaterialTheme.colorScheme.surfaceContainer)
                                    .systemBarsPadding(),
                                navController = navController
                            )
                        }
                    ) {
                        val navBackStackEntry by navController.currentBackStackEntryAsState()
                        val currentRoute = navBackStackEntry?.destination?.route

                        Scaffold(
                            topBar = {
                                CenterAlignedTopAppBar(
                                    title = {
                                        Text(
                                            when {
                                                currentRoute?.startsWith("portal/") == true ->
                                                    appStr(R.string.news)

                                                currentRoute?.startsWith("journal/") == true ->
                                                    appStr(R.string.schedule)

                                                else -> appStr(R.string.app_name)
                                            },
                                            Modifier.padding()
                                        )
                                    },
                                    navigationIcon = {
                                        IconButton(
                                            onClick = {
                                                scope.launch { drawerState.open() }
                                            }
                                        ) {
                                            Icon(Icons.Filled.Menu, "Меню")
                                        }
                                    }
                                )
                            },
                            bottomBar = {
                                NavigationBar {
                                    NavigationBarItem(
                                        onClick = {
                                            navController.navigate("portal/blogposts") {
                                                launchSingleTop = true
                                            }
                                        },
                                        icon = {
                                            Icon(
                                                Icons.Filled.Home,
                                                contentDescription = "Home"
                                            )
                                        },
                                        selected = currentRoute?.startsWith("portal/") == true
                                    )

                                    NavigationBarItem(
                                        onClick = {
                                            navController.navigate("journal/schedule") {
                                                launchSingleTop = true
                                            }
                                        },
                                        icon = {
                                            Icon(
                                                Icons.Filled.DateRange,
                                                contentDescription = "Schedule"
                                            )
                                        },
                                        selected = currentRoute?.startsWith("journal/") == true
                                    )

                                    NavigationBarItem(
                                        onClick = { toast("Not implemented") },
                                        icon = {
                                            Icon(
                                                Icons.Filled.AccountBox,
                                                contentDescription = "Account"
                                            )
                                        },
                                        selected = false
                                    )
                                }
                            }
                        ) { innerPadding ->
                            Box(Modifier.padding(innerPadding)) {
                                NavHost(navController, startDestination = "journal/schedule") {
                                    composable("portal/blogposts") {
                                        Blogposts()
                                    }
                                    composable("journal/schedule") {
                                        Schedule()
                                    }
                                    composable("about") {
                                        About()
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    @Composable
    fun DrawerContent(modifier: Modifier = Modifier, navController: NavController) {
        Column(modifier = modifier.padding(0.dp)) {
            Row(
                Modifier
                    .padding(8.dp)
                    .fillMaxWidth()
                    .background(MaterialTheme.colorScheme.primaryContainer, RoundedCornerShape(16.dp))
                    .padding(8.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                AsyncImage(
                    modifier = Modifier.size(64.dp).clip(CircleShape),
                    model = portalURL + ME.avatar.thumbnail,
                    contentDescription = ME.nameEn
                )

                Column (Modifier.padding(horizontal = 8.dp)) {
                    Text(
                        ME.nameRu.split(" ").dropLast(1).joinToString(" "),
                        fontWeight = FontWeight.Bold
                    )
                    Text(ME.type.s)
                }
            }

            Spacer(modifier = Modifier.height(16.dp))
            HorizontalDivider()

            TextButton(
                {},
                Modifier.fillMaxWidth(),
                shape = RectangleShape,
                enabled = false
            ) {
                Text(
                    appStr(R.string.record_book),
                    Modifier.fillMaxWidth(),
                    textAlign = TextAlign.Start
                )
            }

            TextButton(
                {},
                Modifier.fillMaxWidth(),
                shape = RectangleShape,
                enabled = false
            ) {
                Text(
                    appStr(R.string.documents),
                    Modifier.fillMaxWidth(),
                    textAlign = TextAlign.Start
                )
            }

            TextButton(
                {},
                Modifier.fillMaxWidth(),
                shape = RectangleShape,
                enabled = false
            ) {
                Text(
                    appStr(R.string.materials),
                    Modifier.fillMaxWidth(),
                    textAlign = TextAlign.Start
                )
            }

            Spacer(Modifier.weight(1f))

            HorizontalDivider()
            TextButton(
                {navController.navigate("about")},
                Modifier.fillMaxWidth(),
                shape = RectangleShape
            ) {
                Icon(
                    imageVector = Icons.Outlined.Info,
                    contentDescription = "Info",
                    modifier = Modifier.padding(horizontal = 4.dp)
                )
                Text(appStr(R.string.about_app))
            }
        }
    }
}