package com.easy.clean.business.push.notification

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
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.easy.clean.R
import com.easy.clean.bean.FeatureBean
import com.easy.clean.ui.batteryinfo.BatteryReceiver
import com.easy.clean.ui.main.MainActivity
import com.easy.clean.ui.splash.SplashActivity
import kotlin.math.roundToInt
import kotlin.random.Random

object BatteryNotification {


    fun customRemoteViews(
        context: Context,
        contentView: RemoteViews,
        expendView: RemoteViews,
    ) {
        val requestCode = Random.Default.nextInt(1800)
        val intent = Intent(context, SplashActivity::class.java).apply {
            putExtra("actionId", FeatureBean.Companion.BATTERY_INFO)
        }
        val pendingIntent =
            PendingIntent.getActivity(
                context,
                requestCode,
                intent,
                PendingIntent.FLAG_IMMUTABLE
            )
        contentView.setOnClickPendingIntent(R.id.tv_open, pendingIntent)
        expendView.setOnClickPendingIntent(R.id.tv_open, pendingIntent)
        //set view
        val percent = BatteryReceiver.Companion.level / BatteryReceiver.Companion.scale.toFloat()
        val power ="${(percent * 100).roundToInt()}%"
        val remainingCapacity = BatteryReceiver.Companion.mAh * percent
        val wTime = (remainingCapacity / 3.0f).toInt()
        if (BatteryReceiver.Companion.isCharging) {
            //充电
            contentView.setTextColor(R.id.tv_status, Color.parseColor("#00E961"))
            contentView.setTextViewText(R.id.tv_status, "Charging")
            contentView.setTextColor(R.id.tv_power, Color.parseColor("#00E961"))
            contentView.setTextViewText(R.id.tv_power, power)

            expendView.setImageViewResource(
                R.id.iv_battery,
                R.drawable.remote_view_battery_charging
            )
            expendView.setTextColor(R.id.tv_status, Color.parseColor("#00E961"))
            expendView.setTextViewText(R.id.tv_status, "Charging")

            expendView.setTextColor(R.id.tv_power, Color.parseColor("#00E961"))
            expendView.setTextViewText(R.id.tv_power, power)
            return
        }

        if (percent > 0.5) {
            contentView.setTextColor(R.id.tv_status, Color.parseColor("#00E961"))
            contentView.setTextViewText(R.id.tv_status, "${wTime / 60}h ${wTime % 60}m")
            contentView.setTextColor(R.id.tv_power, Color.parseColor("#00E961"))
            contentView.setTextViewText(R.id.tv_power, power)

            expendView.setImageViewResource(R.id.iv_battery, R.drawable.remote_view_battery_full)
            expendView.setTextColor(R.id.tv_status, Color.parseColor("#00E961"))
            expendView.setTextViewText(R.id.tv_status, "${wTime / 60}h ${wTime % 60}m")
            expendView.setTextColor(R.id.tv_power, Color.parseColor("#00E961"))
            expendView.setTextViewText(R.id.tv_power, power)
        } else {
            contentView.setTextColor(R.id.tv_status, Color.parseColor("#FF2C2C"))
            contentView.setTextViewText(R.id.tv_status, "Low")
            contentView.setTextColor(R.id.tv_power, Color.parseColor("#FF2C2C"))
            contentView.setTextViewText(R.id.tv_power, power)

            expendView.setImageViewResource(R.id.iv_battery, R.drawable.remote_view_battery_low)
            expendView.setTextColor(R.id.tv_status, Color.parseColor("#FF2C2C"))
            expendView.setTextViewText(R.id.tv_status, "Low")
            expendView.setTextColor(R.id.tv_power, Color.parseColor("#FF2C2C"))
            expendView.setTextViewText(R.id.tv_power, power)
        }
    }


    fun createBatteryNotification(context: Context): Notification {

        val channelName = "Permanent Foreground Service Channel"
        val channelId = "permanent_channel"
        val contentView = RemoteViews(context.packageName, R.layout.layout_battery_stay_small)
        val expendView = RemoteViews(context.packageName, R.layout.layout_battery_stay_large)

        customRemoteViews(context, contentView, expendView)

        val builder = NotificationCompat.Builder(context, channelId)
        val smallIcon = IconCompat.createFromIcon(
            context, Icon.createWithResource(
                context, R.mipmap.ic_launcher
            )
        )

        smallIcon?.let {
            builder.setSmallIcon(smallIcon) //设置状态栏内的小图标
        }

        val nfIntent = Intent(context, MainActivity::class.java)
        val pendingIntent =
            PendingIntent.getActivity(context, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE)

        builder.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher))
        builder.setContentTitle(context.resources.getString(R.string.app_name))
        builder.setContentIntent(pendingIntent) //设置PendingIntent
        builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) //设置通知公开可见
        builder.setAutoCancel(false)
        builder.setOngoing(true)
        builder.setPriority(NotificationCompat.PRIORITY_MAX) //优先级为：重要通知
        builder.setWhen(System.currentTimeMillis())
        builder.setCustomContentView(contentView)
        builder.setCustomBigContentView(expendView)
        builder.setCustomHeadsUpContentView(expendView)
        builder.setNumber(0)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
            channel.lockscreenVisibility = 1
            channel.setShowBadge(false)
            val notificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
            builder.setChannelId(channelId)
        }
        return builder.build()
    }


}