package com.za.base

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.za.bean.JpushBean
import com.za.common.GlobalData
import com.za.common.log.LogUtil
import com.za.service.PushListener
import com.za.service.ServiceManager

open class PushMessageActivity : AppCompatActivity() {
	private var currentDialog : AlertDialog? = null

	override fun onCreate(savedInstanceState : Bundle?) {
		super.onCreate(savedInstanceState) //		setupPushMessageReceiver()
	}

	private fun setupPushMessageReceiver() {        // 注册推送消息接收器
		ServiceManager.registerPushListener(object : PushListener {
			override fun broadcast(msg : String) {
				sendMessageToMainProcess(type = Const.PushMessageType.BROADCAST,
					message = msg,
					context = this@PushMessageActivity)
			}

			override fun giveUpOrder(jpushBean : JpushBean) {
				sendMessageToMainProcess(type = Const.PushMessageType.GIVE_UP,
					message = Gson().toJson(jpushBean),
					context = this@PushMessageActivity)
			}

			override fun importantTip(jpushBean : JpushBean) {
				sendMessageToMainProcess(type = Const.PushMessageType.IMPORTANT_TIP,
					message = Gson().toJson(jpushBean),
					context = this@PushMessageActivity)
			}

			override fun newOrderMsg(jpushBean : JpushBean) {
				sendMessageToMainProcess(type = Const.PushMessageType.NEW_ORDER,
					message = Gson().toJson(jpushBean),
					context = this@PushMessageActivity)
			}

			override fun reDispatchOrder(jpushBean : JpushBean) {
				sendMessageToMainProcess(type = Const.PushMessageType.RE_DISPATCH,
					message = Gson().toJson(jpushBean),
					context = this@PushMessageActivity)
			}

			override fun revokeOrder(jpushBean : JpushBean) {
				sendMessageToMainProcess(type = Const.PushMessageType.REVOKE,
					message = Gson().toJson(jpushBean),
					context = this@PushMessageActivity)
			}

			override fun modifyOrder(jpushBean : JpushBean) {
				sendMessageToMainProcess(type = Const.PushMessageType.MODIFY,
					message = Gson().toJson(jpushBean),
					context = this@PushMessageActivity)
			}
		})
	}


	override fun onPause() {
		super.onPause()
		dismissCurrentDialog()
	}

	private fun dismissCurrentDialog() {
		try {
			currentDialog?.dismiss()
			currentDialog = null
		} catch (e : Exception) {
			LogUtil.print("PushActivityLifecycleCallbacks", "关闭对话框失败: ${e.message}")
		}
	}

	companion object {
		internal const val GIVE_UP_TYPE_NORMAL = 1
		internal const val DIALOG_TAG_GIVE_UP = "giveUp"

		private fun sendMessageToMainProcess(context : Context, type : String, message : String) {

			// 使用广播将消息发送到主进程
			val intent = Intent(Const.PushMessageType.ACTION_MAIN.takeIf { GlobalData.isMaster }
				?: Const.PushMessageType.ACTION_SDK)
			intent.setPackage(context.packageName)
			intent.putExtra("type", type)
			intent.putExtra("message", message)
			context.sendBroadcast(intent)
			LogUtil.print("KeepAliveService", "发送消息到主进程: $type")
		}
	}
}