package com.tt.honeyDue.ui.components.task

import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import honeydue.composeapp.generated.resources.*
import com.tt.honeyDue.viewmodel.TaskViewModel
import org.jetbrains.compose.resources.stringResource

// MARK: - Edit Task Button
@Composable
fun EditTaskButton(
    taskId: Int,
    onCompletion: () -> Unit,
    onError: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    val editText = stringResource(Res.string.common_edit)

    Button(
        onClick = {
            // Edit navigates to edit screen - handled by parent
            onCompletion()
        },
        modifier = modifier.fillMaxWidth(),
        colors = ButtonDefaults.buttonColors(
            containerColor = MaterialTheme.colorScheme.surfaceVariant,
            contentColor = MaterialTheme.colorScheme.onSurfaceVariant
        )
    ) {
        Icon(
            imageVector = Icons.Default.Edit,
            contentDescription = editText,
            modifier = Modifier.size(18.dp)
        )
        Spacer(modifier = Modifier.width(8.dp))
        Text(editText, style = MaterialTheme.typography.labelLarge)
    }
}

// MARK: - Cancel Task Button
@Composable
fun CancelTaskButton(
    taskId: Int,
    onCompletion: () -> Unit,
    onError: (String) -> Unit,
    modifier: Modifier = Modifier,
    viewModel: TaskViewModel = viewModel { TaskViewModel() }
) {
    val cancelText = stringResource(Res.string.tasks_cancel)
    val errorMessage = stringResource(Res.string.tasks_failed_to_cancel)

    OutlinedButton(
        onClick = {
            viewModel.cancelTask(taskId) { success ->
                if (success) {
                    onCompletion()
                } else {
                    onError(errorMessage)
                }
            }
        },
        modifier = modifier.fillMaxWidth(),
        colors = ButtonDefaults.outlinedButtonColors(
            contentColor = MaterialTheme.colorScheme.error
        )
    ) {
        Icon(
            imageVector = Icons.Default.Cancel,
            contentDescription = cancelText,
            modifier = Modifier.size(18.dp)
        )
        Spacer(modifier = Modifier.width(8.dp))
        Text(cancelText, style = MaterialTheme.typography.labelLarge)
    }
}

// MARK: - Uncancel (Restore) Task Button
@Composable
fun UncancelTaskButton(
    taskId: Int,
    onCompletion: () -> Unit,
    onError: (String) -> Unit,
    modifier: Modifier = Modifier,
    viewModel: TaskViewModel = viewModel { TaskViewModel() }
) {
    val restoreText = stringResource(Res.string.tasks_uncancel)
    val errorMessage = stringResource(Res.string.tasks_failed_to_restore)

    Button(
        onClick = {
            viewModel.uncancelTask(taskId) { success ->
                if (success) {
                    onCompletion()
                } else {
                    onError(errorMessage)
                }
            }
        },
        modifier = modifier.fillMaxWidth(),
        colors = ButtonDefaults.buttonColors(
            containerColor = MaterialTheme.colorScheme.primary
        )
    ) {
        Icon(
            imageVector = Icons.Default.Undo,
            contentDescription = restoreText,
            modifier = Modifier.size(18.dp)
        )
        Spacer(modifier = Modifier.width(8.dp))
        Text(restoreText, style = MaterialTheme.typography.labelLarge)
    }
}

// MARK: - Mark In Progress Button
@Composable
fun MarkInProgressButton(
    taskId: Int,
    onCompletion: () -> Unit,
    onError: (String) -> Unit,
    modifier: Modifier = Modifier,
    viewModel: TaskViewModel = viewModel { TaskViewModel() }
) {
    val inProgressText = stringResource(Res.string.tasks_in_progress_label)
    val errorMessage = stringResource(Res.string.tasks_failed_to_mark_in_progress)

    OutlinedButton(
        onClick = {
            viewModel.markInProgress(taskId) { success ->
                if (success) {
                    onCompletion()
                } else {
                    onError(errorMessage)
                }
            }
        },
        modifier = modifier.fillMaxWidth(),
        colors = ButtonDefaults.outlinedButtonColors(
            contentColor = MaterialTheme.colorScheme.tertiary
        )
    ) {
        Icon(
            imageVector = Icons.Default.PlayCircle,
            contentDescription = inProgressText,
            modifier = Modifier.size(18.dp)
        )
        Spacer(modifier = Modifier.width(8.dp))
        Text(inProgressText, style = MaterialTheme.typography.labelLarge)
    }
}

// MARK: - Complete Task Button
@Composable
fun CompleteTaskButton(
    taskId: Int,
    onCompletion: () -> Unit,
    onError: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    val completeText = stringResource(Res.string.tasks_mark_complete)

    Button(
        onClick = {
            // Complete shows dialog - handled by parent
            onCompletion()
        },
        modifier = modifier.fillMaxWidth(),
        colors = ButtonDefaults.buttonColors(
            containerColor = MaterialTheme.colorScheme.primary
        )
    ) {
        Icon(
            imageVector = Icons.Default.CheckCircle,
            contentDescription = completeText,
            modifier = Modifier.size(18.dp)
        )
        Spacer(modifier = Modifier.width(8.dp))
        Text(completeText, style = MaterialTheme.typography.labelLarge)
    }
}

// MARK: - Archive Task Button
@Composable
fun ArchiveTaskButton(
    taskId: Int,
    onCompletion: () -> Unit,
    onError: (String) -> Unit,
    modifier: Modifier = Modifier,
    viewModel: TaskViewModel = viewModel { TaskViewModel() }
) {
    val archiveText = stringResource(Res.string.tasks_archive)
    val errorMessage = stringResource(Res.string.tasks_failed_to_archive)

    OutlinedButton(
        onClick = {
            viewModel.archiveTask(taskId) { success ->
                if (success) {
                    onCompletion()
                } else {
                    onError(errorMessage)
                }
            }
        },
        modifier = modifier.fillMaxWidth(),
        colors = ButtonDefaults.outlinedButtonColors(
            contentColor = MaterialTheme.colorScheme.outline
        )
    ) {
        Icon(
            imageVector = Icons.Default.Archive,
            contentDescription = archiveText,
            modifier = Modifier.size(18.dp)
        )
        Spacer(modifier = Modifier.width(8.dp))
        Text(archiveText, style = MaterialTheme.typography.labelLarge)
    }
}

// MARK: - Unarchive Task Button
@Composable
fun UnarchiveTaskButton(
    taskId: Int,
    onCompletion: () -> Unit,
    onError: (String) -> Unit,
    modifier: Modifier = Modifier,
    viewModel: TaskViewModel = viewModel { TaskViewModel() }
) {
    val unarchiveText = stringResource(Res.string.tasks_unarchive)
    val errorMessage = stringResource(Res.string.tasks_failed_to_unarchive)

    Button(
        onClick = {
            viewModel.unarchiveTask(taskId) { success ->
                if (success) {
                    onCompletion()
                } else {
                    onError(errorMessage)
                }
            }
        },
        modifier = modifier.fillMaxWidth(),
        colors = ButtonDefaults.buttonColors(
            containerColor = MaterialTheme.colorScheme.surfaceVariant,
            contentColor = MaterialTheme.colorScheme.onSurfaceVariant
        )
    ) {
        Icon(
            imageVector = Icons.Default.Unarchive,
            contentDescription = unarchiveText,
            modifier = Modifier.size(18.dp)
        )
        Spacer(modifier = Modifier.width(8.dp))
        Text(unarchiveText, style = MaterialTheme.typography.labelLarge)
    }
}
