package com.base.datarecovery.activity.whatsapp

import android.content.Intent
import android.graphics.Color
import androidx.activity.addCallback
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.base.datarecovery.activity.ResultActivity
import com.base.datarecovery.adapter.WhatsAppMediaAdapter
import com.base.datarecovery.ads.admob.AdmobInterstitialUtils
import com.base.datarecovery.ads.admob.AdmobNativeUtils
import com.base.datarecovery.bean.ConstObject
import com.base.datarecovery.bean.MediaBean
import com.base.datarecovery.databinding.ActivityWhatsAppMessageCleanBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.KotlinExt.toFormatSize
import com.base.datarecovery.utils.BarUtils
import com.google.gson.Gson
import java.io.File

class WhatsAppMessageCleanActivity : BaseActivity<ActivityWhatsAppMessageCleanBinding>() {

    private val pathList by lazy {
        val json = intent.extras?.getString("PathList")
        val list = Gson().fromJson(json, Array<String>::class.java)
        list.map { MediaBean(it) }
    }
    private lateinit var adapter: WhatsAppMediaAdapter

    override val binding: ActivityWhatsAppMessageCleanBinding by lazy {
        ActivityWhatsAppMessageCleanBinding.inflate(layoutInflater)
    }


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)

        val tittle = intent.extras?.getString("Tittle") ?: ""
        binding.tvTitle.text = tittle

        var isList = false
        when (tittle) {
            "Audio Messages" -> {
                binding.rv.layoutManager = LinearLayoutManager(this)
                isList = true
            }

            else -> {
                binding.rv.layoutManager = GridLayoutManager(this, 3)
            }
        }

        adapter = WhatsAppMediaAdapter(isList) {
            binding.llSelectAll.isSelected = it
            binding.tvClean.isEnabled = adapter.getSelectData().isNotEmpty()
            val split = adapter.getSelectDataSize().toFormatSize().split(" ")
            binding.tvSize.text = split[0]
            binding.tvUnit.text = split[1]
        }
        binding.rv.adapter = adapter
        adapter.setData(pathList)

        AdmobNativeUtils.showNativeAd(this, binding.flAd)
    }

    override fun initListener() {
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            AdmobInterstitialUtils.showInterstitialAd(this@WhatsAppMessageCleanActivity) {
                finishToMain()
            }
        }
        binding.llSelectAll.setOnClickListener {
            it.isSelected = !it.isSelected
            adapter.toggleSelect(it.isSelected)
            binding.tvClean.isEnabled = it.isSelected
            val split = adapter.getSelectDataSize().toFormatSize().split(" ")
            binding.tvSize.text = split[0]
            binding.tvUnit.text = split[1]
        }

        binding.tvClean.setOnClickListener {

            AdmobInterstitialUtils.showInterstitialAd(this) {
                val list = adapter.getSelectData()
                runCatching {
                    list.forEach { File(it).delete() }
                }
                startActivity(Intent(this@WhatsAppMessageCleanActivity, ResultActivity::class.java).apply {
                    putExtra("from", ConstObject.WHATSAPP_CLEANER)
                })
                finish()
            }
        }
    }


}