package com.aryan.reader

import android.content.Context
import android.view.Window
import android.view.WindowManager
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.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Slider
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.core.content.edit
import kotlin.math.roundToInt

private const val READER_PREFS_NAME = "reader_prefs"
private const val PREF_READER_BRIGHTNESS_USE_SYSTEM = "reader_brightness_use_system"
private const val PREF_READER_BRIGHTNESS_VALUE = "reader_brightness_value"
private const val DEFAULT_CUSTOM_BRIGHTNESS = 0.75f
private const val MIN_CUSTOM_BRIGHTNESS = 0.05f

data class ReaderBrightnessSettings(
    val useSystemBrightness: Boolean = true,
    val customBrightness: Float = DEFAULT_CUSTOM_BRIGHTNESS
) {
    val safeCustomBrightness: Float
        get() = customBrightness.coerceIn(MIN_CUSTOM_BRIGHTNESS, 1f)
}

fun loadReaderBrightnessSettings(context: Context): ReaderBrightnessSettings {
    val prefs = context.getSharedPreferences(READER_PREFS_NAME, Context.MODE_PRIVATE)
    return ReaderBrightnessSettings(
        useSystemBrightness = prefs.getBoolean(PREF_READER_BRIGHTNESS_USE_SYSTEM, true),
        customBrightness = prefs.getFloat(PREF_READER_BRIGHTNESS_VALUE, DEFAULT_CUSTOM_BRIGHTNESS)
            .coerceIn(MIN_CUSTOM_BRIGHTNESS, 1f)
    )
}

fun saveReaderBrightnessSettings(context: Context, settings: ReaderBrightnessSettings) {
    context.getSharedPreferences(READER_PREFS_NAME, Context.MODE_PRIVATE).edit {
        putBoolean(PREF_READER_BRIGHTNESS_USE_SYSTEM, settings.useSystemBrightness)
        putFloat(PREF_READER_BRIGHTNESS_VALUE, settings.safeCustomBrightness)
    }
}

@Composable
fun ReaderBrightnessEffect(
    window: Window?,
    settings: ReaderBrightnessSettings
) {
    DisposableEffect(window) {
        val originalBrightness = window?.attributes?.screenBrightness
            ?: WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
        onDispose {
            window?.setReaderBrightness(originalBrightness)
        }
    }

    LaunchedEffect(window, settings) {
        val brightness = if (settings.useSystemBrightness) {
            WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
        } else {
            settings.safeCustomBrightness
        }
        window?.setReaderBrightness(brightness)
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ReaderBrightnessSheet(
    settings: ReaderBrightnessSettings,
    onSettingsChange: (ReaderBrightnessSettings) -> Unit,
    onDismiss: () -> Unit
) {
    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
    ModalBottomSheet(
        onDismissRequest = onDismiss,
        sheetState = sheetState
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 24.dp, vertical = 8.dp)
                .padding(bottom = 32.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = stringResource(R.string.reader_brightness_title),
                    style = MaterialTheme.typography.titleLarge,
                    fontWeight = FontWeight.Bold
                )
                TextButton(onClick = onDismiss) {
                    Text(stringResource(R.string.action_done))
                }
            }

            HorizontalDivider()

            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Column(modifier = Modifier.weight(1f)) {
                    Text(
                        text = stringResource(R.string.reader_brightness_system),
                        style = MaterialTheme.typography.titleMedium
                    )
                    Text(
                        text = stringResource(R.string.reader_brightness_system_desc),
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.onSurfaceVariant
                    )
                }
                Switch(
                    checked = settings.useSystemBrightness,
                    onCheckedChange = { useSystem ->
                        onSettingsChange(settings.copy(useSystemBrightness = useSystem))
                    }
                )
            }

            Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(
                        text = stringResource(R.string.reader_brightness_custom),
                        style = MaterialTheme.typography.titleMedium
                    )
                    Text(
                        text = stringResource(
                            R.string.reader_brightness_percent,
                            (settings.safeCustomBrightness * 100f).roundToInt()
                        ),
                        style = MaterialTheme.typography.labelLarge,
                        color = MaterialTheme.colorScheme.primary
                    )
                }
                Slider(
                    value = settings.safeCustomBrightness,
                    onValueChange = { brightness ->
                        onSettingsChange(
                            settings.copy(
                                useSystemBrightness = false,
                                customBrightness = brightness.coerceIn(MIN_CUSTOM_BRIGHTNESS, 1f)
                            )
                        )
                    },
                    valueRange = MIN_CUSTOM_BRIGHTNESS..1f
                )
                Text(
                    text = stringResource(R.string.reader_brightness_custom_desc),
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }

            Spacer(Modifier.height(4.dp))
        }
    }
}

private fun Window.setReaderBrightness(brightness: Float) {
    attributes = attributes.apply {
        screenBrightness = brightness
    }
}
