package com.example.dtf.screens

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.*
import androidx.compose.ui.graphics.*
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.*
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.example.dtf.PreferencesManager
import com.example.dtf.utils.ScreenPaths
import com.example.dtf.viewmodels.ProfileViewModel

@Composable
fun ProfileScreen(navController: NavHostController) {
    val sharedPref = PreferencesManager(LocalContext.current)

    val viewModel = hiltViewModel<ProfileViewModel>()
    val user = viewModel.user.observeAsState().value

    viewModel.retrieveUser(sharedPref)

    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier
                       .fillMaxHeight(0.3f)
                       .fillMaxWidth()
                       .background(Color(0xFF388DF2))
        ) {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Card(
                    modifier = Modifier.size(90.dp),
                    shape = RoundedCornerShape(45.dp),
                ) {
                    Box(
                        modifier = Modifier.fillMaxSize(),
                        contentAlignment = Alignment.Center
                    ) {
                        Text(
                            text = if (user != null) { user.username[0].toString() } else {""},
                            fontSize = 30.sp,
                        )
                    }
                }
                Spacer(modifier = Modifier.fillMaxHeight(0.1f))
                Text(
                    text = user?.username ?: "",
                    fontSize = 30.sp,
                    color = Color.White
                )
            }
        }
        Divider()
        Row(
            modifier = Modifier
                       .fillMaxHeight(0.9f)
                       .fillMaxWidth()
                       .background(Color.White),
        ) {
            Column(
                modifier = Modifier.fillMaxSize(),
                verticalArrangement = Arrangement.Bottom,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Box(
                    modifier = Modifier.fillMaxHeight(0.2f),
                    contentAlignment = Alignment.Center,
                ) {

                    Text(
                        modifier = Modifier.clickable {
                            viewModel.logout(sharedPref)
                            navController.navigate(ScreenPaths.Login.route)
                        },
                        text = "Выйти",
                        fontSize = 20.sp,
                        color = Color.Red
                    )
                }
            }
        }
    }
}