package com.za.ext

import android.content.Context
import com.alibaba.fastjson.JSON
import com.blankj.utilcode.util.EncodeUtils
import com.za.base.AppConfig
import com.za.bean.db.order.OrderInfo
import com.za.common.GlobalData
import com.za.common.log.LogUtil
import com.za.room.RoomHelper
import com.za.ui.servicing.check_vehicle.CheckVehicleActivity
import com.za.ui.servicing.departure_photo.DeparturePhotoActivity
import com.za.ui.servicing.destination_photo.DestinationPhotoActivity
import com.za.ui.servicing.go_accident.GoAccidentSiteActivity
import com.za.ui.servicing.go_to_destination.GoToDestinationActivity
import com.za.ui.servicing.operation.InOperationActivity
import com.za.ui.servicing.order_confirm.OrderConfirmActivity
import com.za.ui.servicing.verify.VerifyOrderActivity
import com.za.ui.servicing.wait_to_start.WaitToStartActivity
import java.nio.charset.StandardCharsets

fun Any?.toJson() : String? {
	if (this == null) {
		return null
	}
	return JSON.toJSON(this).toString()
}

fun OrderInfo.goStatusPage(context : Context, isFinishActivity : Boolean? = true) {
	when (this.taskState) { //等待发车
		"START" -> {
			if (! RoomHelper.db?.photoTemplateDao()
					?.getOrderPhotoTemplateFromTaskNode(10100, userOrderId ?: 0).isNullOrEmpty()
			) {
				context.navigationActivity(DeparturePhotoActivity::class.java,
					isFinish = isFinishActivity)
				return
			}
			context.navigationActivity(WaitToStartActivity::class.java, isFinish = isFinishActivity)
		}

		"GOTO" -> context.navigationActivity(GoAccidentSiteActivity::class.java,
			isFinish = isFinishActivity) //验证服务资格
		"VERIFY" -> context.navigationActivity(VerifyOrderActivity::class.java,
			isFinish = isFinishActivity) //检查车辆
		"EXAMINE" -> context.navigationActivity(CheckVehicleActivity::class.java,
			isFinish = isFinishActivity) //作业中
		"OPERATION" -> context.navigationActivity(InOperationActivity::class.java,
			isFinish = isFinishActivity) //前往目的地
		"SENDTO" -> context.navigationActivity(GoToDestinationActivity::class.java,
			isFinish = isFinishActivity) //目的地照片
		"DESTPHOTO" -> context.navigationActivity(DestinationPhotoActivity::class.java,
			isFinish = isFinishActivity) //结算中
		"SETTLEMENT" -> context.navigationActivity(OrderConfirmActivity::class.java,
			isFinish = isFinishActivity) //任务结束
		"FINISH" -> context.navigationActivity(WaitToStartActivity::class.java,
			isFinish = isFinishActivity)
	}
}

fun OrderInfo.getNextStatus() : String? {
	val flowStr : String?
	val flowArray = flow?.split(",")
	if (flowArray.isNullOrEmpty()) {
		LogUtil.print("getNextStatus", "订单流程为空")
		return null
	}
	flowStr = flowArray[flowArray.indexOf(this.taskState) + 1]
	LogUtil.print("getNextStatus flowStr", "$flowStr")
	return flowStr
}

fun goNextPage(status : String?, context : Context, isFinishActivity : Boolean? = true) {
	when (status) { //等待发车
		"START" -> {
			if (! RoomHelper.db?.photoTemplateDao()?.getOrderPhotoTemplateFromTaskNode(10100,
					GlobalData.currentOrder?.userOrderId ?: 0).isNullOrEmpty()
			) {
				context.navigationActivity(DeparturePhotoActivity::class.java,
					isFinish = isFinishActivity)
				return
			}
			context.navigationActivity(WaitToStartActivity::class.java, isFinish = isFinishActivity)
		} //前往事发地
		"GOTO" -> context.navigationActivity(GoAccidentSiteActivity::class.java,
			isFinish = isFinishActivity) //验证服务资格
		"VERIFY" -> context.navigationActivity(VerifyOrderActivity::class.java,
			isFinish = isFinishActivity) //检查车辆
		"EXAMINE" -> context.navigationActivity(CheckVehicleActivity::class.java,
			isFinish = isFinishActivity) //作业中
		"OPERATION" -> context.navigationActivity(InOperationActivity::class.java,
			isFinish = true) //前往目的地
		"SENDTO" -> context.navigationActivity(GoToDestinationActivity::class.java,
			isFinish = isFinishActivity) //目的地照片
		"DESTPHOTO" -> context.navigationActivity(DestinationPhotoActivity::class.java,
			isFinish = isFinishActivity) //结算中
		"SETTLEMENT" -> context.navigationActivity(OrderConfirmActivity::class.java,
			isFinish = isFinishActivity) //任务结束
		"FINISH" -> context.navigationActivity(WaitToStartActivity::class.java,
			isFinish = isFinishActivity)
	}
}

fun OrderInfo.getTaskNode() : Int { //防空的节点为 18100
	return when (this.taskState) { //检查车辆
		"EXAMINE" -> 13001 //作业中
		"OPERATION" -> 15001 //目的地照片
		"DESTPHOTO" -> 17001 //结算中
		"SETTLEMENT" -> 18001
		else -> 0
	}
}

fun OrderInfo.convertToFlowName() : String {
	return when (this.taskState) {
		"START" -> "等待发车"
		"GOTO" -> "前往事发地"
		"VERIFY" -> "验证服务资格"
		"EXAMINE" -> "检查车辆"
		"OPERATION" -> "作业中"
		"SENDTO", "SIMPLE_CARRYING" -> "前往目的地"
		"DESTPHOTO", "SIMPLE_DEST_PHOTO" -> "目的地照片"
		"SIMPLE_SETTLEMENT", "SETTLEMENT" -> "结算中"
		"FINISH" -> "任务结束"
		"SIMPLE_ARRIVE_PHOTO" -> "现场照片"
		"SIMPLE_CARRY_PRE" -> "开始拖车"
		"SIMPLE_DESP_PHOTO" -> "完成确认"

		else -> "继续任务"
	}
}

fun OrderInfo.getEleState() : Int {
	if (RoomHelper.db?.eleWorkOrderDao()?.getEleWorkOrder(taskId ?: 0) == null) {
		LogUtil.print("作业中获取电子表单未空", "电子表单为空")
		return electronOrderState ?: 0
	}

	if (electronOrderState == RoomHelper.db?.eleWorkOrderDao()
			?.getEleWorkOrder(taskId ?: 0)?.orderWorkStatus
	) {
		return electronOrderState ?: 0
	}

	return (electronOrderState ?: 0).coerceAtLeast(RoomHelper.db?.eleWorkOrderDao()
		?.getEleWorkOrder(taskId ?: 0)?.orderWorkStatus ?: 0)
}

fun OrderInfo.getEleOrderH5Url() : String? {
	if (GlobalData.currentOrder == null) {
		return null
	}
	val queryParams =
		"userOrderId=${GlobalData.currentOrder?.userOrderId}&userOrderCode=${GlobalData.currentOrder?.taskCode}&supplierId=${GlobalData.currentOrder?.userOrderId}"
	return AppConfig.Resource_URL + "/electronicWorkOrder/index.html?" + EncodeUtils.base64Encode2String(
		queryParams.toByteArray(StandardCharsets.UTF_8))
}