package com.nothing.camera2magic.view

import android.app.Application
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape

import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.nothing.camera2magic.viewmodel.SettingsViewModel
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import com.nothing.camera2magic.R
import com.nothing.camera2magic.viewmodel.LocalViewModelFactory

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun SettingsView() {
    val factory = LocalViewModelFactory.current
    val viewModel: SettingsViewModel = viewModel(factory = factory)
    val uiState by viewModel.uiState.collectAsState()

    FlowRow(
        modifier = Modifier.fillMaxWidth(),
        maxItemsInEachRow = 2,
        horizontalArrangement = Arrangement.spacedBy(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        // "Play sound" button
        SettingsToggleButton(
            modifier = Modifier.weight(1f),
            text = stringResource(R.string.play_sound_button_name),
            icon = ImageVector.vectorResource(R.drawable.volume_up_24px),
            isChecked = uiState.playSound,
            onClick = { viewModel.onPlaySoundToggled() }
        )

        // "Enable Log" button
        SettingsToggleButton(
            modifier = Modifier.weight(1f),
            text = stringResource(R.string.enable_log_button_name),
            icon = ImageVector.vectorResource(R.drawable.breaking_news_24px),
            isChecked = uiState.enableLog,
            onClick = { viewModel.onEnableLogToggled() }
        )

        // "HAL Mode" button (system-wide camera hook)
        SettingsToggleButton(
            modifier = Modifier.weight(1f),
            text = stringResource(R.string.hal_mode_button_name),
            icon = ImageVector.vectorResource(R.drawable.developer_board_24px),
            isChecked = uiState.halModeEnabled,
            onClick = { viewModel.onHalModeToggled() }
        )

        // "Inject Control" buttons
        /*
        SettingsToggleButton(
            modifier = Modifier.weight(1f),
            text = stringResource(R.string.inject_control_button_name),
            icon = ImageVector.vectorResource(R.drawable.control_camera_24px),
            isChecked = uiState.injectMenu,
            onClick = { viewModel.onInjectMenuToggled() }
        )
        SettingsToggleButton(
            modifier = Modifier.weight(1f),
            text = stringResource(R.string.manually_rotate_button_name),
            icon = ImageVector.vectorResource(R.drawable.rotate_90_degrees_cw_24px),
            isChecked = uiState.manuallyRotate,
            onClick = { viewModel.onManuallyRotateToggled() }
        )

         */
    }
}

@Composable
private fun SettingsToggleButton(
    modifier: Modifier = Modifier,
    text: String,
    icon: ImageVector,
    isChecked: Boolean,
    onClick: () -> Unit
) {
    val containerColor = if (isChecked) {
        MaterialTheme.colorScheme.primary
    } else {
        MaterialTheme.colorScheme.surfaceContainer
    }

    val contentColor = if (isChecked) {
        MaterialTheme.colorScheme.onPrimary
    } else {
        MaterialTheme.colorScheme.onSurfaceVariant
    }

    Button(
        onClick = onClick,
        modifier = modifier
            .defaultMinSize(minHeight = 58.dp)
            .fillMaxWidth(),
        shape = RoundedCornerShape(12.dp),
        colors = ButtonDefaults.buttonColors(
            containerColor = containerColor,
            contentColor = contentColor
        ),
        elevation = ButtonDefaults.buttonElevation(defaultElevation = 0.dp),
        contentPadding = PaddingValues(horizontal = 12.dp, vertical = 20.dp)
    ) {
        Column(
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Icon(
                imageVector = icon,
                contentDescription = null,
                modifier = Modifier.size(16.dp)
            )
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = text,
                fontWeight = FontWeight.Medium,
                maxLines = 1,
                softWrap = false
            )
        }
    }
}
