package com.example.mystictale.Screen.registration

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.gestures.detectTapGestures
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.Spacer
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.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
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.paint
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
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 com.example.mystictale.R
import com.example.mystictale.navigation.Screens
import com.example.mystictale.resources.DateOfBirthTextField
import com.example.mystictale.ui.theme.DarkPurple
import com.example.mystictale.ui.theme.Grey
import com.example.mystictale.ui.theme.OpenSans
import com.vanpra.composematerialdialogs.MaterialDialog
import com.vanpra.composematerialdialogs.datetime.date.datepicker
import com.vanpra.composematerialdialogs.rememberMaterialDialogState
import java.time.LocalDate
import java.time.format.DateTimeFormatter

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun RegistrationDateOfBirth(navController: NavController) {
    val focusManager = LocalFocusManager.current
    val keyboardController = LocalSoftwareKeyboardController.current
    var pickedDate by remember { mutableStateOf(LocalDate.now()) }
    val formattedDate by remember {
        derivedStateOf { DateTimeFormatter.ofPattern("ddMMyyyy").format(pickedDate) }
    }
    val formattedDateFromBase by remember {
        derivedStateOf { DateTimeFormatter.ofPattern("yyyy-MM-dd").format(pickedDate) }
    }
    val dateDialogState = rememberMaterialDialogState()
    val dateOfBirth = remember {
        mutableStateOf("")
    }
    Column(
        Modifier
            .fillMaxSize()
            .paint(
                painterResource(id = R.drawable.background),
                contentScale = ContentScale.FillBounds
            )
            .padding(10.dp)
            .pointerInput(Unit) {
                detectTapGestures(onTap = {
                    keyboardController?.hide()
                    focusManager.clearFocus()
                })
            },
        verticalArrangement = Arrangement.SpaceBetween,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Column {
            Box(
                modifier = Modifier
                    .padding(top = 30.dp),
                contentAlignment = Alignment.TopStart
            ) {
                Button(
                    onClick = {navController.navigate(Screens.RegistrationName.route) }, colors = ButtonColors(
                        disabledContentColor = Color.White,
                        disabledContainerColor = Color.Transparent,
                        contentColor = Color.White,
                        containerColor = Color.Transparent
                    )
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.back),
                        contentDescription = "back",
                        modifier = Modifier
                            .width(11.dp)
                            .height(14.dp)
                    )
                }
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 22.dp),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically
                ) {


                    LinearProgressIndicator(
                        progress = 0.8f,
                        modifier = Modifier.width(157.dp),
                        strokeCap = StrokeCap.Round,
                        trackColor = Grey,
                        color = DarkPurple
                    )
                }
            }
            Spacer(modifier = Modifier.height(30.dp))
            Text(
                text = "Ваша дата рождения",
                fontWeight = FontWeight.Bold,
                fontFamily = OpenSans,
                fontSize = 24.sp,
                color = Color.White,
                modifier = Modifier
                    .fillMaxWidth(0.9f)
                    .padding(start = 35.dp),
                style = MaterialTheme.typography.headlineMedium
            )
            Spacer(modifier = Modifier.height(30.dp))

            DateOfBirthTextField(
                dateOfBirth = dateOfBirth.value,
                onValue = { dob -> dateOfBirth.value = dob.filter { it.isDigit() } },
                dateDialogState = dateDialogState
            )
        }

        Box(Modifier.padding(bottom = 40.dp)) {
            Button(
                onClick = { navController.navigate(Screens.RegistrationGender.route) },
                modifier = Modifier
                    .width(290.dp)
                    .height(48.dp),
                shape = RoundedCornerShape(12.dp),
                colors = ButtonColors(
                    containerColor = DarkPurple,
                    contentColor = Color.White,
                    disabledContentColor = Color.White,
                    disabledContainerColor = DarkPurple
                )
            ) {
                Text("Далее", fontSize = 20.sp, fontWeight = FontWeight.Bold)

            }

        }


    }

    MaterialDialog(
        dialogState = dateDialogState,
        buttons = {
            positiveButton(text = "Ok")
            negativeButton("cancel")
        })
    {
        datepicker(
            initialDate = LocalDate.now(),
            allowedDateValidator = {
                it <= LocalDate.now()
            }


        ) {
            pickedDate = it
            dateOfBirth.value = formattedDate.toString()

        }
    }
}


