package com.syaroful.agrilinkvocpro.presentation.screen.profile

import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
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.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
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.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.syaroful.agrilinkvocpro.R
import com.syaroful.agrilinkvocpro.core.components.textTheme
import com.syaroful.agrilinkvocpro.presentation.theme.LightGrey
import com.syaroful.agrilinkvocpro.presentation.theme.MainGreen
import org.koin.androidx.compose.koinViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ProfileScreen(
    profileViewModel: ProfileViewModel = koinViewModel(),
    onNavigateToLogin: () -> Unit,
) {
    val profileState by profileViewModel.profileState.collectAsState()
    var showLogoutDialog by remember { mutableStateOf(false) }

    LaunchedEffect(profileState) {
        profileViewModel.loadUserProfile()
    }

    Scaffold(
        topBar = {
            TopAppBar(
                title = {
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally,
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("Profile", style = MaterialTheme.typography.titleMedium)
                    }

                },
                navigationIcon = {
                    IconButton(onClick = { }) {
                        Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
                    }
                },
            )
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .padding(innerPadding)
                .padding(horizontal = 20.dp)
        ) {
            Text(text = "Akun", style = textTheme.titleMedium)
            Column(
                modifier = Modifier
                    .padding(vertical = 16.dp)
                    .border(
                        width = 1.dp,
                        color = MainGreen.copy(alpha = 0.1f),
                        shape = RoundedCornerShape(8.dp)
                    )

                    .padding(8.dp)
            ) {
                RowItemWidget(
                    label = "Nama",
                    value = profileState?.name ?: "-"
                )
                RowItemWidget(
                    label = "Username",
                    value = profileState?.username ?: "-"
                )
                RowItemWidget(
                    label = "Email",
                    value = profileState?.email ?: "-"
                )
                RowItemWidget(
                    label = "No. Telepon",
                    value = profileState?.phone ?: "-"
                )
            }
            Text(text = "Informasi", style = textTheme.titleMedium)
            Column(
                modifier = Modifier
                    .padding(vertical = 16.dp)
                    .border(
                        width = 1.dp,
                        color = MainGreen.copy(alpha = 0.1f),
                        shape = RoundedCornerShape(8.dp)
                    )
                    .padding(8.dp)
            ) {
                RowItemWidgetWithIcon(
                    label = "Syarat dan Ketentuan",
                    icon = painterResource(id = R.drawable.ic_doc)
                )
                RowItemWidgetWithIcon(
                    label = "FAQ",
                    icon = painterResource(id = R.drawable.ic_question)
                )
                RowItemWidgetWithIcon(
                    label = "Kebijakan Privasi",
                    icon = painterResource(id = R.drawable.ic_shield)
                )
                RowItemWidgetWithIcon(
                    label = "Log Out",
                    icon = painterResource(id = R.drawable.ic_out)
                ) {
                    showLogoutDialog = true
                }
            }
        }
    }

    if (showLogoutDialog) {
        AlertDialog(
            onDismissRequest = { showLogoutDialog = false },
            title = { Text("Logout") },
            text = { Text("Apakah Anda yakin ingin logout?") },
            confirmButton = {
                TextButton(
                    onClick = {
                        profileViewModel.logout {
                            onNavigateToLogin()
                        }
                        showLogoutDialog = false
                    },
                    colors = ButtonDefaults.textButtonColors(
                        contentColor = Color.Red.copy(alpha = 0.5f)
                    )
                ) {
                    Text("Logout")
                }
            },
            dismissButton = {
                TextButton(
                    onClick = { showLogoutDialog = false },
                    colors = ButtonDefaults.textButtonColors(
                        contentColor = Color.Gray
                    )
                ) { Text("Batal") }
            }
        )
    }
}

@Composable
private fun RowItemWidgetWithIcon(
    label: String,
    icon: Painter,
    onClick: () -> Unit = {}
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable { onClick() }
            .padding(horizontal = 8.dp)
            .padding(vertical = 10.dp)) {
        Image(
            modifier = Modifier.width(24.dp),
            painter = icon,
            contentDescription = label,
        )
        Spacer(modifier = Modifier.width(8.dp))
        Text(
            text = label
        )
    }
}

@Composable
private fun RowItemWidget(
    label: String,
    value: String
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(8.dp),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(text = label)
        Text(text = value, color = LightGrey)
    }
}