package com.za.common

import android.app.Application
import com.amap.api.location.AMapLocation
import com.blankj.utilcode.util.AppUtils
import com.tencent.mmkv.MMKV
import com.za.base.AppConfig
import com.za.base.Const
import com.za.bean.db.order.OrderInfo
import com.za.common.log.LogUtil
import com.za.room.RoomHelper
import com.za.room.db.user.DriverInfoBean

object GlobalData : GlobalLocalData() {
	lateinit var application : Application
	private val mmkv : MMKV by lazy { MMKV.defaultMMKV() }
	var activityCount : Int = 0
	var isMaster = AppUtils.getAppPackageName() == "com.za.rescue.dealer"

	var token : String? = null
		get() {
			return mmkv.decodeString("TOKEN", null)
		}
		set(value) {
			mmkv.encode("TOKEN", value)
			field = value
		}

	var regid : String? = null
		get() {
			return mmkv.decodeString("regid", null)
		}
		set(value) {
			mmkv.encode("regid", value)
			field = value
		}

	var aesKey : String? = null
		get() {
			return mmkv.decodeString("AES_KEY", null)
		}
		set(value) {
			mmkv.encode("AES_KEY", value)
			field = value
		}

	//新订单是否已经被处理
	var isHandlerNewOrder : Boolean? = false

	var driverInfoBean : DriverInfoBean? = null
		get() {
			if (field == null) {
				field = localDriverInfoBean
			}
			LogUtil.print("get driverInfoBean", "获取司机信息: $field")
			return field
		}
		set(value) {
			localDriverInfoBean = value
			field = value
			LogUtil.print("set driverInfoBean", "司机信息: $field")
		}


	var lastLoginBean : DriverInfoBean? = null
		get() {
			val driverInfoBean = mmkv.decodeParcelable("lastLoginBean", DriverInfoBean::class.java)
			field = driverInfoBean
			return driverInfoBean
		}
		set(value) {
			mmkv.encode("lastLoginBean", value)
		}

	//是否已经完成登录后的人脸识别
	var isLoginRecognition : Boolean? = null
		get() {
			return mmkv.decodeBool("isLoginRecognition", false)
		}
		set(value) {
			mmkv.encode("isLoginRecognition", value == true)
			field = value
		}


	var currentOrder : OrderInfo? = null
		get() {
			if (field == null) {
				field = localCurrentOrder
			}
			return field
		}
		set(value) {
			localCurrentOrder = value
			field = value
		}

	var currentLocation : AMapLocation? = null
		get() {
			return mmkv.decodeParcelable("currentLocation", AMapLocation::class.java)
		}
		set(value) {
			value?.time = System.currentTimeMillis()
			mmkv.encode("currentLocation", value)
			field = value
		}

	var loginTime : Long? = null
		get() {
			return mmkv.decodeLong("loginTime", System.currentTimeMillis())
		}
		set(value) {
			mmkv.encode("loginTime", value ?: System.currentTimeMillis())
			field = value
		}

	var networkEnv : Int
		get() {
			return if (AppConfig.isRelease) {
				mmkv.decodeInt("isReviewEnv", Const.NetEnv.Main)
			} else {
				mmkv.decodeInt("isReviewEnv", Const.NetEnv.CRM1)
			}
		}
		set(value) {
			mmkv.encode("isReviewEnv", value)
		}

	fun clearUserCache() {
		token = null
		aesKey = null
		currentLocation = null
		driverInfoBean = null
		loginTime = null
		isLoginRecognition = null

		if (AppConfig.isRelease) {
			networkEnv = if (AppConfig.isRelease) {
				Const.NetEnv.Main
			} else {
				Const.NetEnv.CRM1
			}
		}


	}

	fun clearAllOrderCache() {
		currentOrder = null
		RoomHelper.clearAll()
	}

	fun clearOrderCache(taskId : Int) {
		RoomHelper.clearOrderFromTaskCode(taskId = taskId)
	}
}