package com.example.uitugasakhir


import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.runtime.Composable
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.ui.res.painterResource
import com.example.app.ui.theme.Inria_Regular
import com.example.app.ui.theme.Inter
import androidx.navigation.NavController
import android.widget.Toast
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.*
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource

import com.example.uitugasakhir.pengaturan.AkunCardSection
import com.example.uitugasakhir.pengaturan.BahasaCardSection
import com.example.uitugasakhir.pengaturan.BantuanCardSection
import com.example.uitugasakhir.pengaturan.NotifikasiCardSection
import com.example.uitugasakhir.ViewModel.MainViewModel
import androidx.lifecycle.viewmodel.compose.viewModel

import com.example.uitugasakhir.ViewModel.SensorViewModel



@Composable
fun PengaturanScreen(navController: NavController) {

    val context = LocalContext.current
    val mainViewModel: MainViewModel = viewModel()
    val sensorViewModel: SensorViewModel = viewModel()

    Box(
        modifier = Modifier
            .fillMaxSize()
    ) {

        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(160.dp),
            contentAlignment = Alignment.TopCenter
        ) {

            // Card hijau di atas
            Card(
                modifier = Modifier
                    .fillMaxWidth(),
                shape = RoundedCornerShape(0.dp),
                colors = CardDefaults.cardColors(
                    containerColor = Color(0xFF2F6F4E)
                )
            ) {

                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(top = 22.dp),
                    contentAlignment = Alignment.TopCenter
                ) {

                    Image(
                        painter = painterResource(id = R.drawable.back),
                        contentDescription = "kembali",
                        modifier = Modifier
                            .size(15.dp)
                            .offset(x = -160.dp, y = 1.dp)
                            .clickable {
                                navController.popBackStack()
                            }
                    )

                    Text(
                        text = stringResource(R.string.setting),
                        fontSize = 16.sp,
                        fontFamily = Inter,
                        fontWeight = FontWeight.Bold,
                        color = Color.White
                    )
                }
            }
        }

        Card(
            modifier = Modifier
                .padding(top = 64.dp)
                .verticalScroll(rememberScrollState())
                .fillMaxSize(),
            shape = RoundedCornerShape(topStart = 24.dp, topEnd = 0.dp),
            colors = CardDefaults.cardColors(
                containerColor = Color(0xFFFFFFFF)
            )
        ) {

            Column(
                modifier = Modifier
                    .padding(top = 15.dp)
                    .fillMaxSize(),
                verticalArrangement = Arrangement.Top,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {

                // Tampilan akun
                AkunCardSection(viewModel = mainViewModel)

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

                // Tampilan Bahasa
                BahasaCardSection(
                    viewModel = mainViewModel
                )

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

                // Tampilan Notifikasi
                NotifikasiCardSection(
                    viewModel = mainViewModel,
                    sensorViewModel = sensorViewModel
                )

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

                // Tampilan Bantuan
                BantuanCardSection(
                    viewModel = mainViewModel,
                    navController = navController
                )

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

                Text(
                    text = stringResource(R.string.out),
                    color = Color.Red,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .padding(28.dp)
                        .clickable {
                            Toast.makeText(context, "Berhasil logout", Toast.LENGTH_SHORT).show()

                            // contoh pindah ke login
                            navController.navigate("login") {
                                popUpTo(0)
                            }
                        }
                )
            }
        }
    }
}

