package com.tapjoy.easyapp

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.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
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.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.tapjoy.easyapp.extensions.findActivity
import com.tapjoy.easyapp.theme.DisabledColor
import com.tapjoy.easyapp.theme.Yellow700
import com.unity3d.ads.UnityAds

@Composable
fun UnityAdsScreen(viewModel: UnityAdsViewModel = UnityAdsViewModel()) {
    val context = LocalContext.current
    val eventMessage by viewModel.statusMessage.collectAsState()
    val isConnected by viewModel.isConnected.collectAsState()
    val isLoaded by viewModel.isLoaded.collectAsState()

    val preferences = context.getSharedPreferences(PREFERENCES, 0)
    var gameId by remember { mutableStateOf(preferences.getString(PREF_GAME_ID_KEY, DEFAULT_GAME_ID) ?: DEFAULT_GAME_ID) }
    var adUnitId by remember { mutableStateOf(preferences.getString(PREF_AD_UNIT_ID_KEY, DEFAULT_AD_UNIT_ID) ?: DEFAULT_AD_UNIT_ID) }
    var isTestMode by remember { mutableStateOf(preferences.getBoolean(PREF_TEST_MODE_KEY, DEFAULT_TEST_MODE)) }
    var isDebugMode by remember { mutableStateOf(preferences.getBoolean(PREF_DEBUG_MODE_KEY, UnityAds.debugMode)) }

    LaunchedEffect(Unit) {
        UnityAds.debugMode = isDebugMode
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .wrapContentSize(Alignment.TopCenter)
            .verticalScroll(rememberScrollState()),
    ) {
        Spacer(modifier = Modifier.size(10.dp))
        Text(
            text = eventMessage,
            modifier = Modifier
                .align(Alignment.CenterHorizontally)
                .padding(10.dp),
            textAlign = TextAlign.Center,
        )
        if (!isConnected) {
            Text("Game ID", modifier = Modifier.padding(10.dp), fontSize = 16.sp)
            TextField(gameId, { text ->
                gameId = text
                preferences.edit().putString(PREF_GAME_ID_KEY, text).apply()
            }, modifier = Modifier.padding(horizontal = 20.dp).fillMaxWidth(), trailingIcon = {
                Icon(
                    Icons.Default.Clear,
                    contentDescription = stringResource(id = R.string.clear),
                    modifier = Modifier
                        .clickable {
                            gameId = ""
                            preferences.edit().putString(PREF_GAME_ID_KEY, "").apply()
                        },
                )
            })
            Button(
                colors = ButtonDefaults.buttonColors(containerColor = Yellow700),
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(20.dp)
                    .align(Alignment.CenterHorizontally),
                onClick = {
                    viewModel.connect(context, gameId, isTestMode)
                },
            ) {
                Text(text = stringResource(id = R.string.connect_sdk), fontSize = 16.sp)
            }
        }
        Text("Ad Unit ID", modifier = Modifier.padding(10.dp), fontSize = 16.sp)
        TextField(adUnitId, { text ->
            adUnitId = text
            preferences.edit().putString(PREF_AD_UNIT_ID_KEY, text).apply()
        }, modifier = Modifier.padding(horizontal = 20.dp).fillMaxWidth(), trailingIcon = {
            Icon(
                Icons.Default.Clear,
                contentDescription = stringResource(id = R.string.clear),
                modifier = Modifier
                    .clickable {
                        adUnitId = ""
                        preferences.edit().putString(PREF_AD_UNIT_ID_KEY, "").apply()
                    },
            )
        })
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.Top,
            horizontalArrangement = Arrangement.SpaceEvenly,
        ) {
            Button(
                colors = ButtonDefaults.buttonColors(containerColor = if (isConnected) Yellow700 else DisabledColor),
                modifier = Modifier
                    .weight(1f)
                    .padding(start = 20.dp, top = 20.dp, end = 10.dp),
                onClick = {
                    viewModel.load(adUnitId)
                },
            ) {
                Text(text = stringResource(id = R.string.request), fontSize = 16.sp)
            }
            Button(
                colors = ButtonDefaults.buttonColors(containerColor = if (isLoaded) Yellow700 else DisabledColor),
                modifier = Modifier
                    .weight(1f)
                    .padding(start = 10.dp, top = 20.dp, end = 20.dp),
                onClick = {
                    viewModel.show(context.findActivity() ?: return@Button, adUnitId)
                },
            ) {
                Text(text = stringResource(id = R.string.show), fontSize = 16.sp)
            }
        }
        Spacer(modifier = Modifier.size(10.dp))
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start,
        ) {
            Text("Test Mode", modifier = Modifier.padding(10.dp))
            Switch(
                colors = SwitchDefaults.colors(checkedTrackColor = if (isConnected) DisabledColor else Yellow700),
                checked = isTestMode,
                onCheckedChange = {
                    if (!isConnected) {
                        isTestMode = it
                        preferences.edit().putBoolean(PREF_TEST_MODE_KEY, it).apply()
                    }
                },
            )
        }
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start,
        ) {
            Text("Debug Mode", modifier = Modifier.padding(10.dp))
            Switch(
                colors = SwitchDefaults.colors(checkedTrackColor = Yellow700),
                checked = isDebugMode,
                onCheckedChange = {
                    UnityAds.debugMode = it
                    isDebugMode = it
                    preferences.edit().putBoolean(PREF_DEBUG_MODE_KEY, it).apply()
                },
            )
        }

        Spacer(modifier = Modifier.size(20.dp))
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.Bottom,
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Text(
                text = viewModel.getSDKVersion(),
                color = Color.Black,
                modifier = Modifier
                    .padding(10.dp),
                textAlign = TextAlign.Center,
                fontSize = 16.sp,
            )
        }
    }
}
