package com.za.ui.servicing.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.shape.RoundedCornerShape
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.AbsoluteAlignment
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
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 coil.compose.AsyncImage
import com.za.base.Const
import com.za.base.theme.black5
import com.za.base.theme.headBgColor
import com.za.base.view.AppTipsView
import com.za.base.view.CommonDialog
import com.za.bean.db.order.OrderInfo
import com.za.common.util.MapUtil
import com.za.ext.callPhone
import com.za.ext.finish
import com.za.servicing.R
import com.za.ui.servicing.in_servicing_setting.OrderRequirementsActivity
import com.za.ui.servicing.order_give_up.OrderGiveUpActivity

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun InServicingHeadView(title : String,
	onBack : () -> Unit = {},
	orderInfo : OrderInfo?,
	isCanBack : Boolean = true) {
	val context = LocalContext.current
	val showBottomSheetDialog = remember { mutableStateOf(false) }
	val showNavigationDialog = remember { mutableStateOf(false) }
	val showCallPhoneDialog = remember { mutableStateOf(false) }
	val showCallServicePhoneDialog = remember { mutableStateOf(false) }

	if (showNavigationDialog.value) {
		StartNavigationView(orderInfo = orderInfo, dismiss = { showNavigationDialog.value = false })
	}

	if (showCallPhoneDialog.value) {
		CommonDialog(cancelText = "取消",
			confirmText = "确定",
			title = "是否联系客户？",
			cancelEnable = true,
			cancel = {
				showCallPhoneDialog.value = false
			},
			dismiss = { showCallPhoneDialog.value = false },
			confirm = {
				showCallPhoneDialog.value = false
				context.callPhone(orderInfo?.customerPhone)
				context.finish()
			})
	}

	if (showCallServicePhoneDialog.value) {
		CommonDialog(cancelText = "取消",
			confirmText = "确定",
			title = "是否联系中道客服？",
			cancelEnable = true,
			cancel = {
				showCallServicePhoneDialog.value = false
			},
			dismiss = { showCallServicePhoneDialog.value = false },
			confirm = {
				showCallServicePhoneDialog.value = false
				context.callPhone(orderInfo?.hotline)
				context.finish()
			})
	}


	if (showBottomSheetDialog.value) {
		ModalBottomSheet(containerColor = Color.White,
			onDismissRequest = { showBottomSheetDialog.value = false }) {
			Column(modifier = Modifier
				.fillMaxWidth()
				.padding(10.dp)) {

				if (! orderInfo?.customerReportImgs.isNullOrBlank()) {
					Box(modifier = Modifier
						.fillMaxWidth()
						.clickable {
							showBottomSheetDialog.value = false
							OrderRequirementsActivity.goOrderRequirementsActivity(context,
								orderInfo,
								type = Const.InServiceSettingType.ON_SITE_PHOTO)
						}, contentAlignment = Alignment.Center) {
						Text(text = "现场照片",
							fontSize = 15.sp,
							fontWeight = FontWeight.Medium,
							color = Color.Black)
					}
					HorizontalDivider(modifier = Modifier.padding(vertical = 10.dp), color = black5)
				}

				Box(modifier = Modifier
					.fillMaxWidth()
					.clickable {
						showNavigationDialog.value = true
						showBottomSheetDialog.value = false
					}, contentAlignment = Alignment.Center) {
					Text(text = "开启外部导航",
						fontSize = 15.sp,
						fontWeight = FontWeight.Medium,
						color = Color.Black)
				}
				HorizontalDivider(modifier = Modifier.padding(vertical = 10.dp), color = black5)

				Box(modifier = Modifier
					.fillMaxWidth()
					.clickable {
						showBottomSheetDialog.value = false
						OrderRequirementsActivity.goOrderRequirementsActivity(context,
							orderInfo,
							type = Const.InServiceSettingType.ORDER_REQUIREMENTS)
					}, contentAlignment = Alignment.Center) {
					Text(text = "案件要求",
						fontSize = 15.sp,
						fontWeight = FontWeight.Medium,
						color = Color.Black)
				}
				HorizontalDivider(modifier = Modifier.padding(vertical = 10.dp), color = black5)

				Box(modifier = Modifier
					.fillMaxWidth()
					.clickable {
						showBottomSheetDialog.value = false
						OrderRequirementsActivity.goOrderRequirementsActivity(context,
							orderInfo,
							type = Const.InServiceSettingType.ORDER_DETAIL)
					}, contentAlignment = Alignment.Center) {
					Text(text = "案件详情",
						fontSize = 15.sp,
						fontWeight = FontWeight.Medium,
						color = Color.Black)
				}
				HorizontalDivider(modifier = Modifier.padding(vertical = 10.dp), color = black5)

				Box(modifier = Modifier
					.fillMaxWidth()
					.clickable {
						showBottomSheetDialog.value = false
						OrderGiveUpActivity.goOrderGiveUpActivity(context,
							orderInfo = orderInfo,
							userOrderId = orderInfo?.userOrderId,
							giveUpType = 0)
					}, contentAlignment = Alignment.Center) {
					Text(text = "订单放弃",
						fontSize = 15.sp,
						fontWeight = FontWeight.Medium,
						color = Color.Black)
				}
				HorizontalDivider(modifier = Modifier.padding(vertical = 10.dp), color = black5)

				Box(modifier = Modifier
					.fillMaxWidth()
					.clickable {
						showBottomSheetDialog.value = false
						showCallPhoneDialog.value = true
					}, contentAlignment = Alignment.Center) {
					Text(text = "拨打客户电话",
						fontSize = 15.sp,
						fontWeight = FontWeight.Medium,
						color = Color.Black)
				}

				HorizontalDivider(modifier = Modifier.padding(vertical = 10.dp), color = black5)

				Box(modifier = Modifier
					.fillMaxWidth()
					.clickable {
						showBottomSheetDialog.value = false
						showCallServicePhoneDialog.value = true
					}, contentAlignment = Alignment.Center) {
					Text(text = "联系中道客服",
						fontSize = 15.sp,
						fontWeight = FontWeight.Medium,
						color = Color.Black)
				}

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

			}
		}
	}

	Column {

		CenterAlignedTopAppBar(modifier = Modifier.fillMaxWidth(),
			colors = TopAppBarDefaults.centerAlignedTopAppBarColors()
				.copy(containerColor = headBgColor, titleContentColor = Color.White),
			title = { Text(text = title, fontSize = 15.sp, fontWeight = FontWeight.Medium) },
			navigationIcon = {
				if (isCanBack) {
					AsyncImage(model = R.drawable.sv_back,
						contentDescription = "",
						modifier = Modifier
							.size(40.dp)
							.clickable { onBack() }
							.padding(10.dp))
				}
			},
			actions = {
				Box(modifier = Modifier
					.size(39.dp)
					.clickable {
						OrderRequirementsActivity.goOrderRequirementsActivity(context,
							orderInfo,
							type = Const.InServiceSettingType.ORDER_DETAIL)
					}
					.padding(10.dp), contentAlignment = Alignment.Center) {
					AsyncImage(model = R.drawable.sv_setting,
						contentDescription = "",
						modifier = Modifier.fillMaxSize())
				}
			})

		AppTipsView()
	}
}

@Composable
fun ServiceOperation(orderInfo : OrderInfo?) {
	val context = LocalContext.current
	val showNavigationDialog = remember { mutableStateOf(false) }
	val showCallPhoneDialog = remember { mutableStateOf(false) }
	val showCallServicePhoneDialog = remember { mutableStateOf(false) }

	if (showNavigationDialog.value) {
		StartNavigationView(orderInfo = orderInfo, dismiss = { showNavigationDialog.value = false })
	}

	if (showCallPhoneDialog.value) {
		CommonDialog(cancelText = "取消",
			confirmText = "确定",
			title = "是否联系客户？",
			cancelEnable = true,
			cancel = {
				showCallPhoneDialog.value = false
			},
			dismiss = { showCallPhoneDialog.value = false },
			confirm = {
				showCallPhoneDialog.value = false
				context.callPhone(orderInfo?.customerPhone)
				context.finish()
			})
	}

	if (showCallServicePhoneDialog.value) {
		CommonDialog(cancelText = "取消",
			confirmText = "确定",
			title = "是否联系中道客服？",
			cancelEnable = true,
			cancel = {
				showCallServicePhoneDialog.value = false
			},
			dismiss = { showCallServicePhoneDialog.value = false },
			confirm = {
				showCallServicePhoneDialog.value = false
				context.callPhone(orderInfo?.hotline)
				context.finish()
			})
	}

	Column(modifier = Modifier
		.fillMaxWidth()
		.padding(10.dp),
		horizontalAlignment = AbsoluteAlignment.Right,
		verticalArrangement = Arrangement.Top) {
		Spacer(modifier = Modifier.height(2.dp))
		AsyncImage(model = R.drawable.sv_service_phone,
			contentDescription = "",
			modifier = Modifier
				.size(46.dp)
				.clickable { showCallServicePhoneDialog.value = true })
		Spacer(modifier = Modifier.height(2.dp))
		AsyncImage(model = R.drawable.sv_customer,
			contentDescription = "",
			modifier = Modifier
				.size(46.dp)
				.clickable { showCallPhoneDialog.value = true })
		Spacer(modifier = Modifier.height(2.dp))
		AsyncImage(model = R.drawable.sv_navigation,
			contentDescription = "",
			modifier = Modifier
				.size(46.dp)
				.clickable { showNavigationDialog.value = true })
	}
}

@Composable
fun StartNavigationView(orderInfo : OrderInfo?, dismiss : () -> Unit) {
	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 {
					if ("SENDTO" == orderInfo?.taskState) {
						MapUtil.startNavigationGd(context,
							orderInfo.distLat,
							lng = orderInfo.distLng,
							address = orderInfo.distAddress)
					} else {
						MapUtil.startNavigationGd(context,
							orderInfo?.lat,
							lng = orderInfo?.lng,
							address = orderInfo?.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 {
					if ("SENDTO" == orderInfo?.taskState) {
						MapUtil.startNavigationBd(context,
							orderInfo.distLat,
							lng = orderInfo.distLng,
							address = orderInfo.distAddress)
					} else {
						MapUtil.startNavigationBd(context,
							orderInfo?.lat,
							lng = orderInfo?.lng,
							address = orderInfo?.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 {
					if ("SENDTO" == orderInfo?.taskState) {
						MapUtil.startNavigationTencent(context,
							orderInfo.distLat,
							lng = orderInfo.distLng,
							address = orderInfo.distAddress)
					} else {
						MapUtil.startNavigationTencent(context,
							orderInfo?.lat,
							lng = orderInfo?.lng,
							address = orderInfo?.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)
				}
			}
		}
	}
}