package com.easy.clean.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.easy.clean.R
import com.easy.clean.SpConstObject
import com.easy.clean.base.BaseActivity
import com.easy.clean.base.jumpAction
import com.easy.clean.bean.FeatureBean
import com.easy.clean.bean.FeatureBean.Companion.BATTERY_INFO
import com.easy.clean.bean.FeatureBean.Companion.JUNK_CLEAN
import com.easy.clean.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.easy.clean.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.easy.clean.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.easy.clean.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.easy.clean.business.ads.AdsMgr
import com.easy.clean.business.ads.AdsShowCallBack
import com.easy.clean.databinding.ActivityCleanResultBinding
import com.easy.clean.databinding.ItemCleanResultBinding
import com.easy.clean.utils.MyAnimationUtils
import com.easy.clean.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.isEmpty()) titleName = getString(R.string.clean)
        binding.llTitle.tvTitle.text = titleName
        binding.tvCleanedUp.text = "${getString(R.string.cleaned_up)} ${Utils.getSizeFormat(total)}"
        SpConstObject.cleanedSize += total

        list = getData()
        adapter.notifyDataSetChanged()
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            AdsMgr.showInsert(this@CleanResultActivity, showCallBack = object : AdsShowCallBack() {
                override fun next() {
                    finishToMainTop()
                }
            })
        }
        binding.llTitle.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()
    }
}