package com.example.development.mainActivity.components

import android.util.Log
import android.widget.RatingBar
import android.widget.ScrollView
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
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.fillMaxHeight
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.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.outlined.Star
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.IconToggleButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
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.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.example.development.R
import com.example.development.domain.utils.Constants
import com.example.development.mainActivity.MainViewModel
import com.example.development.model.Menu
import com.example.development.model.Review
import com.example.development.model.User
import com.example.development.ui.theme.OldStandart
import io.github.jan.supabase.gotrue.auth
import io.github.jan.supabase.postgrest.from
import io.github.jan.supabase.postgrest.query.Columns
import io.github.jan.supabase.storage.BucketItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.Locale.filter


@Composable
fun Reviews(navController: NavHostController, viewModel: MainViewModel)
{
    val context = LocalContext.current
    val columns = Columns.raw("""id,
            user(
                surname,
                name
            ),
            estimation,
            comment,
            created_at""".trimIndent())
    var reviews by remember { mutableStateOf<List<Review>>(listOf()) }
    var bucket: ByteArray? = null
    var bytes: ByteArray? = null
    var files: List<BucketItem>? = null

    var rating by remember { mutableStateOf(0f) }

    LaunchedEffect(Unit) {
        withContext(Dispatchers.IO) {
            reviews = Constants.supabase.from("reviews")
                .select().decodeList<Review>()
            reviews.forEach { it ->
                Log.d("P", it.id.toString())
            }
        }
    }

    Box(contentAlignment = Alignment.TopCenter,
        modifier = Modifier
            .fillMaxSize()
    )
    {
        Image(
            painter = painterResource(id = R.drawable.fone),
            contentDescription = "",
            contentScale = ContentScale.FillWidth,
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()
        )
        Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Top, horizontalAlignment = Alignment.CenterHorizontally)
        {
            Row(modifier = Modifier
                .padding(5.dp)
                .fillMaxWidth(),
                horizontalArrangement = Arrangement.End
            ){
                Text("ОТЗЫВЫ",
                    color = Color(255, 215, 0),
                    modifier = Modifier.padding(30.dp),
                    fontSize = 30.sp,
                    fontWeight = FontWeight.Bold,
                    fontFamily = OldStandart
                )
                Button(
                    modifier = Modifier
                        .padding(0.dp),
                    onClick = {navController.navigate("MainPage")},
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color(255, 215, 0, 0),
                        contentColor = Color.Gray
                    ),
                    border = BorderStroke(0.dp, color = Color(255, 215, 0, 0))
                )
                {
                    Image(
                        painter = painterResource(id = R.drawable.home),
                        contentDescription = "",
                        contentScale = ContentScale.FillWidth,
                        modifier = Modifier
                            .width(60.dp)
                            .height(60.dp)
                    )
                }
            }
            LazyColumn {
                items(
                    reviews,
                    key = { reviews -> reviews.id!! },
                ) { reviews ->
                    var user by remember { mutableStateOf<List<User>>(listOf()) }
                    LaunchedEffect(Unit) {
                        withContext(Dispatchers.IO) {
                            user = Constants.supabase.from("users")
                                .select(){
                                    filter{
                                        User::UID eq reviews.user
                                    }
                                }.decodeList<User>()
                            user.forEach { it ->
                                it.UID?.let { it1 -> Log.d("P", it1) }
                            }
                        }
                    }
                    Column(modifier = Modifier
                        .fillMaxWidth()
                        .padding(bottom = 5.dp)
                        .clip(RoundedCornerShape(15.dp))
                        .background(color = Color(236, 236, 236, 130)),
                        verticalArrangement = Arrangement.Center,
                        horizontalAlignment = Alignment.CenterHorizontally)
                    {
                        LazyRow {
                            items(
                                user,
                                key = { user -> user.UID!! },
                            ) { user ->
                                Text(
                                    user.name + " " + user.surname,
                                    modifier = Modifier.padding(8.dp),
                                    color = Color.Black,
                                    fontSize = 18.sp,
                                    fontWeight = FontWeight.Normal,
                                    fontFamily = OldStandart
                                )
                            }
                        }
                        Text(
                            reviews.created_at.toString(),
                            modifier = Modifier.padding(8.dp),
                            color = Color.Black,
                            fontSize = 18.sp,
                            fontWeight = FontWeight.Normal,
                            fontFamily = OldStandart
                        )
                        if (reviews.estimation == 1)
                        {
                            Row(modifier = Modifier.padding(5.dp))
                            {
                                Icon(Icons.Default.Star, contentDescription = "Одна звезда")
                            }
                        }
                        if (reviews.estimation == 2)
                        {
                            Row(modifier = Modifier.padding(5.dp))
                            {
                                Icon(Icons.Default.Star, contentDescription = "Одна звезда")
                                Icon(Icons.Default.Star, contentDescription = "Вторая звезда")
                            }
                        }
                        if (reviews.estimation == 3)
                        {
                            Row(modifier = Modifier.padding(5.dp))
                            {
                                Icon(Icons.Default.Star, contentDescription = "Одна звезда")
                                Icon(Icons.Default.Star, contentDescription = "Вторая звезда")
                                Icon(Icons.Default.Star, contentDescription = "Третья звезда")
                            }
                        }
                        if (reviews.estimation == 4)
                        {
                            Row(modifier = Modifier.padding(5.dp))
                            {
                                Icon(Icons.Default.Star, contentDescription = "Одна звезда")
                                Icon(Icons.Default.Star, contentDescription = "Вторая звезда")
                                Icon(Icons.Default.Star, contentDescription = "Третья звезда")
                                Icon(Icons.Default.Star, contentDescription = "Четвертая звезда")
                            }
                        }
                        if (reviews.estimation == 5)
                        {
                            Row(modifier = Modifier.padding(5.dp))
                            {
                                Icon(Icons.Default.Star, contentDescription = "Одна звезда")
                                Icon(Icons.Default.Star, contentDescription = "Вторая звезда")
                                Icon(Icons.Default.Star, contentDescription = "Третья звезда")
                                Icon(Icons.Default.Star, contentDescription = "Четвертая звезда")
                                Icon(Icons.Default.Star, contentDescription = "Пятая звезда")
                            }
                        }
                        Text(
                            reviews.comment,
                            modifier = Modifier.padding(8.dp),
                            color = Color.Black,
                            fontSize = 18.sp,
                            fontWeight = FontWeight.Normal,
                            fontFamily = OldStandart
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun CreateReviews(navController: NavHostController, viewModel: MainViewModel)
{
    var rating by remember { mutableStateOf(0f) }
    Box(contentAlignment = Alignment.TopCenter,
        modifier = Modifier
            .fillMaxSize()
    )
    {
        Image(
            painter = painterResource(id = R.drawable.fone),
            contentDescription = "",
            contentScale = ContentScale.FillWidth,
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()
        )
        Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Top, horizontalAlignment = Alignment.CenterHorizontally)
        {
            Text("Оцените качество обслуживания",
                color = Color(255,215,0),
                modifier = Modifier.padding(top = 100.dp),
                fontSize = 22.sp,
                fontWeight = FontWeight.Bold,
                fontFamily = OldStandart,
                textAlign = TextAlign.Center
            )
            var stars = remember { mutableStateOf(Int) }
            TextField(
                value = "",
                onValueChange = {},
                Modifier
                    .padding(20.dp)
                    .fillMaxWidth()
                    .height(400.dp)
            )

            TextField(
                value = "",
                onValueChange = {},
                Modifier
                    .padding(20.dp)
                    .fillMaxWidth()
                    .height(400.dp)
            )

            Button(modifier = Modifier,
                onClick = {},
                shape = RoundedCornerShape(23.dp),
                border = BorderStroke(0.dp, Color(0xFFEBEBEB)),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(255,215,0),
                    contentColor = Color.White)
            )
            {
                Text("ОТПРАВИТЬ",
                    fontFamily = OldStandart)
            }
        }
    }
}
