// Fadlan Rivaldi
//202310715280
package com.example.tiptime

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.tiptime.ui.theme.TipTimeTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TipTimeTheme {
                Surface(modifier = Modifier.fillMaxSize(), color = Color(0xFFF4F4F4)) {
                    BMICalculatorApp()
                }
            }
        }
    }
}

@Composable
fun BMICalculatorApp() {
    var showSplash by remember { mutableStateOf(true) }

    if (showSplash) {
        SplashScreen(onStartClick = { showSplash = false })
    } else {
        BMIScreen()
    }
}

@Composable
fun SplashScreen(onStartClick: () -> Unit) {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color(0xFF2196F3)),
        contentAlignment = Alignment.Center
    ) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            // kalau logomu putih, ubah background jadi putih biar kelihatan
            Surface(
                color = Color.White,
                shape = RoundedCornerShape(100.dp),
                modifier = Modifier.size(150.dp)
            ) {
                Image(
                    painter = painterResource(id = R.drawable.logo),
                    contentDescription = "Logo",
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(20.dp)
                )
            }

            Spacer(modifier = Modifier.height(16.dp))

            Text(
                text = "BMI Checker",
                fontSize = 34.sp,
                color = Color.White,
                fontWeight = FontWeight.Bold
            )

            Spacer(modifier = Modifier.height(24.dp))

            Button(
                onClick = onStartClick,
                colors = ButtonDefaults.buttonColors(containerColor = Color.White),
                shape = RoundedCornerShape(12.dp)
            ) {
                Text("Get Started", color = Color(0xFF2196F3), fontSize = 18.sp)
            }
        }
    }
}

@Composable
fun BMIScreen() {
    var height by remember { mutableStateOf("") }
    var weight by remember { mutableStateOf("") }
    var bmi by remember { mutableStateOf<Double?>(null) }
    var selectedUnit by remember { mutableStateOf("Metric") }

    val unitOptions = listOf("Metric (kg/cm)", "US (lbs/inch)")

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(20.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "BMI Calculator",
            fontSize = 30.sp,
            fontWeight = FontWeight.Bold,
            color = Color(0xFF2196F3)
        )

        Spacer(Modifier.height(24.dp))

        // Pilihan Metric / US
        Row(
            horizontalArrangement = Arrangement.Center,
            modifier = Modifier.fillMaxWidth()
        ) {
            unitOptions.forEach { option ->
                val isSelected = selectedUnit == option
                Button(
                    onClick = { selectedUnit = option },
                    colors = ButtonDefaults.buttonColors(
                        containerColor = if (isSelected) Color(0xFF2196F3) else Color.LightGray
                    ),
                    shape = RoundedCornerShape(50.dp),
                    modifier = Modifier
                        .padding(horizontal = 6.dp)
                        .height(45.dp)
                ) {
                    Text(
                        option,
                        color = if (isSelected) Color.White else Color.Black,
                        fontSize = 13.sp
                    )
                }
            }
        }

        Spacer(Modifier.height(24.dp))

        OutlinedTextField(
            value = height,
            onValueChange = { if (it.all { c -> c.isDigit() || c == '.' }) height = it },
            label = { Text("Tinggi Badan (${if (selectedUnit.startsWith("Metric")) "cm" else "inch"})") },
            modifier = Modifier.fillMaxWidth()
        )

        Spacer(Modifier.height(12.dp))

        OutlinedTextField(
            value = weight,
            onValueChange = { if (it.all { c -> c.isDigit() || c == '.' }) weight = it },
            label = { Text("Berat Badan (${if (selectedUnit.startsWith("Metric")) "kg" else "lbs"})") },
            modifier = Modifier.fillMaxWidth()
        )

        Spacer(Modifier.height(20.dp))

        Button(
            onClick = {
                val h = height.toDoubleOrNull()
                val w = weight.toDoubleOrNull()
                if (h != null && w != null && h > 0) {
                    bmi = if (selectedUnit.startsWith("Metric")) {
                        w / ((h / 100) * (h / 100))
                    } else {
                        703 * (w / (h * h))
                    }
                }
            },
            modifier = Modifier.fillMaxWidth(),
            colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF2196F3))
        ) {
            Text("Hitung BMI", fontSize = 18.sp, color = Color.White)
        }

        Spacer(Modifier.height(30.dp))

        bmi?.let {
            HasilBMI(bmi = it)
        }
    }
}

@Composable
fun HasilBMI(bmi: Double) {
    val (category, imageRes) = when {
        bmi < 18.5 -> "Underweight" to R.drawable.stickman_kurus
        bmi < 25 -> "Normal" to R.drawable.stickman_normal
        bmi < 30 -> "Overweight" to R.drawable.stickman_overweight
        else -> "Obesity" to R.drawable.stickman_obesitas
    }

    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        Text(
            text = String.format("BMI Anda: %.1f", bmi),
            fontSize = 22.sp,
            fontWeight = FontWeight.Medium,
            textAlign = TextAlign.Center
        )
        Text(
            text = "Kategori: $category",
            fontSize = 20.sp,
            color = Color.Gray,
            textAlign = TextAlign.Center
        )
        Spacer(Modifier.height(16.dp))
        Image(
            painter = painterResource(id = imageRes),
            contentDescription = category,
            modifier = Modifier
                .fillMaxWidth(0.7f)
                .aspectRatio(1f)
        )
    }
}
