package com.example.myapplication.database.entities.composeui.edit

import android.content.res.Configuration
import android.graphics.BitmapFactory
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.example.myapplication.R
import com.example.myapplication.api.ApiStatus
import com.example.myapplication.database.AppViewModelProvider
import com.example.myapplication.setting.ImageUploader
import com.example.myapplication.ui.theme.PmudemoTheme
import com.example.myapplication.ui.view.ErrorView
import com.example.myapplication.ui.view.LoadingView
import kotlinx.coroutines.launch

@Composable
fun BikeEdit(
    navController: NavController,
    viewModel: BikeEditViewModel = viewModel(factory = AppViewModelProvider.Factory),
) {
    val coroutineScope = rememberCoroutineScope()
/*    when (viewModel.apiStatus) {
        ApiStatus.DONE -> {*/
    BikeEdit(
        bikeUiState = viewModel.bikeUiState,
        onClick = {
            coroutineScope.launch {
                viewModel.saveBike()
                navController.popBackStack()
            }
        },
        onUpdate = viewModel::updateUiState,
    )
/*        }
        ApiStatus.LOADING -> LoadingView()
        else -> ErrorView(
            message = viewModel.apiError,
            onBack = { navController.popBackStack() }
        )
    }*/
}

@Composable
private fun BikeEdit(
    bikeUiState: BikeUiState,
    onClick: () -> Unit,
    onUpdate: (BikeDetails) -> Unit,
) {
    Column(
        Modifier
            .fillMaxWidth()
            .padding(all = 10.dp)
    ) {
        OutlinedTextField(
            modifier = Modifier.fillMaxWidth(),
            value = bikeUiState.bikeDetails.name,
            onValueChange = { onUpdate(bikeUiState.bikeDetails.copy(name = it)) },
            label = { Text(stringResource(id = R.string.Bike_name)) },
            singleLine = true,
            textStyle = MaterialTheme.typography.bodyLarge.copy(
                color = MaterialTheme.colorScheme.onSecondary
            )

        )
        OutlinedTextField(
            modifier = Modifier.fillMaxWidth(),
            value = bikeUiState.bikeDetails.description,
            onValueChange = { onUpdate(bikeUiState.bikeDetails.copy(description = it)) },
            label = { Text(stringResource(id = R.string.Bike_description)) },
            singleLine = true,
            textStyle = MaterialTheme.typography.bodyLarge.copy(
                color = MaterialTheme.colorScheme.onSecondary
            )
        )
        if (bikeUiState.bikeDetails.image != null)
            ImageUploader(
                bitmap = BitmapFactory.decodeByteArray(
                    bikeUiState.bikeDetails.image,
                    0,
                    bikeUiState.bikeDetails.image.size
                ),
                onResult = { byteArray: ByteArray? ->
                    onUpdate(
                        bikeUiState.bikeDetails.copy(
                            image = byteArray
                        )
                    )
                }
            )
        Button(
            onClick = onClick,
            enabled = bikeUiState.isEntryValid,
            shape = MaterialTheme.shapes.small,
            modifier = Modifier.fillMaxWidth()
        ) {
            Text(text = stringResource(R.string.Save_button))
        }
    }
}


@Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun BikeEditPreview() {
    PmudemoTheme {
        Surface(
            color = MaterialTheme.colorScheme.background
        ) {
            BikeEdit(
                bikeUiState = BikeUiState(),
                onClick = {},
                onUpdate = {},
            )
        }
    }
}
