package com.base.scanqrclear.fcm.notification

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.scanqrclear.MyApplication
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.NotificationSendBean
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_APP_PROCESS
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_CLEAN_JUNK
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_CONSTELLATION
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_EMAIL
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_SCAN
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_SCREEN_SHORT
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_SIMILAR
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_TEXT
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_WEATHER
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_WEBSITE
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_WHATSAPP
import com.base.scanqrclear.bean.NotificationSendBean.Companion.ACTION_ID_WIFI
import com.base.scanqrclear.bean.WeatherBean
import com.base.scanqrclear.luma.WeatherUtils
import com.base.scanqrclear.ui.start.StartActivity
import com.base.scanqrclear.utils.TimeUtils.formatATime
import com.base.scanqrclear.utils.TimeUtils.isDayOrNight
import kotlin.random.Random

/**
 * 发送通知UI部分
 */
object NotificationUiUtil {

    private val CHANNEL_ID = "San Qr Channel"
    private val CHANNEL_NAME = "San Qr Channel NAME"

    fun setNotification(sendBean: NotificationSendBean) {
        when (sendBean.actionId) {
            ACTION_ID_SCAN -> {
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_big_scan)
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_small_scan)

                if (sendBean.valueMap.isNotEmpty()) {
                    val value = sendBean.valueMap["AM"]
                    val string = (value as String?) ?: "Scan today's product information."
                    bigRemoteViews.setTextViewText(R.id.tvDesc, string)
                    smallRemoteViews.setTextViewText(R.id.tvDesc, string)
                }
                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_WIFI -> {
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_big_function)
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_small_function)

                bigRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_wifi)
                bigRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own wifi QR cord and share it!")

                smallRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_wifi)
                smallRemoteViews.setTextViewText(R.id.tvTitle, "Create wifi QR")
                smallRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own wifi QR cord and share it!")

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_TEXT -> {
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_big_function)
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_small_function)

                bigRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_text)
                bigRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own text QR cord and share it!")

                smallRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_text)
                smallRemoteViews.setTextViewText(R.id.tvTitle, "Create text QR")
                smallRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own text QR cord and share it!")
                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_EMAIL -> {
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_big_function)
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_small_function)

                bigRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_email)
                bigRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own email QR cord and share it!")

                smallRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_email)
                smallRemoteViews.setTextViewText(R.id.tvTitle, "Create email QR")
                smallRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own email QR cord and share it!")

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_WEBSITE -> {
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_big_function)
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.stay_notification_small_function)

                bigRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_website)
                bigRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own website QR cord and share it!")

                smallRemoteViews.setImageViewResource(R.id.ivIcon, R.mipmap.h_email)
                smallRemoteViews.setTextViewText(R.id.tvTitle, "Create website QR")
                smallRemoteViews.setTextViewText(R.id.tvDesc, "Try to create your own website QR cord and share it!")

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_CLEAN_JUNK -> {
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_common_small)
                smallRemoteViews.setTextViewText(R.id.tv_content, MyApplication.appContext.getString(R.string.clean_junk_pop))
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_common_large)
                bigRemoteViews.setTextViewText(R.id.tv_content, MyApplication.appContext.getString(R.string.clean_junk_pop))
                bigRemoteViews.setImageViewResource(R.id.iv_image, R.mipmap.icon_junk_scan)

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_APP_PROCESS -> {
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_common_small)
                smallRemoteViews.setTextViewText(R.id.tv_content, MyApplication.appContext.getString(R.string.app_process_pop))
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_common_large)
                bigRemoteViews.setTextViewText(R.id.tv_content, MyApplication.appContext.getString(R.string.app_process_pop))
                bigRemoteViews.setImageViewResource(R.id.iv_image, R.mipmap.icon_app_t)

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_WHATSAPP -> {
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_common_small)
                smallRemoteViews.setTextViewText(R.id.tv_content, MyApplication.appContext.getString(R.string.whatsapp_pop))
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_common_large)
                bigRemoteViews.setTextViewText(R.id.tv_content, MyApplication.appContext.getString(R.string.whatsapp_pop))
                bigRemoteViews.setImageViewResource(R.id.iv_image, R.mipmap.icon_whatsapp_clean_t)

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_SCREEN_SHORT -> {
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_common_small)
                smallRemoteViews.setTextViewText(R.id.tv_content, MyApplication.appContext.getString(R.string.screen_pop))
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_common_large)
                bigRemoteViews.setTextViewText(R.id.tv_content, MyApplication.appContext.getString(R.string.screen_pop))
                bigRemoteViews.setImageViewResource(R.id.iv_image, R.mipmap.icon_screenshot_1)

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_SIMILAR -> {
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_common_small)
                smallRemoteViews.setTextViewText(R.id.tv_content, MyApplication.appContext.getString(R.string.similar_pop))
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_common_large)
                bigRemoteViews.setTextViewText(R.id.tv_content, MyApplication.appContext.getString(R.string.similar_pop))
                bigRemoteViews.setImageViewResource(R.id.iv_image, R.mipmap.icon_similar)

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }

            ACTION_ID_CONSTELLATION -> {
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_constellation_small)
                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_constellation_big)

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }


            ACTION_ID_WEATHER -> {
                val weatherBean: WeatherBean = WeatherUtils.getWeatherEntity() ?: return
                if (weatherBean.list.isEmpty()) return

                val subBean = weatherBean.list[0]
                val smallRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_weather_small)
                smallRemoteViews.setTextViewText(R.id.tv_city, weatherBean.city)
                val min = subBean.tempMin.toInt()
                var max = subBean.tempMax.toInt()
                if (max <= min) {
                    max = min + 1
                }
                smallRemoteViews.setTextViewText(R.id.tv_wendu_now, Random.nextInt(min, max).toString())
                val icon = when (WeatherUtils.getWeatherType(subBean.iconDay.toInt())) {
                    "Sunny day" -> R.mipmap.d_qing
                    "Cloudy day" -> R.mipmap.d_yin
                    "Rainy day" -> R.mipmap.d_dayu
                    "Snowy day" -> R.mipmap.d_xiaxue
                    "Greasy day" -> R.mipmap.d_wumaishachengbao
                    "Unknown" -> R.mipmap.d_qing
                    else -> R.mipmap.d_qing
                }

                smallRemoteViews.setImageViewResource(R.id.iv_weather, icon)
                smallRemoteViews.setTextViewText(R.id.tv_wendu_range, "$min℃/$max℃")

                val bigRemoteViews = RemoteViews(MyApplication.appContext.packageName, R.layout.notification_weather_big)
                bigRemoteViews.setImageViewResource(R.id.iv_weather_11h, icon)
                bigRemoteViews.setImageViewResource(R.id.iv_weather_14h, icon)
                bigRemoteViews.setImageViewResource(R.id.iv_weather_17h, icon)
                bigRemoteViews.setImageViewResource(R.id.iv_weather_20h, icon)
                bigRemoteViews.setImageViewResource(R.id.iv_weather_23h, icon)
                bigRemoteViews.setImageViewResource(R.id.iv_weather, icon)

                bigRemoteViews.setTextViewText(R.id.tv_wendu_11h, Random.nextInt(min, max).toString())
                bigRemoteViews.setTextViewText(R.id.tv_wendu_14h, Random.nextInt(min, max).toString())
                bigRemoteViews.setTextViewText(R.id.tv_wendu_17h, Random.nextInt(min, max).toString())
                bigRemoteViews.setTextViewText(R.id.tv_wendu_20h, Random.nextInt(min, max).toString())
                bigRemoteViews.setTextViewText(R.id.tv_wendu_23h, Random.nextInt(min, max).toString())

                bigRemoteViews.setTextViewText(R.id.tv_wendu_now, Random.nextInt(min, max).toString())
                bigRemoteViews.setTextViewText(R.id.tv_day_desc, if (isDayOrNight()) subBean.textDay else subBean.textNight)
                bigRemoteViews.setTextViewText(R.id.tv_weather_info, "$min℃/$max℃ ${formatATime()}")

                sendBean.bigRemoteViews = bigRemoteViews
                sendBean.smallRemoteViews = smallRemoteViews
            }


        }

        val intent = Intent(sendBean.context, StartActivity::class.java)
        intent.putExtra("actionId", sendBean.actionId)
        sendBean.intent = intent

        sendCustomNotification(sendBean)
    }

    private fun sendCustomNotification(
        sendBean: NotificationSendBean
    ) {

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

        //创建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
            channel.enableVibration(false)
            channel.vibrationPattern = longArrayOf(0)
            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.log_svg
            )
        )
        smallIcon?.let {
            builder.setSmallIcon(smallIcon)
        }
        builder.setContentTitle(sendBean.context.resources.getString(R.string.app_name))
//            .setContentText("notification")

        val requestCode = Random.nextInt(1000)
        val pendingIntent = PendingIntent.getActivity(context, requestCode, sendBean.intent, PendingIntent.FLAG_IMMUTABLE)
        builder.setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setAutoCancel(true)

        var small: RemoteViews? = sendBean.bigRemoteViews
        //Android 12以下需要适配小RemoteViews
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            small = sendBean.smallRemoteViews
        }
        // 设置小视图
        sendBean.smallRemoteViews?.let { builder.setCustomContentView(it) }

        // 设置悬浮通知视图（Android 12 及以上）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            sendBean.smallRemoteViews.let { builder.setCustomHeadsUpContentView(it) }
        }

        // 设置大视图
        sendBean.bigRemoteViews?.let { builder.setCustomBigContentView(it) }

        notificationManager.notify(sendBean.notificationId, builder.build())
    }


}