package com.test.basd.onecleanmaster.adapter

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.test.basd.onecleanmaster.R
import com.test.basd.onecleanmaster.bean.AppBean
import com.test.basd.onecleanmaster.databinding.ItemAppRecentBinding
import com.test.basd.onecleanmaster.databinding.ItemAppScreenTimeBinding
import com.test.basd.onecleanmaster.helps.NetworkStatsHelper
import com.test.basd.onecleanmaster.helps.TimeUtils
import com.test.basd.onecleanmaster.view.XmlEx.inflate

class RecentAppAdapter(val activity: Activity, val modeUI: String = UI_LAUNCHES_MODE) : RecyclerView.Adapter<RecentAppAdapter.DDD>() {

    private var appBeans = arrayListOf<AppBean>()
    private var numberMode = UI_MODE_ALL

    inner class DDD(view: View) : ViewHolder(view)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DDD {
        return if (modeUI == UI_LAUNCHES_MODE) {
            DDD(R.layout.item_app_recent.inflate(parent))
        } else {
            DDD(R.layout.item_app_screen_time.inflate(parent))
        }
    }

    override fun getItemCount(): Int {
        return appBeans.size
    }

    @SuppressLint("StringFormatMatches", "SetTextI18n")
    override fun onBindViewHolder(holder: DDD, position: Int) {

        val context = holder.itemView.context
        val data = appBeans[position]

        if (modeUI == UI_LAUNCHES_MODE) {
            val binding = ItemAppRecentBinding.bind(holder.itemView)

            binding.ivIcon.setImageDrawable(data.icIcon)
            binding.tvName.text = data.appName
            val number = when (numberMode) {
                UI_MODE_ALL -> data.launchTimes
                UI_MODE_FOREGROUND -> data.foregroundTimes
                UI_MODE_BACKGROUND -> data.backgroundTimes
                else -> 0
            }
            binding.tvNumber.text = "$number Launches"
            binding.tvStop.background = if (data.isRunning) {
                ContextCompat.getDrawable(context, R.drawable.bg_corners_4772ff_2_5)
            } else {
                ContextCompat.getDrawable(context, R.drawable.bg_corners_bfbfbf)
            }
            binding.tvStop.setOnClickListener {
                if (NetworkStatsHelper.canStop(activity, data.pkg)) {
                    // 跳转到当前应用的应用信息界面
                    val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                    // 如果要跳转到其他应用的应用信息界面，需要传入那个应用的包名
                    intent.setData(Uri.parse("package:${data.pkg}"))
                    context.startActivity(intent)
                }
            }

            val bg = if (NetworkStatsHelper.canStop(activity, data.pkg)) {
                R.drawable.bg_corners_4772ff_2_5
            } else {
                R.drawable.bg_corners_bfbfbf
            }
            binding.tvStop.background = ContextCompat.getDrawable(context, bg)
        }

        if (modeUI == UI_SCREEN_TIME_MODE) {
            val binding = ItemAppScreenTimeBinding.bind(holder.itemView)
            binding.ivIcon.setImageDrawable(data.icIcon)
            binding.tvName.text = data.appName
            binding.tvManage.setOnClickListener {
                // 跳转到当前应用的应用信息界面
                val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                // 如果要跳转到其他应用的应用信息界面，需要传入那个应用的包名
                intent.setData(Uri.parse("package:${data.pkg}"))
                context.startActivity(intent)
            }
            binding.tvTime.text = TimeUtils.longToHoursAndMinutes(data.screenTime)
            val max = appBeans.maxOf { it.screenTime }
            binding.progressBar.progress = ((data.screenTime.toFloat() / max.toFloat()) * 100).toInt()
        }
    }

    fun setNumberMode(mode: Int) {
        numberMode = mode
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(dataList: List<AppBean>) {
        appBeans.clear()
        appBeans.addAll(dataList)
        if (modeUI == UI_LAUNCHES_MODE) {
            when (numberMode) {
                UI_MODE_ALL -> appBeans.sortByDescending { it.launchTimes }
                UI_MODE_FOREGROUND -> appBeans.sortByDescending { it.foregroundTimes }
                UI_MODE_BACKGROUND -> appBeans.sortByDescending { it.backgroundTimes }
            }
        } else {
            appBeans.sortByDescending { it.screenTime }
        }
        notifyDataSetChanged()
    }

    companion object {
        const val UI_MODE_ALL = 5
        const val UI_MODE_FOREGROUND = 25
        const val UI_MODE_BACKGROUND = 35

        const val UI_LAUNCHES_MODE = "LAUNCHES_UI"
        const val UI_SCREEN_TIME_MODE = "SCREEN_TIME_UI"
    }
}