package com.za.ui.servicing.inservice_people_confirm

import android.app.Activity
import android.content.Intent
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.background
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.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
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.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.blankj.utilcode.util.ToastUtils
import com.za.base.view.CommonButton
import com.za.base.view.CommonDialog
import com.za.base.view.HeadView
import com.za.common.GlobalData
import com.za.common.log.LogUtil
import com.za.ext.goNextPage
import com.za.servicing.R
import com.za.ui.camera.ServicePeopleRealActivity

@Composable
fun ServicePeopleConfirmScreen(vm : InServicePeopleConfirmVm = viewModel(),
	success : () -> Unit = {},
	onBack : () -> Unit) {
	val context = LocalContext.current
	val uiState = vm.uiState.collectAsStateWithLifecycle()
	val getResult =
		rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult()) { it ->
			if (it.resultCode == Activity.RESULT_OK) {
				val value = it.data?.getStringExtra("path")
				LogUtil.print("takePhoto", "path==$value")
				if (value.isNullOrBlank()) {
					ToastUtils.showLong("照片路径为空，请重新拍摄！")
					return@rememberLauncherForActivityResult
				}
				vm.dispatch(InServicePeopleConfirmVm.Action.CompilePeople(value))
			}
		}


	BackHandler {
		onBack()
	}

	if (uiState.value.compareResult == true) {
		vm.updateState(uiState.value.copy(compareResult = null))
		if (GlobalData.isMaster) {
			success()
		} else {
			vm.dispatch(InServicePeopleConfirmVm.Action.UpdateTask)
		}
	}

	if (uiState.value.goNextPage != null) {
		goNextPage(uiState.value.goNextPage?.nextState, context)
		vm.updateState(uiState.value.copy(goNextPage = null, compareResult = null))
	}


	if (! uiState.value.showCompareFailedDialog.isNullOrBlank()) {
		CommonDialog(title = uiState.value.showCompareFailedDialog,
			confirmText = "重新认证",
			confirm = {
				vm.updateState(uiState.value.copy(showCompareFailedDialog = null))
				val intent = Intent(context, ServicePeopleRealActivity::class.java)
				getResult.launch(intent)
			},
			cancelText = "关闭",
			cancel = {
				vm.updateState(uiState.value.copy(showCompareFailedDialog = null))
			},
			dismiss = {
				vm.updateState(uiState.value.copy(showCompareFailedDialog = null))
			})
	}

	Scaffold(topBar = { HeadView(title = "身份验证", onBack = onBack) }) {
		Column(modifier = Modifier
			.fillMaxSize()
			.background(color = Color.White)
			.padding(it)
			.verticalScroll(state = rememberScrollState()),
			horizontalAlignment = Alignment.CenterHorizontally) {
			Spacer(modifier = Modifier.height(60.dp))

			Box {
				Text(text = "为了确保服务技师信息的准确",
					fontWeight = FontWeight.SemiBold,
					color = Color.DarkGray,
					fontSize = 18.sp)
			}

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

			Box {
				Text(text = "需要进行人脸核验",
					color = Color.Gray,
					fontWeight = FontWeight.Normal,
					fontSize = 14.sp)
			}

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


			AsyncImage(model = R.drawable.sv_facea_verify,
				contentDescription = "",
				modifier = Modifier
					.background(color = Color.White)
					.padding(30.dp))

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

			Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
				Column(horizontalAlignment = Alignment.CenterHorizontally,
					verticalArrangement = Arrangement.Center) {
					AsyncImage(model = R.drawable.sv_face_light,
						contentDescription = "",
						modifier = Modifier.size(20.dp))
					Spacer(modifier = Modifier.height(5.dp))
					Text(text = "保持光线充足", fontSize = 10.sp, color = Color.Gray)
				}
				Spacer(modifier = Modifier.width(30.dp))
				Column(horizontalAlignment = Alignment.CenterHorizontally,
					verticalArrangement = Arrangement.Center) {
					AsyncImage(model = R.drawable.sv_face_phone,
						contentDescription = "",
						modifier = Modifier.size(20.dp))
					Spacer(modifier = Modifier.height(5.dp))
					Text(text = "需正对手机", fontSize = 10.sp, color = Color.Gray)
				}
				Spacer(modifier = Modifier.width(30.dp))
				Column(horizontalAlignment = Alignment.CenterHorizontally,
					verticalArrangement = Arrangement.Center) {
					AsyncImage(model = R.drawable.sv_face_face,
						contentDescription = "",
						modifier = Modifier.size(20.dp))
					Spacer(modifier = Modifier.height(5.dp))
					Text(text = "确保面部无遮挡", fontSize = 10.sp, color = Color.Gray)
				}
			}

			Spacer(modifier = Modifier.height(40.dp))
			CommonButton(text = "开始核验", onClick = {
				val intent = Intent(context, ServicePeopleRealActivity::class.java)
				getResult.launch(intent)
			})
		}
	}
}