package com.bassbooster.soundeffects.equalizer.utils

import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.view.View
import android.widget.RemoteViews
import androidx.annotation.IdRes
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.bassbooster.equalizer.R

class CustomNotificationService {
    companion object {

        const val EXTRA_BUTTON_CLICKED = "EXTRA_BUTTON_CLICKED"
        const val EXTRA_BUTTON = "EXTRA_BUTTON"
        const val EXTRA_BUTTON_TEXT = "EXTRA_BUTTON_TEXT"
        const val EXTRA_BUTTON_NEXT = "EXTRA_BUTTON_NEXT"
        const val EXTRA_BUTTON_LIGHT = "EXTRA_BUTTON_LIGHT"
        const val EXTRA_BUTTON_OFF = "EXTRA_BUTTON_OFF"
        var nameType = ""
        var bass = 0
        var stereo = 0

        fun createNotificationChannel(context: Context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val name = "My Channel"
                val descriptionText = "My Channel Description"
                val importance = NotificationManager.IMPORTANCE_DEFAULT
                val channel = NotificationChannel("channel_id", name, importance).apply {
                    description = descriptionText
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    // Tạo và đăng ký kênh thông báo nếu bạn đang chạy trên Android 8.0 (API level 26) trở lên
                    channel.setSound(null, null) // Tắt âm thanh
                    channel.enableVibration(false)
                }
                val notificationManager =
                    context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                notificationManager.createNotificationChannel(channel)
            }
        }

        @SuppressLint("MissingPermission")
        fun showBasicNotification(context: Context, text: String, statusOnOff : Boolean = false,isLightOn: Boolean = false) {
            if (text.isNotEmpty()){
                nameType = text
            }
            val notificationLayout = RemoteViews(context.packageName, R.layout.notification_custom)
            notificationLayout.apply {
                if (statusOnOff){
                    setViewVisibility(R.id.mOff,View.VISIBLE)
                    setViewVisibility(R.id.mOn,View.GONE)
                }else{
                    setViewVisibility(R.id.mOff,View.GONE)
                    setViewVisibility(R.id.mOn,View.VISIBLE)
                }
                if(isLightOn){
                    setViewVisibility(R.id.icLightOff,View.GONE)
                    setViewVisibility(R.id.icLight,View.VISIBLE)
                }else{
                    setViewVisibility(R.id.icLightOff,View.VISIBLE)
                    setViewVisibility(R.id.icLight,View.GONE)
                }
                setTextViewText(R.id.mTxtNameBass, "Bass:")
                setTextViewText(R.id.mTxtNameStereo, "Stereo")
                setTextViewText(R.id.textView, nameType)
                setTextViewText(R.id.mTxtStereo, "100")
                setOnClickPendingIntent(
                    R.id.icLeft,
                    onButtonNotificationClick(context = context, id = R.id.icLeft,text=text)
                )
                setOnClickPendingIntent(
                    R.id.icRight,
                    onButtonNotificationClick(context = context, id = R.id.icRight,text=text)
                )
                setOnClickPendingIntent(
                    R.id.icLightOff,
                    onButtonNotificationClick(context = context, id = R.id.icLightOff)
                )
                setOnClickPendingIntent(
                    R.id.icLight,
                    onButtonNotificationClick(context = context, id = R.id.icLight)
                )
                setOnClickPendingIntent(
                    R.id.mOff,
                    onButtonNotificationClick(context = context, id = R.id.mOff)
                )
                setOnClickPendingIntent(
                    R.id.mOn,
                    onButtonNotificationClick(context = context, id = R.id.mOn)
                )
            }


            val builder = NotificationCompat.Builder(context, "channel_id")
                .setSmallIcon(R.drawable.ic_logo)
                .setContentTitle("Tiêu đề thông báo")
                .setContentText("Nội dung thông báo")
                .setOnlyAlertOnce(true)
                .setAutoCancel(true)
                .setOngoing(true)
                .setCustomContentView(notificationLayout)
                .setCustomBigContentView(notificationLayout)
                .setPriority(NotificationCompat.PRIORITY_HIGH)

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val notificationManager =
                    context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

                val channel = NotificationChannel(
                    "channel_id",
                    "channel_name",
                    NotificationManager.IMPORTANCE_DEFAULT
                )
                channel.description = "Channel Equalizer"
                notificationManager.createNotificationChannel(channel)
            }

            val notificationManager = NotificationManagerCompat.from(context)
            notificationManager.notify(0, builder.build())
        }

        private fun onButtonNotificationClick(context: Context, @IdRes id: Int,text: String? = null): PendingIntent? {
            val intent: Intent = Intent(EXTRA_BUTTON_CLICKED)
            intent.putExtra(EXTRA_BUTTON, id)
            intent.putExtra(EXTRA_BUTTON_TEXT,text)
            // Sử dụng FLAG_MUTABLE nếu bạn muốn thay đổi PendingIntent sau khi đã tạo nó.
            // Sử dụng FLAG_IMMUTABLE nếu bạn không cần thay đổi PendingIntent sau khi đã tạo nó.
            val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                PendingIntent.FLAG_MUTABLE
            } else {
                PendingIntent.FLAG_UPDATE_CURRENT
            }
            return PendingIntent.getBroadcast(
                context,
                id,
                intent,
                flags
            )
        }
    }
}

open class MyBroadcastReceiver : BroadcastReceiver() {

    @RequiresApi(Build.VERSION_CODES.N)
    override fun onReceive(context: Context?, intent: Intent?) {
    }

}
