package com.za.service

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import android.os.Build
import android.os.Handler
import android.os.Looper
import androidx.core.app.NotificationCompat
import cn.jiguang.api.utils.JCollectionAuth
import cn.jpush.android.api.JPushInterface
import com.blankj.utilcode.util.ProcessUtils
import com.google.gson.Gson
import com.za.bean.JpushBean
import com.za.common.GlobalData
import com.za.common.log.LogUtil
import com.za.common.util.DeviceUtil
import com.za.service.mqtt.MyMqttClient
import com.za.servicing.R

interface PushListener {
	fun newOrderMsg(jpushBean : JpushBean)
	fun giveUpOrder(jpushBean : JpushBean)
	fun revokeOrder(jpushBean : JpushBean)
	fun reDispatchOrder(jpushBean : JpushBean)
	fun broadcast(string : String)
	fun importantTip(jpushBean : JpushBean)
}

data class LastJPushBean(val msg : String, val time : Long = System.currentTimeMillis())

object ServiceManager {
	@Volatile
	private var pushListener : PushListener? = null
	private var lastJPushBean : LastJPushBean? = null
	private const val DUPLICATE_MSG_THRESHOLD = 3000L // 3秒


	// Initialize SharedPreferences
	fun initialize(context : Context) {
		LogUtil.print("ServiceManager", "Initializing ServiceManager")
		jpushRegister(context)
		MyMqttClient.initialize(deviceId = DeviceUtil.getAndroidId(context))
		setupPushMessageReceiver(context)
	}

	private fun setupPushMessageReceiver(context : Context) {        // 注册推送消息接收器
		registerPushListener(object : PushListener {
			override fun broadcast(msg : String) {
				sendMessageToMainProcess(context = context, "broadcast", msg)
			}

			override fun giveUpOrder(jpushBean : JpushBean) {
				sendMessageToMainProcess(context = context, "giveUp", Gson().toJson(jpushBean))
			}

			override fun importantTip(jpushBean : JpushBean) {
				sendMessageToMainProcess(context = context,
					"importantTip",
					Gson().toJson(jpushBean))
			}

			override fun newOrderMsg(jpushBean : JpushBean) {
				sendMessageToMainProcess(context = context, "newOrder", Gson().toJson(jpushBean))
			}

			override fun reDispatchOrder(jpushBean : JpushBean) {
				sendMessageToMainProcess(context = context, "reDispatch", Gson().toJson(jpushBean))
			}

			override fun revokeOrder(jpushBean : JpushBean) {
				sendMessageToMainProcess(context = context, "revoke", Gson().toJson(jpushBean))
			}
		})
	}

	private fun sendMessageToMainProcess(context : Context,
		type : String,
		message : String) {        // 使用广播将消息发送到主进程
		val intent = Intent("com.za.rescue.dealer.PUSH_MESSAGE".takeIf { GlobalData.isMaster }
			?: ZdPushServiceReceive.RECEIVE_ACTION)
		intent.setPackage(context.packageName)
		intent.putExtra("type", type)
		intent.putExtra("message", message)
		context.sendBroadcast(intent)
		LogUtil.print("KeepAliveService", "发送消息到主进程: $type")
	}

	// Register JPush
	private fun jpushRegister(context : Context) {
		try {
			JCollectionAuth.setAuth(context, true)
			JPushInterface.setDebugMode(false)
			JPushInterface.init(context)
			LogUtil.print("ServiceManager", "JPush initialized successfully")
		} catch (e : Exception) {
			LogUtil.print("ServiceManager", "JPush initialization failed: ${e.message}")
		}
	}

	// Register push listener
	fun registerPushListener(listener : PushListener) {
		Handler(Looper.getMainLooper()).post {
			this.pushListener = listener
			LogUtil.print("ServiceManager", "Registered push listener: ${this.pushListener}")
		}
	}

	// Handle incoming push messages
	fun handlerPushMsg(msg : String) {
		LogUtil.print("handlerPushMsg", "Received push message: $msg")

		// 优化后的重复消息判断
		lastJPushBean?.let {
			if (System.currentTimeMillis() - it.time < DUPLICATE_MSG_THRESHOLD && it.msg == msg) {
				LogUtil.print("MessageHandler", "Duplicate message detected (hash: ${msg})")
				return
			}
		}

		if (msg.startsWith("broadcast:")) {
			lastJPushBean = LastJPushBean(msg = msg)
			handleBroadcast(msg)
			return
		}
		try {
			lastJPushBean = LastJPushBean(msg = msg)
			val jpushOrderInfoBean = Gson().fromJson(msg, JpushBean::class.java)
			sendSystemNotificationFromMessage(jpushOrderInfoBean)
			when (jpushOrderInfoBean.pushType) {
				0 -> newOrderMsg(jpushOrderInfoBean)
				1 -> handleTypeOneMessage(jpushOrderInfoBean)
				3 -> importantTip(jpushOrderInfoBean)
				else -> LogUtil.print("JpushMessage",
					"Unknown push type: ${jpushOrderInfoBean.pushType}")
			}
		} catch (e : Exception) {
			if (msg.startsWith("broadcast:")) {
				handleBroadcast(msg)
			}
			LogUtil.print("JpushMessage", "Error handling message: ${e.message}")
		}
	}

	// Handle broadcast messages
	private fun handleBroadcast(msg : String) {
		try {
			val content = msg.substring(10)
			pushListener?.broadcast(content)
			sendNotification(GlobalData.application, content)
			LogUtil.print("JpushMessage", "Broadcast content: $content")
		} catch (e : Exception) {
			LogUtil.print("JpushMessage", "Broadcast failed: ${e.message}")
		}
	}

	// Handle type one messages
	private fun handleTypeOneMessage(jpushOrderBean : JpushBean) {
		when (jpushOrderBean.typeDesc) {
			"giveUp" -> giveUpOrder(jpushOrderBean)
			"revoke" -> revokeOrder(jpushOrderBean)
			"reDispatch" -> reDispatchOrder(jpushOrderBean)
			else -> LogUtil.print("JpushMessage", "Unknown typeDesc: ${jpushOrderBean.typeDesc}")
		}
	}

	// Handle new order messages
	private fun newOrderMsg(jpushOrderBean : JpushBean) {
		try {
			LogUtil.print("JpushMessage",
				"Handling new order message: ${this.pushListener} ${this.pushListener?.javaClass?.simpleName}")
			LogUtil.print("isManThread", " ${ProcessUtils.getCurrentProcessName()}")
			this.pushListener?.newOrderMsg(jpushOrderBean)
		} catch (e : Exception) {
			LogUtil.print("JpushMessage", "Failed to handle new order message: ${e.message}")
		}
	}

	// Handle give up order messages
	private fun giveUpOrder(jpushOrderBean : JpushBean) {
		pushListener?.giveUpOrder(jpushOrderBean)
	}

	// Handle revoke order messages
	private fun revokeOrder(jpushOrderBean : JpushBean) {
		pushListener?.revokeOrder(jpushOrderBean)
	}

	// Handle re-dispatch order messages
	private fun reDispatchOrder(jpushOrderBean : JpushBean) {
		pushListener?.reDispatchOrder(jpushOrderBean)
	}

	// Handle important tip messages
	private fun importantTip(jpushOrderBean : JpushBean) {
		pushListener?.importantTip(jpushOrderBean)
	}

	// Disconnect from JPush and MQTT
	fun disconnect(context : Context) {
		Handler(Looper.getMainLooper()).post {
			try {
				JPushInterface.stopPush(context) // Stop JPush
				MyMqttClient.disconnect() // Disconnect MQTT
				LogUtil.print("ServiceManager", "Disconnected from JPush and MQTT successfully")
			} catch (e : Exception) {
				LogUtil.print("ServiceManager", "Error during disconnection: ${e.message}")
			}
		}
	}


	private const val CHANNEL_ID = "ImportantMessagesChannel"
	private const val NOTIFICATION_ID = 1003

	// Initialize notification channel
	private fun createNotificationChannel(context : Context) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			val channel = NotificationChannel(CHANNEL_ID,
				"订单通知",
				NotificationManager.IMPORTANCE_HIGH).apply {
				description = "用于接收重要消息通知"
				setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
					Notification.AUDIO_ATTRIBUTES_DEFAULT)
				enableVibration(true)
			}
			val notificationManager =
				context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
			notificationManager.createNotificationChannel(channel)
		}
	}

	private fun sendSystemNotificationFromMessage(jpushOrderInfoBean : JpushBean) {
		when (jpushOrderInfoBean.pushType) {
			0 -> sendNotification(GlobalData.application,
				"您有新的 ${jpushOrderInfoBean.addressProperty ?: ""} ${jpushOrderInfoBean.serviceTypeName ?: ""}: ${jpushOrderInfoBean.taskCode ?: ""}")

			1 -> {
				when (jpushOrderInfoBean.typeDesc) {
					"giveUp" -> sendNotification(GlobalData.application,
						"订单:${jpushOrderInfoBean.taskCode ?: ""}已被放弃！")

					"revoke" -> sendNotification(GlobalData.application,
						"订单:${jpushOrderInfoBean.taskCode ?: ""}已被撤回！")

					"reDispatch" -> sendNotification(GlobalData.application,
						"订单:${jpushOrderInfoBean.taskCode ?: ""}被改派！")

					else -> {}
				}
			}

			3 -> sendNotification(GlobalData.application,
				"重要提醒：${jpushOrderInfoBean.tipContent ?: ""}")

			else -> {}
		}
	}

	// Send notification
	private fun sendNotification(context : Context, message : String) {
		createNotificationChannel(context)
		val notification =
			NotificationCompat.Builder(context, CHANNEL_ID).setContentTitle("重要通知")
				.setContentText(message).setSmallIcon(R.mipmap.ic_launcher) // 替换为你的应用图标
				.setPriority(NotificationCompat.PRIORITY_HIGH).setAutoCancel(true) // 点击后自动取消通知
				.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
				.setVibrate(longArrayOf(0, 100, 200, 300)).build()

		val notificationManager =
			context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
		notificationManager.notify(NOTIFICATION_ID, notification)
	}

}