package com.base.filerecoveryrecyclebin.activity.whatsapp

import android.graphics.Color
import androidx.activity.addCallback
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.base.filerecoveryrecyclebin.adapter.WhatsAppMediaAdapter
import com.base.filerecoveryrecyclebin.ads.AdmobMaxHelper
import com.base.filerecoveryrecyclebin.ads.admob.AdmobInterstitialUtils
import com.base.filerecoveryrecyclebin.ads.admob.AdmobNativeUtils
import com.base.filerecoveryrecyclebin.bean.MediaBean
import com.base.filerecoveryrecyclebin.databinding.ActivityWhatsAppMessageCleanBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatSize
import com.base.filerecoveryrecyclebin.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)

    }

    override fun initListener() {
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            AdmobMaxHelper.admobMaxShowInterstitialAd(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() }
                }
                finish()
            }
        }
    }


}