package com.example.wabi.view.screeens.logIn.items.signUpItems

import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
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.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import com.example.wabi.ui.theme.WabiTheme
import com.example.wabi.view.components.buttons.MainButton
import com.example.wabi.view.components.images.CoilImageInput
import com.example.wabi.view.components.images.StandartProfileImage
import com.example.wabi.view.components.textFields.MainTextField
import com.example.wabi.view.components.texts.HeaderText
import com.example.wabi.view.components.texts.MainText
import com.example.wabi.view.screeens.logIn.LogInViewModel

@Composable
fun CreatePofile(
    station: SingUpDestinations,
    modifier: Modifier = Modifier,
    vm: LogInViewModel,
) {
    val user = vm.signUpUser

    var image: MutableState<Uri> = remember { mutableStateOf(Uri.EMPTY) }
    val launcher =
        rememberLauncherForActivityResult(contract = ActivityResultContracts.PickVisualMedia()) { uri ->
            if (uri != null) {
                image.value = uri
                if (image.value != Uri.EMPTY) {
                    var byteArray = vm.converterBitMapToByteArray(uri = image.value)
                    vm.updateUserDataSignUp(user.copy(imageOfBytes = byteArray))
                }
            }
        }

    HeaderText(text = station.header)

    MainText(
        text = station.description
    )

    Box(
        contentAlignment = Alignment.Center, modifier = modifier.border(
            width = 3.dp, color = WabiTheme.colors.mainColor, shape = RoundedCornerShape(30.dp)
        )
    ) {
        if (image.value == Uri.EMPTY) {
            StandartProfileImage(
                Modifier
                    .clip(shape = RoundedCornerShape(30.dp))
                    .size(200.dp)
            )
        } else {
            CoilImageInput(
                image.value,
                Modifier
                    .clip(shape = RoundedCornerShape(30.dp))
                    .size(200.dp),
                standartImage = StandartProfileImage()
            )
        }
    }

    MainButton(
        onClick = {
            launcher.launch(PickVisualMediaRequest(mediaType = ActivityResultContracts.PickVisualMedia.ImageOnly))
        }, textContent = "Cвоё изображение", modifier = Modifier.fillMaxWidth(0.82f)
    )

    MainTextField(
        value = user.nickname,
        input = { vm.updateUserDataSignUp(user.copy(nickname = it)) },
        placeholder = "nikname",
        lable = "Введите псевдоним:",
        trailingText = ""
    )

    MainButton(
        onClick = {
            vm.signUp()
        }, textContent = station.buttonName, modifier = Modifier.fillMaxWidth(0.82f)
    )

}