package com.base.scanqrclear.luma

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.addCallback
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.FileBean
import com.base.scanqrclear.databinding.ActivityWhatsappCleanDetailBinding

class WhatsappCleanDetailActivity : BaseActivity2() {

    companion object {
        var list = listOf<FileBean>()
        var type = 0
        var titleName = ""
        fun start(context: Context, list: List<FileBean>, type: Int, titleName: String) {
            Companion.list = list
            Companion.type = type
            Companion.titleName = titleName
            val intent = Intent(context, WhatsappCleanDetailActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val binding by lazy {
        ActivityWhatsappCleanDetailBinding.inflate(layoutInflater)
    }

    private lateinit var adapter: CleanDetailAdapter
    private var total: Long = 0
    private var isSelectAll = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initView()
        onBackPressedDispatcher.addCallback {
            handleBackPressed(this@WhatsappCleanDetailActivity)
        }
    }

    private fun initView() {
        adapter = CleanDetailAdapter(list, type)
        adapter.callback = object : BaseAdapter.OnClickCallback<FileBean> {
            override fun onClicked(view: View, position: Int, item: FileBean) {
                if (item.isSelected) total += item.length
                else total -= item.length
                isSelectAll = adapter.list.stream().allMatch(FileBean::isSelected)
                updateView(total > 0)
            }
        }
        binding.rvWhatsapp.adapter = adapter
        binding.rvWhatsapp.layoutManager = if (type == 2) {
            LinearLayoutManager(this)
        } else {
            GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false)
        }
        if (titleName.isNotEmpty()) binding.tvTitle.text = titleName
        binding.flBack.setOnClickListener { backPressed() }
        binding.llSelectAll.setOnClickListener {
            isSelectAll = !isSelectAll
            val list = adapter.list
            list.forEach { it.isSelected = isSelectAll }
            adapter.submitList(list)
            total = list.filter { it.isSelected }.sumOf { it.length }
            updateView(isSelectAll)
        }
        binding.tvDelete.setOnClickListener {
            if (total <= 0) return@setOnClickListener
            DialogHelps.showDeleteDialog(this, binding.tvDelete) {
                showAnimationAd {
                    toCleanResult(total, getString(R.string.whatsapp_clean))
                }
                cleanFiles(adapter.list)
            }
        }
    }

    private fun updateView(isSelection: Boolean) {
        binding.ivSelectAll.setImageResource(if (isSelectAll) R.mipmap.ic_check_s else R.mipmap.ic_check_n)
        binding.tvDelete.setBackgroundResource(if (isSelection) R.drawable.gradient_26 else R.drawable.btn_not_clickable)
        val value = Utils.getSizeArray(total)
        binding.tvSize.text = value[0]
        binding.tvUnit.text = value[1]
    }
}