package com.example.tasks.screens

import android.content.Context
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Create
import androidx.compose.material.icons.filled.ExitToApp
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
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.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import coil.compose.AsyncImagePainter
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import coil.size.Size
import com.example.tasks.R
import com.example.tasks.components.CustomProfile
import com.example.tasks.model.UserState
import com.example.tasks.navigation.Screen
import com.example.tasks.viewModels.AuthViewModel
import com.example.tasks.viewModels.DatabaseViewModel

@Composable
fun Profile(
    viewModel: DatabaseViewModel,
    viewModelAuth: AuthViewModel,
    navController: NavController
) {
    val userState by viewModel.userState
    val users by viewModel.users
    val flag = remember { mutableStateOf(true) }
    val flag1 = remember { mutableStateOf(false) }
    val context = LocalContext.current
    val name = remember {
        mutableStateOf("")
    }
    val surname  = remember {
        mutableStateOf("")
    }
    if (flag.value) {
        viewModel.profileData()
        flag.value = false
    }
    val enabled = remember {
        mutableStateOf(false)
    }



    if (users.isNotEmpty()) {
        name.value = users.last().FirstName
        surname.value = users.last().LastName
        val avatar = rememberAsyncImagePainter(
            model = ImageRequest.Builder(LocalContext.current).data(users.last().avatar)
                .size(Size.ORIGINAL).build()
        ).state
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(30.dp)
                .padding(top = 30.dp, bottom = 100.dp),
            verticalArrangement = Arrangement.SpaceAround,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(0.7f),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    "Профиль",
                    fontSize = 30.sp,
                    fontWeight = FontWeight.W700,
                    color = Color(0xFF4C2259)
                )
                Icon(Icons.Filled.ExitToApp, "",
                    Modifier
                        .background(Color.Red)
                        .clickable {
                            viewModelAuth.logout(context)
                            flag1.value = true
                        })
                if(enabled.value == false){
                    Icon(Icons.Filled.Create, "",
                        Modifier
                            .clickable {
                                enabled.value = true
                            })
                }
                else{
                    Button(onClick = { /*TODO*/ }) {
                        Text(text = "Save")
                    }
                }
            }

            if (avatar is AsyncImagePainter.State.Success) {
                Box(
                    modifier = Modifier
                        .width(100.dp)
                        .height(100.dp)
                        .clip(RoundedCornerShape(50.dp))
                ) {
                    Image(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(200.dp),
                        painter = avatar.painter,
                        contentDescription = "",
                        contentScale = ContentScale.Crop
                    )
                }
            }
            if (avatar is AsyncImagePainter.State.Loading) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(200.dp),
                    contentAlignment = Alignment.Center
                ) {
                    CircularProgressIndicator()
                }
            }
            if (avatar is AsyncImagePainter.State.Error) {
                Box(
                    modifier = Modifier
                        .width(100.dp)
                        .height(100.dp)
                        .clip(RoundedCornerShape(50.dp))
                ) {
                    if(users.last().Gender == 1)
                    {
                        Image(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(200.dp),
                            painter = painterResource (R.drawable.girl),
                            contentDescription = "",
                            contentScale = ContentScale.Crop
                        )
                    }
                    else{
                        Image(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(200.dp),
                            painter = painterResource (R.drawable.boy),
                            contentDescription = "",
                            contentScale = ContentScale.Crop
                        )
                    }
                }
            }

            CustomProfile(search = name.value, onValueChange = {name.value = it}, enabled = enabled.value)
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(50.dp)
                    .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
                    .clip(RoundedCornerShape(10.dp))
                    .background(
                        Color(0xFFF2D6FA)
                    )
                    .padding(10.dp),
                contentAlignment = Alignment.CenterStart
            ) {
                Text(users.last().FirstName)
            }
            if (users.last().Patronymic != "") {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(50.dp)
                        .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
                        .clip(RoundedCornerShape(10.dp))
                        .background(
                            Color(0xFFF2D6FA)
                        )
                        .padding(10.dp),
                    contentAlignment = Alignment.CenterStart
                ) {
                    Text(users.last().Patronymic!!)
                }
            }
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(50.dp)
                    .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
                    .clip(RoundedCornerShape(10.dp))
                    .background(
                        Color(0xFFF2D6FA)
                    )
                    .padding(10.dp),
                contentAlignment = Alignment.CenterStart
            ) {
                Text(users.last().DateOfBirth)
            }
            if (users.last().Gender == 1) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(50.dp)
                        .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
                        .clip(RoundedCornerShape(10.dp))
                        .background(
                            Color(0xFFF2D6FA)
                        )
                        .padding(10.dp),
                    contentAlignment = Alignment.CenterStart
                ) {
                    Text("Женский")
                }
            } else {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(50.dp)
                        .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
                        .clip(RoundedCornerShape(10.dp))
                        .background(
                            Color(0xFFF2D6FA)
                        )
                        .padding(10.dp),
                    contentAlignment = Alignment.CenterStart
                ) {
                    Text("Мужской")
                }
            }

        }
    } else {
        Column(Modifier.fillMaxSize()) {
            CircularProgressIndicator()
        }
    }

    if(flag1.value){
        when (userState) {
            is UserState.Loading -> {
                CircularProgressIndicator()
            }

            is UserState.Success -> {
                navController.navigate(Screen.Start.route)
                flag1.value = false
            }


            is UserState.Error -> {
                val message = (userState as UserState.Error).message
                Toast.makeText(context,message,Toast.LENGTH_SHORT).show()
                flag1.value = false
            }

        }
    }


}
