package com.tapjoy.easyapp

import android.widget.Toast
import androidx.compose.foundation.clickable
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.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material.icons.filled.ArrowDropUp
import androidx.compose.material3.Button
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
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.input.KeyboardType
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 com.tapjoy.easyapp.theme.Yellow700

@Preview
@Composable
fun UserScreen(viewModel: UserViewModel = UserViewModel()) {
    val context = LocalContext.current

    val userIdInput by viewModel.userIdInput.collectAsState()
    val userLevelInput by viewModel.userLevelInput.collectAsState()
    val maxLevelInput by viewModel.maxLevelInput.collectAsState()
    val userTagInput by viewModel.userTagInput.collectAsState()
    val userPrivacyInput by viewModel.usPrivacyInput.collectAsState()

    val segmentSelected by viewModel.segmentSelected.collectAsState()
    val gdprSelected by viewModel.gdprSelected.collectAsState()
    val consentSelected by viewModel.consentSelected.collectAsState()
    val belowAgeSelected by viewModel.belowAgeSelected.collectAsState()
    val usageStatsSelected by viewModel.usageStatsSelected.collectAsState()
    val optOutOfAdvertisingId by viewModel.optOutOfAdvertisingId.collectAsState()

    var segmentExpanded: Boolean by remember { mutableStateOf(false) }
    var gdprExpanded: Boolean by remember { mutableStateOf(false) }
    var consentExpanded: Boolean by remember { mutableStateOf(false) }
    var belowAgeExpanded: Boolean by remember { mutableStateOf(false) }
    var usageStatsExpanded: Boolean by remember { mutableStateOf(false) }

    val segmentIcon = if (segmentExpanded) Icons.Filled.ArrowDropUp else Icons.Filled.ArrowDropDown
    val gdprIcon = if (gdprExpanded) Icons.Filled.ArrowDropUp else Icons.Filled.ArrowDropDown
    val consentIcon = if (consentExpanded) Icons.Filled.ArrowDropUp else Icons.Filled.ArrowDropDown
    val belowAgeIcon = if (belowAgeExpanded) Icons.Filled.ArrowDropUp else Icons.Filled.ArrowDropDown
    val usageStatsIcon = if (usageStatsExpanded) Icons.Filled.ArrowDropUp else Icons.Filled.ArrowDropDown

    LaunchedEffect(Unit) {
        viewModel.initUserDetails(context)
        viewModel.toastMessage
            .collect { message ->
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
            }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .wrapContentSize(Alignment.TopCenter)
            .verticalScroll(rememberScrollState()),
    ) {
        Spacer(modifier = Modifier.height(20.dp))
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Text(
                modifier = Modifier
                    .width(120.dp)
                    .padding(start = 20.dp, end = 20.dp),
                text = stringResource(id = R.string.user_id),
                color = Color.Black,
                textAlign = TextAlign.Start,
                fontSize = 16.sp,
            )
            TextField(
                value = userIdInput,
                singleLine = true,
                modifier = Modifier
                    .padding(end = 20.dp, top = 5.dp, bottom = 5.dp)
                    .weight(1f),
                onValueChange = {
                    viewModel.updateUserIdInput(it)
                },
            )
        }
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Text(
                modifier = Modifier
                    .width(120.dp)
                    .padding(start = 20.dp, end = 20.dp),
                text = stringResource(id = R.string.user_level),
                color = Color.Black,
                textAlign = TextAlign.Start,
                fontSize = 16.sp,
            )
            TextField(
                value = userLevelInput,
                singleLine = true,
                keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
                modifier = Modifier
                    .weight(1f)
                    .padding(end = 20.dp, top = 5.dp, bottom = 5.dp),
                onValueChange = {
                    viewModel.updateUserLevelInput(it)
                },
            )
        }
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Text(
                modifier = Modifier
                    .width(120.dp)
                    .padding(start = 20.dp, end = 20.dp),
                text = stringResource(id = R.string.max_level),
                color = Color.Black,
                textAlign = TextAlign.Start,
                fontSize = 16.sp,
            )
            TextField(
                value = maxLevelInput,
                singleLine = true,
                keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
                modifier = Modifier
                    .weight(1f)
                    .padding(end = 20.dp, top = 5.dp, bottom = 5.dp),
                onValueChange = {
                    viewModel.updateMaxLevelInput(it)
                },
            )
        }

        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start,
        ) {
            Text(
                modifier = Modifier
                    .align(Alignment.CenterVertically)
                    .padding(20.dp),
                text = stringResource(id = R.string.user_segment),
                color = Color.Black,
                textAlign = TextAlign.Start,
                fontSize = 16.sp,
            )

            Column {
                Row {
                    Text(segmentSelected, modifier = Modifier.clickable { segmentExpanded = !segmentExpanded })
                    Icon(imageVector = segmentIcon, "", Modifier.clickable { segmentExpanded = !segmentExpanded })
                }
                DropdownMenu(
                    expanded = segmentExpanded,
                    onDismissRequest = { segmentExpanded = false },
                ) {
                    viewModel.segmentList.forEach { label ->
                        DropdownMenuItem(text = {
                            Text(text = label)
                        }, onClick = {
                            viewModel.updateSegmentSelected(label)
                            segmentExpanded = false
                        })
                    }
                }
            }
        }
        Row(horizontalArrangement = Arrangement.SpaceEvenly) {
            Button(
                modifier = Modifier
                    .weight(1f)
                    .padding(start = 20.dp, end = 10.dp),
                onClick = {
                    viewModel.setUserInfo()
                },
            ) {
                Text(text = stringResource(id = R.string.set), fontSize = 16.sp)
            }
            Button(
                modifier = Modifier
                    .weight(1f)
                    .padding(start = 10.dp, end = 20.dp),
                onClick = {
                    viewModel.clearUserInput()
                },
            ) {
                Text(text = stringResource(id = R.string.clear), fontSize = 16.sp)
            }
        }
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 10.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Text(
                modifier = Modifier
                    .width(120.dp)
                    .padding(start = 20.dp, end = 20.dp),
                text = stringResource(id = R.string.user_tag),
                color = Color.Black,
                textAlign = TextAlign.Start,
                fontSize = 16.sp,
            )
            TextField(
                value = userTagInput,
                singleLine = true,
                modifier = Modifier
                    .weight(1f)
                    .padding(end = 20.dp, top = 5.dp, bottom = 5.dp),
                onValueChange = {
                    viewModel.updateUserTagInput(it)
                },
            )
        }
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.Top,
            horizontalArrangement = Arrangement.SpaceEvenly,
        ) {
            Button(
                modifier = Modifier
                    .weight(1f)
                    .padding(start = 20.dp, end = 10.dp),
                onClick = {
                    viewModel.addUserTag()
                },
            ) {
                Text(text = stringResource(id = R.string.add), fontSize = 16.sp)
            }
            Button(
                modifier = Modifier
                    .weight(1f)
                    .padding(start = 10.dp, end = 10.dp),
                onClick = {
                    viewModel.removeUserTag()
                },
            ) {
                Text(text = stringResource(id = R.string.remove), fontSize = 16.sp)
            }
            Button(
                modifier = Modifier
                    .weight(1f)
                    .padding(start = 10.dp, end = 20.dp),
                onClick = {
                    viewModel.clearUserTags()
                },
            ) {
                Text(text = stringResource(id = R.string.clear), fontSize = 16.sp)
            }
        }
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start,
        ) {
            Text(
                modifier = Modifier
                    .align(Alignment.CenterVertically)
                    .padding(horizontal = 20.dp, vertical = 10.dp),
                text = stringResource(id = R.string.subject_to_gpr),
                color = Color.Black,
                textAlign = TextAlign.Start,
                fontSize = 16.sp,
            )

            Column {
                Row {
                    Text(gdprSelected, modifier = Modifier.clickable { gdprExpanded = !gdprExpanded })
                    Icon(imageVector = gdprIcon, "", Modifier.clickable { gdprExpanded = !gdprExpanded })
                }
                DropdownMenu(
                    expanded = gdprExpanded,
                    onDismissRequest = { gdprExpanded = false },
                ) {
                    viewModel.privacyList.forEach { label ->
                        DropdownMenuItem(text = {
                            Text(text = label)
                        }, onClick = {
                            viewModel.updateGDPRSelected(label)
                            gdprExpanded = false
                        })
                    }
                }
            }
        }
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start,
        ) {
            Text(
                modifier = Modifier
                    .align(Alignment.CenterVertically)
                    .padding(horizontal = 20.dp, vertical = 10.dp),
                text = stringResource(id = R.string.user_consent),
                color = Color.Black,
                textAlign = TextAlign.Start,
                fontSize = 16.sp,
            )

            Column {
                Row {
                    Text(consentSelected, modifier = Modifier.clickable { consentExpanded = !consentExpanded })
                    Icon(imageVector = consentIcon, "", Modifier.clickable { consentExpanded = !consentExpanded })
                }
                DropdownMenu(
                    expanded = consentExpanded,
                    onDismissRequest = { consentExpanded = false },
                ) {
                    viewModel.privacyList.forEach { label ->
                        DropdownMenuItem(text = {
                            Text(text = label)
                        }, onClick = {
                            viewModel.updateConsentSelected(label)
                            consentExpanded = false
                        })
                    }
                }
            }
        }
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start,
        ) {
            Text(
                modifier = Modifier
                    .align(Alignment.CenterVertically)
                    .padding(horizontal = 20.dp, vertical = 10.dp),
                text = stringResource(id = R.string.below_consent_age),
                color = Color.Black,
                textAlign = TextAlign.Start,
                fontSize = 16.sp,
            )

            Column {
                Row {
                    Text(belowAgeSelected, modifier = Modifier.clickable { belowAgeExpanded = !belowAgeExpanded })
                    Icon(imageVector = belowAgeIcon, "", Modifier.clickable { belowAgeExpanded = !belowAgeExpanded })
                }
                DropdownMenu(
                    expanded = belowAgeExpanded,
                    onDismissRequest = { belowAgeExpanded = false },
                ) {
                    viewModel.privacyList.forEach { label ->
                        DropdownMenuItem(text = {
                            Text(text = label)
                        }, onClick = {
                            viewModel.updateBelowAgeSelected(label)
                            belowAgeExpanded = false
                        })
                    }
                }
            }
        }
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start,
        ) {
            Text(
                modifier = Modifier
                    .align(Alignment.CenterVertically)
                    .padding(horizontal = 20.dp, vertical = 10.dp),
                text = stringResource(id = R.string.usage_stats_consent),
                color = Color.Black,
                textAlign = TextAlign.Start,
                fontSize = 16.sp,
            )

            Column {
                Row {
                    Text(usageStatsSelected, modifier = Modifier.clickable { usageStatsExpanded = !usageStatsExpanded })
                    Icon(imageVector = usageStatsIcon, "", Modifier.clickable { usageStatsExpanded = !usageStatsExpanded })
                }
                DropdownMenu(
                    expanded = usageStatsExpanded,
                    onDismissRequest = { usageStatsExpanded = false },
                ) {
                    viewModel.privacyList.forEach { label ->
                        DropdownMenuItem(text = {
                            Text(text = label)
                        }, onClick = {
                            viewModel.updateUsageStatsConsentSelected(label)
                            usageStatsExpanded = false
                        })
                    }
                }
            }
        }
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Text(
                modifier = Modifier
                    .width(120.dp)
                    .padding(start = 20.dp, end = 20.dp),
                text = stringResource(id = R.string.us_privacy),
                color = Color.Black,
                textAlign = TextAlign.Start,
                fontSize = 16.sp,
            )
            TextField(
                value = userPrivacyInput,
                singleLine = true,
                modifier = Modifier
                    .weight(1f)
                    .padding(vertical = 10.dp),
                onValueChange = {
                    viewModel.updateUSPrivacyInput(it)
                },
            )
            Button(
                modifier = Modifier
                    .padding(start = 20.dp, end = 20.dp, top = 10.dp, bottom = 10.dp),
                onClick = {
                    viewModel.setUSPrivacy()
                },
            ) {
                Text(text = stringResource(id = R.string.set), fontSize = 16.sp)
            }
        }
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start,
        ) {
            Text(text = stringResource(id = R.string.opt_out_advertising_id), modifier = Modifier.padding(10.dp))
            Switch(
                colors = SwitchDefaults.colors(checkedTrackColor = Yellow700),
                checked = optOutOfAdvertisingId,
                onCheckedChange = {
                    viewModel.updateOptOutAdvertisingId(context, it)
                },
            )
        }
        Spacer(modifier = Modifier.weight(1f))
    }
}
