package com.base.datarecovery.fcm

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Icon
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.graphics.drawable.IconCompat
import com.base.datarecovery.R
import com.base.datarecovery.activity.SplashActivity
import com.base.datarecovery.bean.ConstObject.ID_JUNK_CLEAN_PUSH
import com.base.datarecovery.bean.ConstObject.ID_PRIVACY_SPACE
import com.base.datarecovery.bean.ConstObject.ID_RECOVERY_DOCUMENTS
import com.base.datarecovery.bean.ConstObject.ID_RECOVERY_PHOTOS
import com.base.datarecovery.bean.ConstObject.ID_RECOVERY_VIDEOS
import com.base.datarecovery.bean.ConstObject.ID_SCREENSHOT_CLEAN
import com.base.datarecovery.bean.ConstObject.ID_SIMILAR_IMAGE
import com.base.datarecovery.utils.AppPreferences
import java.util.Random

/**
 * 构建发送通知
 * 用的actionId代替notificationId使用
 */
object NotificationUtil {

    private const val CHANNEL_ID = "recovery_notification_id" // 通知渠道ID
    private const val CHANNEL_NAME = "recovery_fcm_channel" // 通知渠道名称

    fun sendNotification(context: Context, actionId: Int) {
        val bigRemoteViews = RemoteViews(context.packageName, R.layout.notification_common)
        val smallRemoteViews = RemoteViews(context.packageName, R.layout.notification_common_small)

        var icon = 0
        var desc = ""
        var btn = ""
        when (actionId) {
            ID_JUNK_CLEAN_PUSH -> {
                icon = R.mipmap.cleanjunk_ss
                desc = "Free up space on your phone! Clean unwanted junk files now."
                btn = "Clean Up Space"
            }

            ID_SIMILAR_IMAGE -> {
                icon = R.mipmap.h_similar
                desc = "Check similar photos to release more space"
                btn = "Clean up"
            }

            ID_SCREENSHOT_CLEAN -> {
//                icon = R.mipmap.similar
                desc = "Streamline your photo album! Remove old and unnecessary screenshots."
                btn = "Remove Screenshots"
            }

            ID_RECOVERY_PHOTOS -> {
//                icon = R.mipmap.h_similar
                desc = "Has a photo gone missing? Try our recovery tool to get it back."
                btn = "Retrieve Photo"
            }

            ID_RECOVERY_VIDEOS -> {
//                icon = R.mipmap.h_similar
                desc = "Regain lost video moments. Start your video recovery now!"
                btn = "Recover Video"
            }

            ID_RECOVERY_DOCUMENTS -> {
//                icon = R.mipmap.h_similar
                desc = "Lost a vital document? Our recovery service might be able to help."
                btn = "Restore Document"
            }

            ID_PRIVACY_SPACE -> {
//                icon = R.mipmap.h_similar
                desc = "Secure your private content. Tuck away photos and videos in your hidden space."
                btn = "Hide Content"
            }
        }
        bigRemoteViews.setImageViewResource(R.id.iv_icon, icon)
        bigRemoteViews.setTextViewText(R.id.tv_desc, desc)
        bigRemoteViews.setTextViewText(R.id.tv_btn, btn)

        smallRemoteViews.setImageViewResource(R.id.iv_icon, icon)
        smallRemoteViews.setTextViewText(R.id.tv_desc, desc)
        smallRemoteViews.setTextViewText(R.id.tv_btn, btn)

        //取消按钮
        bigRemoteViews.setOnClickPendingIntent(R.id.fl_cancel, cancelNotificationPendingIntent(context, actionId))

        //跳转
        val intent = Intent(context, SplashActivity::class.java)
        intent.putExtra("actionId", actionId)
        val btnRequestCode = Random().nextInt(1000)
        val btnPendingIntent = PendingIntent.getActivity(context, btnRequestCode, intent, PendingIntent.FLAG_IMMUTABLE)
        bigRemoteViews.setOnClickPendingIntent(R.id.fl_btn, btnPendingIntent)
        smallRemoteViews.setOnClickPendingIntent(R.id.tv_btn, btnPendingIntent)


        //构建发送通知
        sendCustomNotification(context, actionId, intent, bigRemoteViews, smallRemoteViews)
    }

    private fun cancelNotificationPendingIntent(context: Context, actionId: Int): PendingIntent {
        val cancelIntent = Intent(context, CloseNotificationReceiver::class.java)
        cancelIntent.setAction(CloseNotificationReceiver.Action)
        cancelIntent.putExtra(CloseNotificationReceiver.NotificationId, actionId)
        val broadcastRequestCode = Random().nextInt(1000)
        return PendingIntent.getBroadcast(context, broadcastRequestCode, cancelIntent, PendingIntent.FLAG_IMMUTABLE)
    }


    private fun sendCustomNotification(
        context: Context,
        actionId: Int,
        intent: Intent,
        bigRemoteViews: RemoteViews,
        smallRemoteViews: RemoteViews
    ) {

        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        //创建channel
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                CHANNEL_ID,
                CHANNEL_NAME,
                NotificationManager.IMPORTANCE_HIGH
            )
            channel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
            notificationManager.createNotificationChannel(channel)
        }


        // Create the notification
        val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, CHANNEL_ID)

        //设置状态栏内的小图标
        val smallIcon = IconCompat.createFromIcon(
            context, Icon.createWithResource(
                context, R.drawable.icon_100
            )
        )
        smallIcon?.let {
            builder.setSmallIcon(smallIcon)
        }
        builder.setContentTitle(context.resources.getString(R.string.app_name))
            .setContentText("notification")

        val requestCode = kotlin.random.Random.nextInt(1000)
        val pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE)
        builder.setContentIntent(pendingIntent)
            .setDeleteIntent(cancelNotificationPendingIntent(context, actionId))
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setAutoCancel(true)


        var small: RemoteViews? = bigRemoteViews
        //Android 12以下需要适配小RemoteViews
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            small = smallRemoteViews
        }
        builder.setContent(small)
        builder.setCustomHeadsUpContentView(small)
        builder.setCustomContentView(small)
        builder.setCustomBigContentView(bigRemoteViews)

        // Show the notification
        if (AppPreferences.getInstance().getInt("actionS", 1) == 1) {
            notificationManager.notify(intent.getIntExtra("actionId", actionId), builder.build())
        } else {
            notificationManager.notify(actionId, builder.build())
        }
        AppPreferences.getInstance().put("last_notification_time", System.currentTimeMillis())
    }
}