package com.example.dtf.screens

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
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.EditPostViewModel
import com.example.dtf.widgets.MyTextField

@Composable
fun EditPostScreen(navController: NavHostController, postId: Int) {
    val sharedPref = PreferencesManager(LocalContext.current)

    val title = remember { mutableStateOf(TextFieldValue("")) }
    val content = remember { mutableStateOf(TextFieldValue("")) }

    val viewModel = hiltViewModel<EditPostViewModel>()

    val post = viewModel.post.observeAsState().value

    val success = viewModel.editingPostState.observeAsState().value

    viewModel.retrievePost(postId)

    if (post != null) {
        title.value = TextFieldValue(post.title)
        content.value = TextFieldValue(post.content)
    }

    if (success == true) {
        navController.navigate(ScreenPaths.Post.route.replace("{post}", postId.toString())) {
            popUpTo(ScreenPaths.Post.route.replace("{post}", postId.toString())) {
                inclusive = true
            }
        }
    }

    if (success == false) {
        AlertDialog(
            title = {
                Text(
                    text = "Ошибка",
                    fontWeight = FontWeight.Bold,
                    fontSize = 20.sp
                )
            },
            text = {
                Text(
                    text = "Нельзя сохранить новость с пустым заголовком или содержанием",
                    fontWeight = FontWeight.Normal,
                    fontSize = 16.sp
                )
            },
            onDismissRequest = {
                viewModel.calmEditingState()
            },
            buttons = {
                TextButton(
                    onClick = {
                        viewModel.calmEditingState()
                    }
                ) {
                    Text("ОК")
                }
            }
        )
    }

    Column(
        modifier = Modifier.verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.spacedBy(10.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        MyTextField(
            value = title,
            "Заголовок",
            onChanged = {title.value = it},
            modifier = Modifier.fillMaxWidth().padding(15.dp, 10.dp, 15.dp, 0.dp),
            backgroundColor = Color(0xFFFEFEFE)
        )
        MyTextField(
            value = content,
            "Содержимое",
            onChanged = {content.value = it},
            modifier = Modifier.fillMaxWidth().heightIn(min = 500.dp).padding(horizontal = 15.dp),
            backgroundColor = Color(0xFFFEFEFE),
            false
        )
        Button(
            {
                viewModel.editPost(sharedPref, postId, title.value.text, content.value.text)
            },
            colors = ButtonDefaults.buttonColors(Color(0xFF388DF2)),
            border = BorderStroke(1.dp, Color(0xFF0085FF)),
            shape = RoundedCornerShape(15.dp),
            modifier = Modifier.fillMaxWidth(0.5f)
        ) {
            Text(
                text = "Сохранить",
                style = TextStyle(
                    fontSize = 24.sp,
                    fontWeight = FontWeight(400),
                    color = Color(0xFFFFFFFF),
                )
            )
        }
        Spacer(modifier = Modifier.height(70.dp))
    }
}