package com.example.livingai.pages.home

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
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.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.navigation.NavController
import com.example.livingai.R
import com.example.livingai.pages.commons.Dimentions
import com.example.livingai.pages.components.CommonScaffold
import com.example.livingai.pages.components.LabeledDropdown
import com.example.livingai.pages.navigation.Route
import com.example.livingai.utils.Constants

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen(navController: NavController) {
    val context = LocalContext.current
    val silhouetteMap = remember {
        Constants.silhouetteList.associateWith { item ->
            val resId = context.resources.getIdentifier("label_${item}", "string", context.packageName)
            if (resId != 0) context.getString(resId) else item
        }
    }
    
    // Reverse map for lookup (Display Name -> ID)
    val displayToIdMap = remember { silhouetteMap.entries.associate { (k, v) -> v to k } }
    val orientationOptions = remember { silhouetteMap.values.toList() }
    
    var selectedOrientationDisplay by remember { mutableStateOf(orientationOptions.firstOrNull() ?: "") }

    CommonScaffold(
        navController = navController,
        title = stringResource(id = R.string.app_name),
        showFab = false,
        onFabClick = { }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
                .padding(Dimentions.SMALL_PADDING_TEXT),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            Image(
                painter = painterResource(id = R.drawable.ic_launcher_foreground),
                contentDescription = null,
                modifier = Modifier.size(Dimentions.BIG_ICON_SIZE)
            )
            Text(
                text = stringResource(id = R.string.app_name),
                style = MaterialTheme.typography.headlineLarge,
                fontWeight = FontWeight.Bold
            )
            Spacer(modifier = Modifier.height(Dimentions.MEDIUM_PADDING))
            
            HomeButton(
                text = stringResource(id = R.string.top_bar_listings),
                onClick = { navController.navigate(Route.ListingsScreen) }
            )

            HomeButton(
                text = stringResource(id = R.string.top_bar_add_profile),
                onClick = { navController.navigate(Route.AddProfileScreen()) }
            )
            
//            Spacer(modifier = Modifier.height(Dimentions.SMALL_PADDING))
//
//            // Dropdown for selecting orientation
//            LabeledDropdown(
//                labelRes = R.string.default_orientation_label, // Or create a generic "Orientation" label
//                options = orientationOptions,
//                selected = selectedOrientationDisplay,
//                onSelected = { selectedOrientationDisplay = it },
//                modifier = Modifier.fillMaxWidth()
//            )
//
//            HomeButton(
//                text = "Camera Capture",
//                onClick = {
//                    val orientationId = displayToIdMap[selectedOrientationDisplay] ?: "side"
//                    navController.navigate(Route.CameraScreen(orientation = orientationId, animalId = "home_test"))
//                }
//            )

        }
    }
}

@Composable
fun HomeButton(
    text: String,
    onClick: () -> Unit
) {
    Button(
        onClick = onClick,
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = Dimentions.SMALL_PADDING_TEXT)
    ) {
        Text(text = text)
    }
}
