package com.za.common

import com.tencent.mmkv.MMKV
import com.za.bean.db.order.OrderInfo
import com.za.common.GlobalData.lastLoginBean
import com.za.common.log.LogUtil
import com.za.room.RoomHelper
import com.za.room.db.user.DriverInfoBean

open class GlobalLocalData {
	var localDriverInfoBean : DriverInfoBean?
		get() {
			return try {
				val driverInfo = MMKV.defaultMMKV()
					.decodeParcelable("driverInfoBean", DriverInfoBean::class.java)
				LogUtil.print("local_driverInfoBean", "获取司机信息: $driverInfo")
				driverInfo
			} catch (e : Exception) {
				LogUtil.print("local_driverInfoBean", "获取司机信息失败: ${e.message}")
				null
			}
		}
		set(value) {
			try {
				MMKV.defaultMMKV().encode("driverInfoBean", value)
				if (value != null) {
					lastLoginBean = value
				}
				LogUtil.print("set local_driverInfoBean", "保存司机信息: $value")
			} catch (e : Exception) {
				LogUtil.print("set local_driverInfoBean", "保存司机信息失败: ${e.message}")
			}
		}

	var localCurrentOrder : OrderInfo? = null
		get() {
			return MMKV.defaultMMKV().decodeParcelable("currentOrder", OrderInfo::class.java)
		}
		set(value) {
			MMKV.defaultMMKV().encode("currentOrder", value)
			if (RoomHelper.db?.orderDao()?.getCurrentOrder() == null && value != null) {
				RoomHelper.db?.orderDao()?.insertOrder(value)
			} else if (value != null) {
				RoomHelper.db?.orderDao()?.update(value)
			}
			field = value
		}
}

