package com.example.wabi.view.screeens.profile.items

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.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.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import androidx.navigation.NavHostController
import com.example.wabi.ui.theme.WabiTheme
import com.example.wabi.view.components.buttons.MainButton
import com.example.wabi.view.components.colors.darkenColor
import com.example.wabi.view.components.images.CoilImageInput
import com.example.wabi.view.components.images.StandartProfileImage
import com.example.wabi.view.components.texts.HeaderText
import com.example.wabi.view.components.texts.MainText
import com.example.wabi.view.screeens.profile.ProfileViewModel

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun ProfileCurrentUser(
    navHostController: NavHostController,
    vm: ProfileViewModel,
    stateChanger: MutableState<Int>,
    modifier: Modifier = Modifier
) {
    Column(
        modifier = modifier
            .fillMaxSize()
            .background(color = darkenColor(WabiTheme.colors.backgroundColor, 0.2f)),
        verticalArrangement = Arrangement.SpaceBetween,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Column(
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Box(
                modifier = Modifier.fillMaxHeight(0.35f), contentAlignment = Alignment.Center
            ) {
                if (vm.currentUser.image.isNullOrEmpty()) {
                    StandartProfileImage(
                        Modifier
                            .clip(shape = RoundedCornerShape(30.dp))
                            .size(200.dp)
                    )
                } else {
                    CoilImageInput(
                        (vm.currentUser.image!!).toUri(),
                        Modifier
                            .clip(shape = RoundedCornerShape(30.dp))
                            .size(200.dp),
                        standartImage = StandartProfileImage(
                            Modifier
                                .clip(shape = RoundedCornerShape(30.dp))
                                .size(200.dp)
                        )
                    )

                }
            }
            Column(
                modifier = Modifier.fillMaxHeight(0.2f),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                HeaderText(
                    text = vm.currentUser.nickname,
                    maxLines = 1,
                    minLines = 1,
                    textAlign = TextAlign.Center
                )
                MainText(
                    text = vm.currentUser.email, maxLines = 1, minLines = 1
                )
            }
        }

        Column(
            modifier = Modifier
                .fillMaxHeight(0.8f)
                .fillMaxWidth()
                .border(
                    width = 3.dp,
                    color = WabiTheme.colors.mainColor,
                    shape = RoundedCornerShape(topStart = 15.dp, topEnd = 15.dp)
                )
                .background(
                    color = WabiTheme.colors.backgroundColor,
                    shape = RoundedCornerShape(topStart = 15.dp, topEnd = 15.dp)
                )
                .clip(shape = RoundedCornerShape(topStart = 15.dp, topEnd = 15.dp)),
            verticalArrangement = Arrangement.SpaceAround,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Column(
                modifier = Modifier.fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                MainButton(
                    onClick = { },
                    textContent = "Изменить тему",
                    modifier = Modifier.fillMaxWidth(0.82f)
                )
                Spacer(Modifier.height(15.dp))
                MainButton(
                    onClick = { stateChanger.value = ProfileStateDestinations.update },
                    textContent = "Изменить профиль",
                    modifier = Modifier.fillMaxWidth(0.82f)
                )
            }

            Row(
                modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center
            ) {
                MainButton(
                    onClick = { vm.signOutCurrentUser(navHostController) },
                    textContent = "Выход",
                    color = WabiTheme.colors.error,
                    colorContent = WabiTheme.colors.onBoardLight,
                    modifier = Modifier.fillMaxWidth(0.82f)
                )
            }
        }
    }
}