package com.za.base.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
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.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import coil.compose.AsyncImage
import com.za.base.theme.black90
import com.za.common.util.MapUtil
import com.za.servicing.R

@Composable
fun CommonDialog(title : String? = null,
	confirmText : String = "确定",
	confirm : () -> Unit,
	content : @Composable () -> Unit = {},
	message : String? = null,
	cancelText : String? = "取消",
	cancelEnable : Boolean = true,
	cancel : () -> Unit = {},
	dismiss : () -> Unit) {
	Dialog(onDismissRequest = { dismiss() },
		properties = DialogProperties(dismissOnBackPress = cancelEnable,
			dismissOnClickOutside = cancelEnable)) {
		Box(modifier = Modifier.background(color = Color.White,
			shape = RoundedCornerShape(13.dp))) {
			Spacer(modifier = Modifier
				.fillMaxWidth()
				.height(105.dp)
				.background(brush = Brush.verticalGradient(colors = arrayListOf(Color(0xFFDAE8FF),
					Color(0x00DAE8FF))),
					shape = RoundedCornerShape(topStart = 13.dp, topEnd = 13.dp)))
			Column(modifier = Modifier
				.fillMaxWidth()
				.padding(horizontal = 30.dp),
				horizontalAlignment = Alignment.CenterHorizontally,
				verticalArrangement = Arrangement.Center) {
				Spacer(modifier = Modifier.height(35.dp))
				Text(text = "$title",
					fontSize = 16.sp,
					fontWeight = FontWeight.Medium,
					color = Color(0xFF2A4054))
				Spacer(modifier = Modifier.height(16.dp))
				if (message == null) {
					content()
				} else {
					Text(text = message, fontSize = 14.sp, color = Color(0xFF536475))
				}

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

				Box(modifier = Modifier
					.width(202.dp)
					.clickable { confirm() }
					.background(color = Color(0xFF3A58B1), shape = RoundedCornerShape(23.dp))
					.padding(vertical = 12.dp), contentAlignment = Alignment.Center) {
					Text(text = confirmText,
						color = Color.White,
						fontSize = 15.sp,
						fontWeight = FontWeight.Medium)
				}

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

				if (! cancelText.isNullOrBlank() && cancelEnable) {
					Box(modifier = Modifier
						.fillMaxWidth()
						.clickable { cancel() },
						contentAlignment = Alignment.Center) {
						Text(text = cancelText,
							fontSize = 15.sp,
							fontWeight = FontWeight.Medium,
							color = black90)
					}
				}

				Spacer(modifier = Modifier.height(10.dp))
			}
		}
	}
}


@Composable
fun ReTakePhotoDialog(title : String? = null,
	confirm : () -> Unit,
	againSubmit : () -> Unit,
	path : String? = null,
	showAgain : Boolean = false,
	cancel : () -> Unit = {},
	dismiss : () -> Unit) {
	Dialog(onDismissRequest = { dismiss() },
		properties = DialogProperties(dismissOnBackPress = true, dismissOnClickOutside = true)) {
		Box(modifier = Modifier.background(color = Color.White,
			shape = RoundedCornerShape(13.dp))) {
			Spacer(modifier = Modifier
				.fillMaxWidth()
				.height(105.dp)
				.background(brush = Brush.verticalGradient(colors = arrayListOf(Color(0xFFDAE8FF),
					Color(0x00DAE8FF))),
					shape = RoundedCornerShape(topStart = 13.dp, topEnd = 13.dp)))
			Column(modifier = Modifier
				.fillMaxWidth()
				.padding(horizontal = 30.dp),
				horizontalAlignment = Alignment.CenterHorizontally,
				verticalArrangement = Arrangement.Center) {
				Spacer(modifier = Modifier.height(35.dp))
				Text(text = "$title",
					fontSize = 16.sp,
					fontWeight = FontWeight.Medium,
					color = Color(0xFF2A4054))
				Spacer(modifier = Modifier.height(16.dp))
				Box(modifier = Modifier
					.fillMaxWidth()
					.height(300.dp),
					contentAlignment = Alignment.Center) {
					AsyncImage(model = path,
						contentDescription = "",
						modifier = Modifier.fillMaxSize(),
						contentScale = ContentScale.FillWidth)
				}
				Spacer(modifier = Modifier.height(24.dp))

				Box(modifier = Modifier
					.width(202.dp)
					.clickable { confirm() }
					.background(color = Color(0xFF3A58B1), shape = RoundedCornerShape(23.dp))
					.padding(vertical = 12.dp), contentAlignment = Alignment.Center) {
					Text(text = "重拍",
						color = Color.White,
						fontSize = 15.sp,
						fontWeight = FontWeight.Medium)
				}

				if (showAgain) {
					Spacer(modifier = Modifier.height(20.dp))

					Box(modifier = Modifier
						.width(202.dp)
						.clickable { againSubmit() }
						.background(color = Color.Red, shape = RoundedCornerShape(23.dp))
						.padding(vertical = 12.dp), contentAlignment = Alignment.Center) {
						Text(text = "再次上传",
							color = Color.White,
							fontSize = 15.sp,
							fontWeight = FontWeight.Medium)
					}
				}

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

				Box(modifier = Modifier
					.fillMaxWidth()
					.clickable { cancel() },
					contentAlignment = Alignment.Center) {
					Text(text = "取消",
						fontSize = 15.sp,
						fontWeight = FontWeight.Medium,
						color = black90)
				}

				Spacer(modifier = Modifier.height(10.dp))
			}
		}
	}
}


@Composable
fun ChoiceMapDialog(dismiss : () -> Unit, lat : Double?, lng : Double?, address : String?) {
	val context = LocalContext.current
	Dialog(onDismissRequest = { dismiss() }) {
		Row(modifier = Modifier
			.fillMaxWidth()
			.height(180.dp)
			.background(color = Color.White, shape = RoundedCornerShape(8.dp))
			.padding(10.dp),
			verticalAlignment = Alignment.CenterVertically,
			horizontalArrangement = Arrangement.SpaceAround) {
			if (MapUtil.isGdMapInstalled(context)) {
				Column(modifier = Modifier.clickable {
					MapUtil.startNavigationGd(context, lat = lat, lng = lng, address = address)
					dismiss()
				}) {
					AsyncImage(model = R.drawable.sv_amap_icon,
						contentDescription = "",
						modifier = Modifier.size(60.dp))
					Spacer(modifier = Modifier.height(5.dp))
					Text(text = "高德地图",
						color = Color.Black,
						fontWeight = FontWeight.Medium,
						fontSize = 14.sp)
				}
			}

			if (MapUtil.isBaiduMapInstalled(context)) {
				Column(modifier = Modifier.clickable {
					MapUtil.startNavigationBd(context, lat = lat, lng = lng, address = address)
					dismiss()
				}) {
					AsyncImage(model = R.drawable.sv_baidu_icon,
						contentDescription = "",
						modifier = Modifier.size(60.dp))
					Spacer(modifier = Modifier.height(5.dp))
					Text(text = "百度地图",
						color = Color.Black,
						fontWeight = FontWeight.Medium,
						fontSize = 14.sp)
				}
			}

			if (MapUtil.isTencentInstalled(context)) {
				Column(modifier = Modifier.clickable {
					MapUtil.startNavigationTencent(context, lat = lat, lng = lng, address = address)
					dismiss()
				}) {
					AsyncImage(model = R.drawable.sv_tencent_icon,
						contentDescription = "",
						modifier = Modifier.size(60.dp))
					Spacer(modifier = Modifier.height(5.dp))
					Text(text = "腾讯地图",
						color = Color.Black,
						fontWeight = FontWeight.Medium,
						fontSize = 14.sp)
				}
			}
		}
	}
}
