package com.example.emailcode

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.emailcode.ui.theme.EmailCodeTheme

private fun connectedUnputtedCode(
    textList:List<MutableState<TextFieldValue>>,
    onVerifyCode: ((success: Boolean) -> Unit)? = null
) {
    var code = ""
    for (text in textList){
        code += text.value.text
    }

    if (code.length == 4) verifyCode(code, onSuccess = {
        onVerifyCode?.let{
            it(true)
        }
    }, onError = {
        onVerifyCode?.let{
            it(false)
        }
    }
}


private fun NextFocus
            (textList: List<MutableState<TextFieldValue>>,
             requesterList: List<FocusRequester>)
{
    for (index in textList.indices){
        if (textList[index].value.text == ""){
            if (index < textList.size){
                requesterList[index].requestFocus()
                break
            }
        }
    }
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun ContentView(
    textList:List<MutableState<TextFieldValue>>,
    requesterList: List<FocusRequester>
) {
    val focusManager = LocalFocusManager.current
    val keyboardController = LocalSoftwareKeyboardController.current
    val context = LocalContext.current

    Surface(
        modifier = Modifier
            .fillMaxSize(),
        color = Color.DarkGray

    ){
        Box(
            modifier = Modifier
                .fillMaxSize()
        ){
            Row(
                modifier = Modifier
                    .padding(horizontal = 16.dp)
                    .padding(top = 50.dp)
                    .align(Alignment.TopCenter)
            ){
                for (i in textList.indices){
                    InputView(
                        value = textList[i].value,
                        onValueChange = {newValue ->

                            if (textList[i].value.text != ""){

                                if (newValue.text == ""){
                                    textList[i].value = TextFieldValue(
                                        text = "",
                                        selection = TextRange(0)
                                    )
                                }
                                return@InputView
                            }

                            textList[i].value = TextFieldValue(
                                text = newValue.text,
                                selection = TextRange(newValue.text.length)
                            )

                            NextFocus(textList, requesterList)
                        },
                        focusRequester = requesterList[i]
                    )
                }
            }
            LaunchedEffect(key1 = null, block = {
                delay(300)
                requesterList[0].requestFocus()
            })
        }
    }
}
private fun verifyCode(code: String, onSuccess: () -> Unit, onError: () -> Unit){
    if (code == MainActivity.TEST_VERIFY_CODE){
        onSuccess()
    }
    else{
        onError()
    }
}

class MainActivity : ComponentActivity() {

    companion object{
        const val TEST_VERIFY_CODE = "6768"
    }

    private val textList = listOf(
        mutableStateOf(
            TextFieldValue(
                text = "",
                selection = TextRange(0)
            )
        ),
        mutableStateOf(
            TextFieldValue(
                text = "",
                selection = TextRange(0)
            )
        ),
        mutableStateOf(
            TextFieldValue(
                text = "",
                selection = TextRange(0)
            )
        ),
        mutableStateOf(
            TextFieldValue(
                text = "",
                selection = TextRange(0)
            )
        )
    )


    private val requesterList = listOf(
        FocusRequester(),
        FocusRequester(),
        FocusRequester(),
        FocusRequester()
    )
    override fun onCreate(savedInstanceState: Bundle?){
        super.onCreate(savedInstanceState)

        actionBar?.hide()

        setContent {
            ContentView(
                textList = textList,
                requesterList = requesterList
            )
        }
    }

}



@Composable
private fun InputView(
    value: TextFieldValue,
    onValueChange: (value: TextFieldValue) -> Unit,
    focusRequester: FocusRequester
){
    BasicTextField(
        readOnly = false,
        value = value,
        onValueChange = onValueChange,
        modifier = Modifier
            .padding(horizontal = 10.dp)
            .clip(RoundedCornerShape(8.dp))
            .background(Color.Gray)
            .wrapContentSize()
            .focusRequester(focusRequester),
        maxLines = 1,
        decorationBox = { innerTextField ->
            Box(
                modifier = Modifier
                    .width(50.dp)
                    .height(70.dp),
                contentAlignment = Alignment.Center
            ) {
                innerTextField()
            }
        },
        cursorBrush = SolidColor(Color.White),
        textStyle = TextStyle(
            color = Color.White,
            fontSize = 26.sp,
            fontWeight = FontWeight.Bold,
            textAlign = TextAlign.Center
        ),
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number, imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(
            onDone = null
        )
    )
}