package com.example.development.mainActivity.components

import android.widget.ScrollView
import android.widget.Toast
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.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Create
import androidx.compose.material.icons.filled.DateRange
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.DatePicker
import androidx.compose.material3.Divider
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
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.mainActivity.MainViewModel
import com.example.development.model.Category
import com.example.development.ui.theme.OldStandart

@Composable
fun TableReserve(navController: NavHostController, viewModel: MainViewModel) {
    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
                .padding(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        )
        {
            Row(modifier = Modifier
                .padding(5.dp)
                .fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceAround, verticalAlignment = Alignment.CenterVertically
            ){
                Text("СХЕМА ЗАЛА",
                    color = Color(255, 215, 0),
                    modifier = Modifier.padding(30.dp),
                    fontSize = 30.sp,
                    fontWeight = FontWeight.Bold,
                    fontFamily = OldStandart
                )
                Button(
                    modifier = Modifier.fillMaxWidth()
                        ,
                    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)
                    )
                }
            }
            Image(
                painter = painterResource(id = R.drawable.schema),
                contentDescription = "",
                contentScale = ContentScale.FillWidth,
                modifier = Modifier.padding(30.dp)
            )
            Button(modifier = Modifier
                .width(width = 270.dp)
                .height(height = 40.dp),
                onClick = {navController.navigate("TableReserve2")},
                shape = RoundedCornerShape(23.dp),
                border = BorderStroke(0.dp, Color(0xFFEBEBEB)),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(255,215,0),
                    contentColor = Color.White)
            )
            {
                Text("ЗАБРОНИРОВАТЬ СТОЛ",
                    fontSize = 20.sp,
                    color = Color.White,
                    fontWeight = FontWeight.Normal,
                    fontFamily = OldStandart)
            }
        }
    }
}

@Composable
fun TableReserve2(navController: NavHostController, viewModel: MainViewModel)
{
    val context = LocalContext.current
    val flagNotNull = remember {mutableStateOf(false)}
    val nameFlag = remember {mutableStateOf(false)}
    val phoneFlag = remember { mutableStateOf(false) }
    val personFlag = remember { mutableStateOf(false) }
    val commentFlag = remember { mutableStateOf(false) }
    val emailFlag = remember { mutableStateOf(false) }
    val surnameFlag = remember { mutableStateOf(false) }
    val surname = remember { mutableStateOf("") }
    val name = remember { mutableStateOf("") }
    val phoneNumber = remember { mutableStateOf("") }
    val person = remember { mutableStateOf("") }
    val comment = remember { mutableStateOf("") }
    val email = remember { mutableStateOf("") }
    val tables = remember {mutableStateOf(String)}
    val tableFlag = remember { mutableStateOf(false) }
    val data = remember { mutableStateOf("") }
    val dataFlag = remember { mutableStateOf(false) }

    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
                .padding(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        )
        {
            Row(modifier = Modifier
                .padding(5.dp)
                .fillMaxWidth(),
                horizontalArrangement = Arrangement.End, verticalAlignment = Alignment.CenterVertically
            ){
                Text("РЕЗЕРВ СТОЛА",
                    color = Color(255, 215, 0),
                    modifier = Modifier,
                    fontSize = 24.sp,
                    fontWeight = FontWeight.Bold,
                    fontFamily = OldStandart
                )
                Button(
                    modifier = Modifier
                        .padding(0.dp),
                    onClick = {},
                    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)
                    )
                }
            }
            Text(
                "Имя *",
                fontSize = 16.sp,
                color = Color.White,
                fontWeight = FontWeight.Normal,
                fontFamily = OldStandart
            )
            OutlinedTextField(value = name.value,
                onValueChange = { it -> name.value = it },
                leadingIcon = { Icon(Icons.Filled.Person, contentDescription = "Имя") },
                colors = TextFieldDefaults.colors(
                    focusedContainerColor = Color(0XFFF5F5F9),
                    focusedIndicatorColor = Color(0xFFEBEBEB),
                    focusedTextColor = Color.Black,
                    disabledIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent,
                    cursorColor = Color(0XFF578FFF),
                    focusedSupportingTextColor = Color(0xFF00000)
                ),
                modifier = Modifier
                    .fillMaxWidth(1f)
                    .background(Color(0XFFF5F5F9))
                    .height(height = 56.dp)
                    .width(width = 200.dp),
                isError = nameFlag.value,
                placeholder = {
                    Text(
                        modifier = Modifier.fillMaxWidth(),
                        text = "Имя",
                        fontWeight = FontWeight.Normal,
                        fontSize = 15.sp,
                        color = Color(0XFF939396)
                    )
                }
            )

            if (name.value.isEmpty()) {
                nameFlag.value = true
                Text("Введите имя", color = Color.Red, fontSize = 10.sp)
            } else {
                nameFlag.value = false
            }
            Text(
                "Фамилия *",
                fontSize = 16.sp,
                color = Color.White,
                fontWeight = FontWeight.Normal,
                fontFamily = OldStandart
            )
            OutlinedTextField(value = surname.value,
                onValueChange = { it -> surname.value = it },
                leadingIcon = { Icon(Icons.Filled.Person, contentDescription = "Фамилия") },
                colors = TextFieldDefaults.colors(
                    focusedContainerColor = Color(0XFFF5F5F9),
                    focusedIndicatorColor = Color(0xFFEBEBEB),
                    focusedTextColor = Color.Black,
                    disabledIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent,
                    cursorColor = Color(0XFF578FFF),
                    focusedSupportingTextColor = Color(0xFF00000)
                ),
                modifier = Modifier
                    .fillMaxWidth(1f)
                    .background(Color(0XFFF5F5F9))
                    .height(height = 56.dp)
                    .width(width = 200.dp),
                isError = surnameFlag.value,
                placeholder = {
                    Text(
                        modifier = Modifier.fillMaxWidth(),
                        text = "Фамилия",
                        fontWeight = FontWeight.Normal,
                        fontSize = 15.sp,
                        color = Color(0XFF939396)
                    )
                }
            )

            if (surname.value.isEmpty()) {
                surnameFlag.value = true
                Text("Введите фамилию", color = Color.Red, fontSize = 10.sp)
            } else {
                surnameFlag.value = false
            }

            Text(
                "Номер телефона *",
                fontSize = 16.sp,
                color = Color.White,
                fontWeight = FontWeight.Normal,
                fontFamily = OldStandart
            )
            OutlinedTextField(value = phoneNumber.value,
                onValueChange = { it -> phoneNumber.value = it },
                leadingIcon = { Icon(Icons.Filled.Person, contentDescription = "Номер телефона") },
                colors = TextFieldDefaults.colors(
                    focusedContainerColor = Color(0XFFF5F5F9),
                    focusedIndicatorColor = Color(0xFFEBEBEB),
                    focusedTextColor = Color.Black,
                    disabledIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent,
                    cursorColor = Color(0XFF578FFF),
                    focusedSupportingTextColor = Color(0xFF00000)
                ),
                modifier = Modifier
                    .fillMaxWidth(1f)
                    .background(Color(0XFFF5F5F9))
                    .height(height = 56.dp)
                    .width(width = 200.dp),
                isError = phoneFlag.value,
                placeholder = {
                    Text(
                        modifier = Modifier.fillMaxWidth(),
                        text = "Номер телефона",
                        fontWeight = FontWeight.Normal,
                        fontSize = 15.sp,
                        color = Color(0XFF939396)
                    )
                }
            )

            if (phoneNumber.value.isEmpty()) {
                phoneFlag.value = true
                Text("Введите номер телефона", color = Color.Red, fontSize = 10.sp)
            } else {
                phoneFlag.value = false
            }
            Text(
                "E-mail *",
                fontSize = 16.sp,
                color = Color.White,
                fontWeight = FontWeight.Normal,
                fontFamily = OldStandart
            )
            OutlinedTextField(value = email.value,
                onValueChange = { it -> email.value = it },
                leadingIcon = { Icon(Icons.Filled.Person, contentDescription = "E-mail") },
                colors = TextFieldDefaults.colors(
                    focusedContainerColor = Color(0XFFF5F5F9),
                    focusedIndicatorColor = Color(0xFFEBEBEB),
                    focusedTextColor = Color.Black,
                    disabledIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent,
                    cursorColor = Color(0XFF578FFF),
                    focusedSupportingTextColor = Color(0xFF00000)
                ),
                modifier = Modifier
                    .fillMaxWidth(1f)
                    .background(Color(0XFFF5F5F9))
                    .height(height = 56.dp)
                    .width(width = 200.dp),
                isError = emailFlag.value,
                placeholder = {
                    Text(
                        modifier = Modifier.fillMaxWidth(),
                        text = "E-mail",
                        fontWeight = FontWeight.Normal,
                        fontSize = 15.sp,
                        color = Color(0XFF939396)
                    )
                }
            )

            if (email.value.isEmpty()) {
                emailFlag.value = true
                Text("Введите e-mail", color = Color.Red, fontSize = 10.sp)
            } else {
                emailFlag.value = false
            }
            Text(
                "Номер стола *",
                fontSize = 16.sp,
                color = Color.White,
                fontWeight = FontWeight.Normal,
                fontFamily = OldStandart
            )
            var expanded by remember { mutableStateOf(false) }
            var selectedTable by remember {mutableStateOf("")}
            Box {
                Row {
                    IconButton(onClick = { expanded = true }) {
                        Icon(Icons.Default.KeyboardArrowDown, contentDescription = "Выбрать столик")
                    }
                    Text("Выбран стол №$selectedTable", fontSize = 18.sp)
                }
                DropdownMenu(
                    expanded = expanded,
                    onDismissRequest = { expanded = false }
                ) {
                    DropdownMenuItem(
                        onClick = { selectedTable = "1" },
                        text = { Text("Стол №1") }
                    )
                    DropdownMenuItem(
                        onClick = { selectedTable = "2" },
                        text = { Text("Стол №2") }
                    )
                    DropdownMenuItem(
                        onClick = { selectedTable = "3" },
                        text = { Text("Стол №3") }
                    )
                    DropdownMenuItem(
                        onClick = { selectedTable = "4" },
                        text = { Text("Стол №4") }
                    )
                    DropdownMenuItem(
                        onClick = { selectedTable = "5" },
                        text = { Text("Стол №5") }
                    )
                    DropdownMenuItem(
                        onClick = { selectedTable = "6" },
                        text = { Text("Стол №6") }
                    )
                    DropdownMenuItem(
                        onClick = { selectedTable = "7" },
                        text = { Text("Стол №7") }
                    )
                    DropdownMenuItem(
                        onClick = { selectedTable = "8" },
                        text = { Text("Стол №8") }
                    )
                    DropdownMenuItem(
                        onClick = { selectedTable = "9" },
                        text = { Text("Стол №9") }
                    )
                    DropdownMenuItem(
                        onClick = { selectedTable = "10" },
                        text = { Text("Стол №10") }
                    )
                }
            }

            if (selectedTable.isEmpty()) {
                tableFlag.value = true
                Text("Введите номер стола", color = Color.Red, fontSize = 10.sp)
            } else {
                tableFlag.value = false
            }

            Text(
                "Количество человек *",
                fontSize = 16.sp,
                color = Color.White,
                fontWeight = FontWeight.Normal,
                fontFamily = OldStandart
            )
            OutlinedTextField(value = person.value,
                onValueChange = { it -> person.value = it },
                leadingIcon = { Icon(Icons.Filled.Person, contentDescription = "Количество человек") },
                colors = TextFieldDefaults.colors(
                    focusedContainerColor = Color(0XFFF5F5F9),
                    focusedIndicatorColor = Color(0xFFEBEBEB),
                    focusedTextColor = Color.Black,
                    disabledIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent,
                    cursorColor = Color(0XFF578FFF),
                    focusedSupportingTextColor = Color(0xFF00000)
                ),
                modifier = Modifier
                    .fillMaxWidth(1f)
                    .background(Color(0XFFF5F5F9))
                    .height(height = 56.dp)
                    .width(width = 200.dp),
                isError = personFlag.value,
                placeholder = {
                    Text(
                        modifier = Modifier.fillMaxWidth(),
                        text = "Количество человек",
                        fontWeight = FontWeight.Normal,
                        fontSize = 15.sp,
                        color = Color(0XFF939396)
                    )
                }
            )

            if (person.value.isEmpty()) {
                personFlag.value = true
                Text("Введите количество", color = Color.Red, fontSize = 10.sp)
            } else {
                personFlag.value = false
            }

            Text(
                "Дата и время бронирования *",
                fontSize = 16.sp,
                color = Color.White,
                fontWeight = FontWeight.Normal,
                fontFamily = OldStandart
            )
            OutlinedTextField(value = data.value,
                onValueChange = { it -> data.value = it },
                leadingIcon = { Icon(Icons.Filled.DateRange, contentDescription = "Дата и время") },
                colors = TextFieldDefaults.colors(
                    focusedContainerColor = Color(0XFFF5F5F9),
                    focusedIndicatorColor = Color(0xFFEBEBEB),
                    focusedTextColor = Color.Black,
                    disabledIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent,
                    cursorColor = Color(0XFF578FFF),
                    focusedSupportingTextColor = Color(0xFF00000)
                ),
                modifier = Modifier
                    .fillMaxWidth(1f)
                    .background(Color(0XFFF5F5F9))
                    .height(height = 56.dp)
                    .width(width = 200.dp),
                isError = dataFlag.value,
                placeholder = {
                    Text(
                        modifier = Modifier.fillMaxWidth(),
                        text = "Дата и время",
                        fontWeight = FontWeight.Normal,
                        fontSize = 15.sp,
                        color = Color(0XFF939396)
                    )
                }
            )
            if (data.value.isEmpty()) {
                dataFlag.value = true
                Text("Введите дату и время бронирования", color = Color.Red, fontSize = 10.sp)
            } else {
                dataFlag.value = false
            }
            Text(
                "Комментарий",
                fontSize = 16.sp,
                color = Color.White,
                fontWeight = FontWeight.Normal,
                fontFamily = OldStandart
            )
            OutlinedTextField(value = comment.value,
                onValueChange = { it -> comment.value = it },
                leadingIcon = { Icon(Icons.Filled.Create, contentDescription = "Комментарий") },
                colors = TextFieldDefaults.colors(
                    focusedContainerColor = Color(0XFFF5F5F9),
                    focusedIndicatorColor = Color(0xFFEBEBEB),
                    focusedTextColor = Color.Black,
                    disabledIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent,
                    cursorColor = Color(0XFF578FFF),
                    focusedSupportingTextColor = Color(0xFF00000)
                ),
                modifier = Modifier
                    .fillMaxWidth(1f)
                    .background(Color(0XFFF5F5F9))
                    .height(height = 56.dp)
                    .width(width = 200.dp),
                isError = commentFlag.value,
                placeholder = {
                    Text(
                        modifier = Modifier.fillMaxWidth(),
                        text = "Комментарий",
                        fontWeight = FontWeight.Normal,
                        fontSize = 15.sp,
                        color = Color(0XFF939396)
                    )
                }
            )
            if (comment.value.isEmpty()) {
                commentFlag.value = true
                Text("Введите комментарий", color = Color.Red, fontSize = 10.sp)
            } else {
                commentFlag.value = false
            }
            Row()
            {
                Text("Согласен на обработку персональных данных",
                    fontSize = 16.sp,
                    color = Color.White,
                    fontWeight = FontWeight.Normal,
                    fontFamily = OldStandart)
            }
            if (name.value == "" || surname.value == "" || phoneNumber.value == "" || email.value == "" || person.value == "" || data.value == "" || comment.value == "" || selectedTable == "")
            {
                flagNotNull.value = false
            }
            else
            {
                flagNotNull.value = true
            }
            Button(modifier = Modifier
                .width(width = 270.dp)
                .height(height = 40.dp),
            onClick = { if (flagNotNull.value) { viewModel.AddReservation(
                id_table = selectedTable.toInt(),
                date_and_time = data.value,
                quantity_of_person = person.value.toInt(),
                comment = comment.value
            ) }
            else { Toast.makeText(context, "Заполнены не все поля", Toast.LENGTH_SHORT).show() } },
            shape = RoundedCornerShape(23.dp),
            border = BorderStroke(0.dp, Color(0xFFEBEBEB)),
            colors = ButtonDefaults.buttonColors(
                containerColor = Color(255,215,0),
                contentColor = Color.White)
            )
            {
                Text("ОТПРАВИТЬ", fontFamily = OldStandart)
            }
        }
    }
}
