package com.za.base

import com.za.bean.db.ele.EleWorkOrderBean
import com.za.bean.db.order.OrderInfo
import com.za.bean.db.order.PhotoTemplateInfo
import com.za.common.GlobalData
import com.za.common.log.LogUtil
import com.za.ext.getTaskNode
import com.za.ext.toJson
import com.za.net.CommonMethod
import com.za.offline.OfflineManager
import com.za.offline.OfflineUpdateTaskBean
import com.za.room.RoomHelper

abstract class IServicingVm<T, U> : BaseVm<T, U>() {

	fun getCurrentOrder() : OrderInfo? =
		GlobalData.currentOrder ?: RoomHelper.db?.orderDao()?.getCurrentOrder()

	fun updateOrder(orderInfo : OrderInfo?) {
		orderInfo?.let { order ->
			if (order.isCurrent == true) {
				GlobalData.currentOrder = order
			}
			RoomHelper.db?.orderDao()?.update(order)
			LogUtil.print("$tag updateOrder", order.toJson() ?: "")
		} ?: LogUtil.print("$tag updateOrder", "order is null")
	}

	// 使用 Flow 重写获取照片模板的方法
	//当前订单的照片模版
	fun getCurrentPhotoTemplate(success : (List<PhotoTemplateInfo>?) -> Unit,
		failure : (String?) -> Unit = {}) {
		val photoTemplateList = RoomHelper.db?.photoTemplateDao()
			?.getOrderPhotoTemplateFromTaskNode(getCurrentOrder()?.getTaskNode() ?: 0,
				getCurrentOrder()?.userOrderId ?: 0)
		if (photoTemplateList.isNullOrEmpty()) {
			CommonMethod.fetchPhotoTemplate(GlobalData.currentOrder, success = {
				val data = RoomHelper.db?.photoTemplateDao()?.getOrderPhotoTemplateFromTaskNode(
					getCurrentOrder()?.getTaskNode() ?: 0,
					getCurrentOrder()?.userOrderId ?: 0)
				success(data)
			}, failed = {
				failure(it)
			})
		} else {
			success(photoTemplateList)
		}
	}

	// 使用 Flow 重写获取照片模板的方法
	//获取出发前照片模版
	fun getDeparturePhotoTemplate(success : (List<PhotoTemplateInfo>?) -> Unit,
		failure : (String?) -> Unit = {}) {
		val photoTemplateList = RoomHelper.db?.photoTemplateDao()
			?.getOrderPhotoTemplateFromTaskNode(10100, getCurrentOrder()?.userOrderId ?: 0)
		if (photoTemplateList.isNullOrEmpty()) {
			CommonMethod.fetchPhotoTemplate(GlobalData.currentOrder, success = {
				val data = RoomHelper.db?.photoTemplateDao()
					?.getOrderPhotoTemplateFromTaskNode(10100, getCurrentOrder()?.userOrderId ?: 0)
				success(data)
			}, failed = {
				failure(it)
			})
		} else {
			success(photoTemplateList)
		}
	}

	fun checkIsGoDeparturePhoto() : Boolean {
		val list = RoomHelper.db?.photoTemplateDao()
			?.getOrderPhotoTemplateFromTaskNode(10100, getCurrentOrder()?.userOrderId ?: 0)
		return ! list.isNullOrEmpty()
	}

	fun updateCurrentEleWorkOrder(eleWorkOrderBean : EleWorkOrderBean) {
		RoomHelper.db?.eleWorkOrderDao()?.update(eleWorkOrderBean)

		getCurrentOrder()?.let { order ->
			val updatedOrder = order.copy(electronOrderState = eleWorkOrderBean.orderWorkStatus,
				taskSuccessStatus = if (eleWorkOrderBean.isSuccess == 1) 0 else 1)
			updateOrder(updatedOrder)
		}

		LogUtil.print("$tag updateCurrentEleWorkOrder", eleWorkOrderBean.toJson() ?: "")
	}

	fun getCurrentOrderOfflineTask() : List<OfflineUpdateTaskBean>? {
		return getCurrentOrder()?.taskId?.let { taskId ->
			RoomHelper.db?.offlineTaskDao()?.getOfflineTaskFromTaskId(taskId)
		}
	}

	fun clearCurrentOrderOfflineTask() {
		getCurrentOrder()?.taskId?.let { taskId ->
			RoomHelper.db?.offlineTaskDao()?.deleteOfflineTaskFromTaskId(taskId)
			LogUtil.print("$tag clearCurrentOrderOfflineTask", "taskId==$taskId")
		}
	}

	fun getCurrentOrderEleWorkOrder() : EleWorkOrderBean? {
		return RoomHelper.db?.eleWorkOrderDao()?.getEleWorkOrder(getCurrentOrder()?.taskId ?: 0)
	}

	fun insertOfflineTask(offlineUpdateTaskBean : OfflineUpdateTaskBean) {
		RoomHelper.db?.offlineTaskDao()?.let { offlineDao ->
			val primaryId = offlineUpdateTaskBean.primaryId ?: 0
			val existingTask = offlineDao.getOfflineTaskFromPrimaryId(primaryId)

			if (existingTask != null) {
				offlineDao.update(offlineUpdateTaskBean)
			} else {
				offlineDao.insertOfflineTask(offlineUpdateTaskBean)
			}

			offlineUpdateTaskBean.taskId?.let { taskId ->
				OfflineManager.start(taskId)
			}

			LogUtil.print("$tag insertOfflineTask", offlineUpdateTaskBean.toJson() ?: "")
		}
	}
}