package com.za.ui.servicing.order_confirm

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.za.base.BaseActivity
import com.za.base.view.CommonDialog
import com.za.base.view.LoadingManager
import com.za.room.RoomHelper
import com.za.ui.servicing.order_confirm.input_money.InputMoneyActivity
import com.za.ui.servicing.order_confirm.real_order_confirm.RealOrderConfirmActivity
import com.za.ui.servicing.order_confirm.receive_money.ReceiveMoneyActivity

class OrderConfirmActivity : BaseActivity() {

	@Composable
	override fun ContentView() {
		OrderConfirmInitScreen()
	}
}

@Composable
fun OrderConfirmInitScreen(vm : OrderConfirmInitVm = viewModel()) {
	val uiState = vm.uiState.collectAsStateWithLifecycle()
	val context = LocalContext.current
	LaunchedEffect(key1 = Unit) {
		vm.dispatch(OrderConfirmInitVm.Action.Init)
	}

	if (uiState.value.showNoNeedPayDialog == true) {
		CommonDialog(title = "是否需要收款？",
			confirmText = "去收款",
			cancelText = "无需收款",
			confirm = {
				vm.updateState(uiState.value.copy(showNoNeedPayDialog = false))
				InputMoneyActivity.goInputMoney(context,
					userOrderId = uiState.value.orderInfo?.userOrderId ?: 0,
					taskId = uiState.value.orderInfo?.taskId ?: 0)
			},
			cancel = {
				uiState.value.eleWorkOrderBean?.copy(driverChoiceNoNeedReceiveMoney = true)?.let {
					RoomHelper.db?.eleWorkOrderDao()?.update(it)
				}
				vm.updateState(uiState.value.copy(showNoNeedPayDialog = false,
					orderConfirmState = OrderConfirmInitVm.OrderConfirmState.Init))
				vm.dispatch(OrderConfirmInitVm.Action.Init)
			},
			dismiss = {
				vm.updateState(uiState.value.copy(showNoNeedPayDialog = false,
					orderConfirmState = OrderConfirmInitVm.OrderConfirmState.Init))
				vm.dispatch(OrderConfirmInitVm.Action.Init)
			})
	}

	when (uiState.value.orderConfirmState) {
		is OrderConfirmInitVm.OrderConfirmState.ChangeBattery -> ChangeBatteryScreen()
		is OrderConfirmInitVm.OrderConfirmState.ConfirmEle -> ConfirmEleScreen()
		is OrderConfirmInitVm.OrderConfirmState.ConfirmH5Success -> ConfirmH5SuccessScreen()
		is OrderConfirmInitVm.OrderConfirmState.Init -> {
			Box {
				LoadingManager.showLoading()
			}
		}

		is OrderConfirmInitVm.OrderConfirmState.Failed -> {
			Box(modifier = Modifier.clickable {
				vm.dispatch(OrderConfirmInitVm.Action.Init)
			}) {
				Text("点击重试")
			}
		}

		is OrderConfirmInitVm.OrderConfirmState.PaymentInfo -> {
			ReceiveMoneyActivity.goReceiveMoney(context,
				userOrderId = uiState.value.orderInfo?.userOrderId ?: 0,
				taskId = uiState.value.orderInfo?.taskId ?: 0)
		}

		else -> RealOrderConfirmActivity.goRealOrderConfirm(context)
	}

}