package com.tapjoy.easyapp

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.layout.Arrangement
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.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
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.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.PopupProperties
import com.tapjoy.easyapp.theme.Yellow700

@Preview
@Composable
fun ConfigScreen(viewModel: ConfigViewModel = ConfigViewModel()) {
    val context = LocalContext.current
    val sdksList by viewModel.sdksList.collectAsState()
    val isAutoConnectedChecked by viewModel.checked.collectAsState()
    val sdkKeyInput by viewModel.sdkKeyInput.collectAsState()
    val statusMessage by viewModel.statusMessage.collectAsState()
    var isExpanded: Boolean by remember { mutableStateOf(false) }
    val preferences = context.getSharedPreferences(PREFERENCES, 0)

    LaunchedEffect(Unit) {
        viewModel.initSdksList(preferences)
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .wrapContentSize(Alignment.Center),
    ) {
        Spacer(modifier = Modifier.size(20.dp))
        Text(
            text = statusMessage,
            color = Color.Black,
            modifier = Modifier.align(Alignment.CenterHorizontally),
            textAlign = TextAlign.Center,
            fontSize = 16.sp,
        )
        Spacer(modifier = Modifier.size(20.dp))
        Row(modifier = Modifier.align(Alignment.Start)) {
            Checkbox(
                modifier = Modifier.padding(start = 10.dp),
                checked = isAutoConnectedChecked,
                onCheckedChange = {
                    viewModel.setAutoConnect(preferences)
                },
            )
            Text(
                modifier = Modifier
                    .align(Alignment.CenterVertically)
                    .padding(end = 10.dp),
                text = stringResource(id = R.string.auto_connect),
            )
            Spacer(modifier = Modifier.size(10.dp))
        }

        Text(
            text = stringResource(id = R.string.hint_sdk_key),
            color = Color.Black,
            modifier = Modifier.padding(top = 10.dp, start = 20.dp, end = 20.dp),
            fontSize = 16.sp,
        )

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 20.dp, end = 20.dp),
            verticalAlignment = Alignment.Top,
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            TextField(
                value = sdkKeyInput,
                singleLine = true,
                interactionSource = remember { MutableInteractionSource() }
                    .also { interactionSource ->
                        LaunchedEffect(interactionSource) {
                            interactionSource.interactions.collect {
                                if (it is PressInteraction.Release) {
                                    isExpanded = true
                                }
                            }
                        }
                    },
                trailingIcon = {
                    Icon(
                        Icons.Default.Clear,
                        contentDescription = stringResource(id = R.string.clear),
                        modifier = Modifier
                            .clickable {
                                viewModel.setSdkInput("")
                            },
                    )
                },
                modifier = Modifier.weight(1f),
                onValueChange = {
                    viewModel.setSdkInput(it)
                },
            )
            Button(
                modifier = Modifier
                    .align(Alignment.CenterVertically)
                    .padding(start = 20.dp)
                    .background(color = Yellow700, shape = RoundedCornerShape(4.dp))
                    .width(50.dp)
                    .height(50.dp),
                onClick = {
                    viewModel.addSdkKeyClicked(preferences)
                    isExpanded = !isExpanded
                },
            ) {
                Text(
                    text = "+",
                    color = Color.Black,
                    fontSize = 18.sp,
                    textAlign = TextAlign.Center,
                )
            }
            DropdownMenu(
                expanded = isExpanded,
                properties = PopupProperties(focusable = false),
                onDismissRequest = { isExpanded = false },
                modifier = Modifier.fillMaxWidth(),
            ) {
                sdksList.forEach { sdkKey ->
                    DropdownMenuItem(text = {
                        Text(text = sdkKey)
                    }, onClick = {
                        viewModel.onSdkItemSelected(preferences, sdkKey)
                        isExpanded = false
                    })
                }
            }
        }

        Spacer(modifier = Modifier.weight(1f))
    }
}
