package com.example.uitugasakhir.pengaturan

import android.widget.Toast
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.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import com.example.uitugasakhir.ViewModel.SensorViewModel
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.app.ui.theme.Inria_Regular
import com.example.uitugasakhir.R
import com.example.uitugasakhir.ViewModel.MainViewModel


@Composable
fun NotifikasiCardSection(
    viewModel: MainViewModel,
    sensorViewModel: SensorViewModel
) {

    val notifTanah = sensorViewModel.notifTanahAktif
    val notifHujan = sensorViewModel.notifHujanAktif
    val buzzerAktif = sensorViewModel.buzzerAktif

    val context = LocalContext.current

    Text(
        text = stringResource(R.string.notification),
        fontSize = 15.sp,
        fontFamily = Inria_Regular,
        fontWeight = FontWeight.Medium,
        color = Color.Gray,
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 28.dp)
    )

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

    Card(
        modifier = Modifier
            .padding(horizontal = 25.dp)
            .size(width = 333.dp, height = 140.dp),
        shape = RoundedCornerShape(24.dp),
        colors = CardDefaults.cardColors(
            containerColor = Color(0xFFEDEDED)
        )
    ) {
        Column(modifier = Modifier.padding(16.dp)
            .verticalScroll(rememberScrollState())) {

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text( text = stringResource(R.string.soil),)
                Switch(
                    checked = notifTanah,
                    onCheckedChange = {

                        sensorViewModel.setNotifTanah(it)

                        Toast.makeText(
                            context,
                            if (it) "Notif tanah aktif"
                            else "Notif tanah dimatikan",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                )
            }

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text( text = stringResource(R.string.rain),)
                Switch(
                    checked = notifHujan,
                    onCheckedChange = {

                        sensorViewModel.setNotifHujan(it)

                        Toast.makeText(
                            context,
                            if (it) "Notif hujan aktif"
                            else "Notif hujan dimatikan",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                )
            }

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text("Buzzer")
                Switch(
                    checked = buzzerAktif,
                    onCheckedChange = {

                        sensorViewModel.setBuzzer(it)

                        Toast.makeText(
                            context,
                            if (it) "Buzzer aktif"
                            else "Buzzer dimatikan",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                )
            }
        }
    }
}