package com.tgv.malware.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.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.tgv.malware.R
import com.tgv.malware.base.BaseActivity
import com.tgv.malware.base.jumpAction
import com.tgv.malware.bean.FeatureBean
import com.tgv.malware.bean.FeatureBean.Companion.ANTIVIRUS
import com.tgv.malware.bean.FeatureBean.Companion.BATTERY_INFO
import com.tgv.malware.bean.FeatureBean.Companion.JUNK_CLEAN
import com.tgv.malware.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.tgv.malware.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.tgv.malware.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.tgv.malware.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.tgv.malware.business.ads.AdsMgr
import com.tgv.malware.business.ads.AdsShowCallBack
import com.tgv.malware.databinding.ActivityCleanResultBinding
import com.tgv.malware.databinding.ItemCleanResultBinding
import com.tgv.malware.ui.malware.MalwareDialog
import com.tgv.malware.utils.MyAnimationUtils
import com.tgv.malware.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.tvTry.setOnClickListener {
                    if (item.key == ANTIVIRUS) {
                        val dialog = MalwareDialog(this@CleanResultActivity)
                        dialog.action={
                            jumpAction(item.key)
                        }
                        dialog.showDialog()
                    } else {
                        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 == ANTIVIRUS) {
            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.junk_clean, R.drawable.icon_clean_r, R.string.result_junk_clean))
            add(FeatureBean(ANTIVIRUS, R.string.antivirus, R.drawable.icon_antivirus_r, R.string.result_antivirus))
            add(
                FeatureBean(
                    SCREENSHOT_CLEAN,
                    R.string.screenshot_clean,
                    R.drawable.icon_screenshot_r,
                    R.string.result_screenshot_clean
                )
            )
            add(
                FeatureBean(
                    LARGE_FILE_CLEAN,
                    R.string.large_file_clean,
                    R.drawable.icon_large_file_clean_r,
                    R.string.result_large_file_clean
                )
            )
            add(FeatureBean(SIMILAR_PHOTOS, R.string.similar_photos, R.drawable.icon_similar_photos_r, R.string.result_similar_photos))
            add(
                FeatureBean(
                    PHOTO_COMPRESSION,
                    R.string.photo_compression,
                    R.drawable.icon_image_compression_r,
                    R.string.result_photo_compression
                )
            )
            add(FeatureBean(BATTERY_INFO, R.string.battery_info, R.drawable.icon_battery_info_r, R.string.result_battery_info))

        }

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