package com.base.appzxhy.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.base.appzxhy.R
import com.base.appzxhy.base.BaseActivity
import com.base.appzxhy.base.jumpAction
import com.base.appzxhy.bean.FeatureBean
import com.base.appzxhy.bean.FeatureBean.Companion.BATTERY_INFO
import com.base.appzxhy.bean.FeatureBean.Companion.JUNK_CLEAN
import com.base.appzxhy.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.base.appzxhy.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.base.appzxhy.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.base.appzxhy.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.base.appzxhy.business.ads.AdsMgr
import com.base.appzxhy.business.ads.AdsShowCallBack
import com.base.appzxhy.databinding.ActivityCleanResultBinding
import com.base.appzxhy.databinding.ItemCleanResultBinding
import com.base.appzxhy.utils.MyAnimationUtils
import com.base.appzxhy.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() {
        setStatusBarColor()
        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.clean_junk, R.drawable.icon_clean_clean_result, R.string.result_junk_clean))
            add(FeatureBean(BATTERY_INFO, R.string.battery_info, R.drawable.icon_battery_clean_result, R.string.result_battery_info))
            add(FeatureBean(LARGE_FILE_CLEAN, R.string.large_file_clean, R.drawable.icon_file_clean_result, R.string.result_large_file_clean))
            add(
                FeatureBean(
                    PHOTO_COMPRESSION,
                    R.string.Compress_Photos,
                    R.drawable.icon_compress,
                    R.string.result_photo_compression
                )
            )
            add(FeatureBean(SCREENSHOT_CLEAN, R.string.screenshot_clean, R.drawable.icon_screenshot_2, R.string.result_screenshot_clean))
            add(FeatureBean(SIMILAR_PHOTOS, R.string.similar_photos, R.drawable.icon_similar_photos_clean_result, 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()
    }
}