package com.simplecleaner.app.ui.cleanresult

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.addCallback
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.simplecleaner.app.R
import com.simplecleaner.app.base.BaseActivity
import com.simplecleaner.app.base.jumpAction
import com.simplecleaner.app.bean.FeatureBean
import com.simplecleaner.app.bean.FeatureBean.Companion.BATTERY_INFO
import com.simplecleaner.app.bean.FeatureBean.Companion.JUNK_CLEAN
import com.simplecleaner.app.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.simplecleaner.app.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.simplecleaner.app.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.simplecleaner.app.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.simplecleaner.app.bean.FeatureBean.Companion.UNINSTALL_APP
import com.simplecleaner.app.business.ads.AdsMgr
import com.simplecleaner.app.business.ads.AdsShowCallBack
import com.simplecleaner.app.databinding.ActivityCleanResultBinding
import com.simplecleaner.app.databinding.ItemCleanResultBinding
import com.simplecleaner.app.utils.MyAnimationUtils
import com.simplecleaner.app.utils.Utils

class CleanResultActivity : BaseActivity<ActivityCleanResultBinding>(ActivityCleanResultBinding::inflate) {

    companion object {
        var total: Long = 0
        var functionKey = ""
        var titleName = ""
    }

    private var list = mutableListOf<FeatureBean>()
    private val adapter by lazy {
        class ViewHolder(val binding: ItemCleanResultBinding) : RecyclerView.ViewHolder(binding.root)

        object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
                val binding = ItemCleanResultBinding.inflate(LayoutInflater.from(parent.context), parent, false)
                return ViewHolder(binding)
            }

            override fun getItemCount(): Int = list.size

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                val item = list[position]
                holder.binding.ivImage.setImageResource(item.icon)
                holder.binding.tvName.text = getString(item.titleRes)
                holder.binding.tvContent.setText(item.content)

                if (position == 0) {
                    holder.binding.tvBtn.background = ContextCompat.getDrawable(this@CleanResultActivity, R.drawable.bg_btn_4)
                } else {
                    holder.binding.tvBtn.background =
                        ContextCompat.getDrawable(this@CleanResultActivity, R.drawable.bg_70eb91_4fe5b6)
                }
                holder.binding.tvBtn.setText(item.content)
                holder.binding.tvBtn.setOnClickListener {
                    jumpAction(item.key)
                }

                val translationX = resources.displayMetrics.widthPixels.toFloat()
                holder.itemView.translationX = -translationX
                MyAnimationUtils.translationX(holder.itemView, -translationX, 0f, 1000, (position + 1) * 300L)

                if (position == 0) {
                    AdsMgr.showNative(holder.binding.flAd, R.layout.layout_admob_native_custom)
                }
            }
        }
    }

    override fun useDefaultImmersive() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            binding.clTop.setPadding(0, systemBars.top, 0, 0)
            insets
        }
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun initView() {
        super.initView()
        binding.rvResult.adapter = adapter
        binding.rvResult.layoutManager = LinearLayoutManager(this)

        if (titleName.isNotEmpty()) binding.tvTitle.text = titleName
        if (total != 0L) {
            binding.tvCleanedUp.text = "${getString(R.string.cleaned_up)} ${Utils.getSizeFormat(total)}"
            total = 0L
        }
        if (functionKey == UNINSTALL_APP) {
            binding.tvCleanedUp.updatePadding(bottom = resources.getDimensionPixelSize(R.dimen.dp_20))
            binding.tvTip.visibility = View.GONE
//            binding.tvCleanedUp.text = getString(R.string.no_threats_found)
        }

        list = getData()
        adapter.notifyDataSetChanged()
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            AdsMgr.showInsert(this@CleanResultActivity, showCallBack = object : AdsShowCallBack() {
                override fun next() {
                    finishToMainTop()
                }
            })
        }
        binding.ivBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }

    }

    private fun getData(): MutableList<FeatureBean> {
        val list = mutableListOf<FeatureBean>().apply {
            add(FeatureBean(JUNK_CLEAN, R.string.quick_clean, R.drawable.icon_clean_r, R.string.clean_now))
            add(FeatureBean(BATTERY_INFO, R.string.battery_info, R.drawable.icon_battery_info_r, R.string.view))
            add(
                FeatureBean(
                    LARGE_FILE_CLEAN,
                    R.string.large_file_clean,
                    R.drawable.icon_large_file_clean_r,
                    R.string.clean_now
                )
            )
            add(FeatureBean(SIMILAR_PHOTOS, R.string.similar_photos, R.drawable.icon_similar_photos_r, R.string.clean_now))
            add(
                FeatureBean(
                    SCREENSHOT_CLEAN,
                    R.string.screenshot_clean,
                    R.drawable.icon_screenshot_r,
                    R.string.clean_now
                )
            )

            add(
                FeatureBean(
                    PHOTO_COMPRESSION,
                    R.string.photo_compression,
                    R.drawable.icon_image_compression_r,
                    R.string.clean_now
                )
            )
            add(
                FeatureBean(
                    UNINSTALL_APP,
                    R.string.uninstall_app,
                    R.drawable.icon_app_uninstall_r,
                    R.string.view
                )
            )

        }

        val filteredList = list.filter { it.key != functionKey }
        val randomIndices = filteredList.indices.shuffled().take(4).toList()
        val data = randomIndices.map { filteredList[it] }
        return data.toMutableList()
    }
}