package com.vnidrop.app.ui.components

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Surface
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.path
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.vnidrop.app.ui.state.WindowClass
import com.vnidrop.app.ui.theme.LocalVniDropColors
import org.jetbrains.compose.resources.stringResource
import vnidrop.shared.generated.resources.Res
import vnidrop.shared.generated.resources.button_close

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AdaptiveDrawer(
	windowClass: WindowClass,
	onDismissRequest: () -> Unit,
	content: @Composable () -> Unit,
) {
	if (windowClass == WindowClass.Phone) {
		ModalBottomSheet(
			onDismissRequest = onDismissRequest,
			sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true),
			containerColor = LocalVniDropColors.current.backgroundDialog,
		) {
			ClosableModalContent(onDismissRequest, Modifier.fillMaxWidth().navigationBarsPadding().padding(bottom = 12.dp), content)
		}
	} else {
		Dialog(
			onDismissRequest = onDismissRequest,
			properties = DialogProperties(usePlatformDefaultWidth = false),
		) {
			Surface(
				modifier = Modifier.fillMaxWidth(0.86f).widthIn(max = 560.dp),
				shape = RoundedCornerShape(20.dp),
				color = LocalVniDropColors.current.backgroundDialog,
				shadowElevation = 12.dp,
			) { ClosableModalContent(onDismissRequest, content = content) }
		}
	}
}

@Composable
private fun ClosableModalContent(
	onClose: () -> Unit,
	modifier: Modifier = Modifier,
	content: @Composable () -> Unit,
) {
	Box(modifier) {
		content()
		IconButton(
			onClick = onClose,
			modifier = Modifier.align(androidx.compose.ui.Alignment.TopEnd).padding(8.dp).size(40.dp),
		) {
			Icon(CloseIcon, stringResource(Res.string.button_close), tint = LocalVniDropColors.current.foregroundLight)
		}
	}
}

private val CloseIcon = ImageVector.Builder("Close", 24.dp, 24.dp, 24f, 24f).apply {
	path(fill = SolidColor(Color.Transparent), stroke = SolidColor(Color.Black), strokeLineWidth = 2f, strokeLineCap = StrokeCap.Round) {
		moveTo(6f, 6f); lineTo(18f, 18f)
		moveTo(18f, 6f); lineTo(6f, 18f)
	}
}.build()
