package com.za.ui.servicing.order_give_up

import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
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.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
import com.za.base.BaseActivity
import com.za.base.view.CommonButton
import com.za.base.view.CommonDialog
import com.za.base.view.HeadView
import com.za.bean.db.order.OrderInfo
import com.za.bean.db.order.PhotoTemplateInfo
import com.za.ext.finish
import com.za.servicing.R
import com.za.ui.servicing.InServicingPhotoView
import com.za.ui.view.SignatureView

class OrderGiveUpActivity : BaseActivity() {

	@Composable
	override fun ContentView() {
		val orderInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
			intent.getParcelableExtra("orderInfo", OrderInfo::class.java)
		} else {
			intent.getParcelableExtra("orderInfo")
		}

		OrderGiveUpScreen(orderInfo = orderInfo,
			taskId = intent.getIntExtra("taskId", 0),
			giveUpType = intent.getIntExtra("giveUpType", 0)) {
		}
	}

	companion object {
		//giveUp type
		//0 师傅手机操作放弃   1 后台操作放弃  2 师傅出发前后台操作放弃
		fun goOrderGiveUpActivity(context : Context,
			orderInfo : OrderInfo? = null,
			taskId : Int? = null,
			userOrderId : Int? = null,
			giveUpType : Int) {
			val intent = Intent(context, OrderGiveUpActivity::class.java)
			intent.putExtra("giveUpType", giveUpType)
			intent.putExtra("orderInfo", orderInfo)
			intent.putExtra("taskId", taskId)
			intent.putExtra("userOrderId", userOrderId)
			context.startActivity(intent)
		}
	}
}


@Composable
fun OrderGiveUpScreen(vm : OrderGiveUpVm = viewModel(),
	orderInfo : OrderInfo?,
	taskId : Int,
	giveUpType : Int,
	onBack : () -> Unit) {
	val uiState = vm.uiState.collectAsStateWithLifecycle()
	val context = LocalContext.current

	LaunchedEffect(key1 = Unit) {
		vm.dispatch(OrderGiveUpVm.Action.UpdateState(uiState.value.copy(orderInfo = orderInfo,
			taskId = taskId,
			giveUpType = giveUpType)))
		vm.dispatch(OrderGiveUpVm.Action.Init)
	}

	if (uiState.value.orderGiveUpSuccess == true) {
		context.finish()
		onBack()
	}

	if (uiState.value.isGoNextPageDialog == true) {
		CommonDialog(cancelText = "取消",
			confirmText = "是否确认放弃订单",
			title = "放弃订单",
			cancelEnable = true,
			cancel = {
				vm.dispatch(OrderGiveUpVm.Action.UpdateState(uiState.value.copy(isGoNextPageDialog = false)))
			},
			dismiss = {
				vm.dispatch(OrderGiveUpVm.Action.UpdateState(uiState.value.copy(isGoNextPageDialog = false)))
			},
			confirm = {
				vm.dispatch(OrderGiveUpVm.Action.UpdateState(uiState.value.copy(isGoNextPageDialog = false)))
				vm.dispatch(OrderGiveUpVm.Action.UpdateTask)
			})
	}

	Scaffold(topBar = {
		HeadView(title = "放弃信息", onBack = { context.finish() })
	}, bottomBar = {
		CommonButton(text = "提交信息，确认放弃案件") {
			vm.dispatch(OrderGiveUpVm.Action.UpdateState(uiState.value.copy(isGoNextPageDialog = true)))
		}
	}) { it ->
		LazyColumn(modifier = Modifier
			.fillMaxSize()
			.padding(it),
			contentPadding = PaddingValues(10.dp)) {

			itemsIndexed(items = uiState.value.photoTemplateList ?: arrayListOf(),
				key = { _, item -> item.hashCode() }) { index : Int, item : PhotoTemplateInfo ->
				InServicingPhotoView(photoTemplateInfo = item, index = index + 1, success = {
					vm.dispatch(OrderGiveUpVm.Action.UpdatePhotoTemplate(it))
				})
				Spacer(modifier = Modifier.height(10.dp))
			}

			if (uiState.value.giveUpType == 0) {
				item {
					Spacer(modifier = Modifier.height(10.dp))
					Column(modifier = Modifier
						.fillMaxWidth()
						.padding(horizontal = 10.dp)
						.background(color = Color.White, shape = RoundedCornerShape(6.dp))
						.padding(10.dp)) {

						Row(modifier = Modifier.fillMaxWidth(),
							verticalAlignment = Alignment.CenterVertically) {
							Text(text = "司机签名",
								color = Color.Black,
								fontWeight = FontWeight.Medium,
								fontSize = 14.sp)
							Spacer(Modifier.weight(1f))
							AsyncImage(model = R.drawable.sv_sign_new,
								contentDescription = "",
								modifier = Modifier.size(20.dp))
						}

						Spacer(modifier = Modifier.height(10.dp))
						SignatureView(modifier = Modifier
							.fillMaxWidth()
							.height(122.dp), success = {
							vm.dispatch(OrderGiveUpVm.Action.UploadSign(it))
						}, serverPath = uiState.value.serverServicePeopleSignPath)
					}
				}
			}
		}
	}
}