package com.dumpster.cleaner.ui.cleanresult

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.activity.addCallback
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.dumpster.cleaner.R
import com.dumpster.cleaner.base.BaseActivity
import com.dumpster.cleaner.base.jumpAction
import com.dumpster.cleaner.bean.FeatureBean
import com.dumpster.cleaner.bean.FeatureBean.Companion.BATTERY_INFO
import com.dumpster.cleaner.bean.FeatureBean.Companion.JUNK_CLEAN
import com.dumpster.cleaner.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.dumpster.cleaner.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.dumpster.cleaner.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.dumpster.cleaner.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.dumpster.cleaner.business.ads.AdsMgr
import com.dumpster.cleaner.business.ads.AdsShowCallBack
import com.dumpster.cleaner.databinding.ActivityCleanResultBinding
import com.dumpster.cleaner.databinding.ItemCleanResultBinding
import com.dumpster.cleaner.utils.MyAnimationUtils
import com.dumpster.cleaner.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)
                holder.binding.tvCleanNow.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
        binding.tvCleanedUp.text = "${getString(R.string.cleaned_up)} ${Utils.getSizeFormat(total)}"

        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.junk_clean, R.drawable.h_junkclean, R.string.result_junk_clean))
            add(FeatureBean(BATTERY_INFO, R.string.battery_info, R.drawable.h_battery, R.string.result_battery_info))
            add(FeatureBean(LARGE_FILE_CLEAN, R.string.large_file_clean, R.drawable.h_lagre, R.string.result_large_file_clean))
            add(
                FeatureBean(
                    PHOTO_COMPRESSION,
                    R.string.photo_compression,
                    R.drawable.h_compression,
                    R.string.result_photo_compression
                )
            )
            add(FeatureBean(SCREENSHOT_CLEAN, R.string.screenshot_clean, R.drawable.h_screenshot, R.string.result_screenshot_clean))
            add(FeatureBean(SIMILAR_PHOTOS, R.string.similar_photos, R.drawable.h_similar, R.string.result_similar_photos))
        }

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