package com.example.tasks.screens

import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
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.PaddingValues
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.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Create
import androidx.compose.material.icons.filled.ExitToApp
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
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.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 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 ProfileSeller(
    viewModel: DatabaseViewModel,
    viewModelAuth: AuthViewModel,
    navController: NavController,
) {
    val userState by viewModel.userState
    val flag = remember { mutableStateOf(true) }
    val flag1 = remember { mutableStateOf(false) }
    val flag2 = remember { mutableStateOf(false) }
    val context = LocalContext.current


    if (flag.value) {
        viewModel.profileDataSeller()
        flag.value = false
    }
    val enabled = remember {
        mutableStateOf(false)
    }



    if (viewModel.sellers.value.isNotEmpty()) {
        val title = remember {
            mutableStateOf(viewModel.sellers.value.last().title_organization)
        }
        val inn = remember {
            mutableStateOf(viewModel.sellers.value.last().inn)
        }
        val country = remember {
            mutableStateOf(viewModel.sellers.value.last().country)
        }
        val avatar = rememberAsyncImagePainter(
            model = ImageRequest.Builder(LocalContext.current)
                .data(viewModel.sellers.value.last().image)
                .size(200, 200).build()
        ).state
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color(0xFF951DDD))
                .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.White
                )
                Icon(
                    Icons.Filled.ExitToApp, "",
                    Modifier
                        .clickable {
                            viewModelAuth.logout(context)
                            flag1.value = true
                        }, tint = Color(0xFFFEB3F6)
                )
                if (enabled.value == false) {
                    Icon(Icons.Filled.Create, "",
                        Modifier
                            .clickable {
                                enabled.value = true
                            },tint = Color.White)
                } else {
                    Button(
                        onClick = { viewModel.updateSeller(title.value,inn.value,country.value)
                                  flag2.value = true}, shape = CircleShape,
                        modifier = Modifier.size(40.dp),
                        colors = ButtonColors(
                            contentColor = Color(0xFF951DDD),
                            containerColor = Color.White,
                            disabledContentColor = Color(0xFF951DDD),
                            disabledContainerColor = Color.White
                        ),
                        contentPadding = PaddingValues(1.dp)
                    ) {
                        Icon(Icons.Filled.Check, "", tint = Color(0xFF951DDD))
                    }
                }
            }

            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()
                }
            }



            CustomProfile(
                search = title.value,
                onValueChange = { title.value = it },
                enabled = enabled.value
            )
            CustomProfile(
                search = inn.value,
                onValueChange = { inn.value = it },
                enabled = enabled.value
            )
            CustomProfile(
                search = country.value,
                onValueChange = { country.value = it },
                enabled = enabled.value
            )

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

                    is UserState.Success -> {
                        viewModel.cleanUsers()
                        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
                    }

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

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


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

                }
            }


        }
    } else {
        CircularProgressIndicator()
    }
}
