package com.dumpster.cleaner.ui.photocompression

import android.view.View
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.dumpster.cleaner.R
import com.dumpster.cleaner.base.BaseActivity
import com.dumpster.cleaner.base.LottieEnum
import com.dumpster.cleaner.bean.FileBean
import com.dumpster.cleaner.business.ads.AdsMgr
import com.dumpster.cleaner.databinding.ActivityPhotoCompressionBinding
import com.dumpster.cleaner.utils.MediaUtils
import com.dumpster.cleaner.utils.MineTypeUtils.TYPE_IMAGE
import com.dumpster.cleaner.utils.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class PhotoCompressionActivity : BaseActivity<ActivityPhotoCompressionBinding>(ActivityPhotoCompressionBinding::inflate) {

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

    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
        }
    }

    override fun initView() {
        super.initView()
        adapter = CleanDetailAdapter(TYPE_IMAGE) {
            if (it.isSelected) total += it.length
            else total -= it.length
            isSelectAll = adapter.list.stream().allMatch(FileBean::isSelected)
            updateView(total > 0)
        }
        binding.rvCompress.adapter = adapter
        binding.rvCompress.layoutManager = GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false)
        initData()

        showAdAnimation(LottieEnum.PHOTO_COMPRESSION_LOTTIE) {
            AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)
        }
    }

    private fun initData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val images = async { MediaUtils.findImageFiles(this@PhotoCompressionActivity) }.await()
            withContext(Dispatchers.Main) {
                adapter.submitList(images)
                binding.ivEmpty.visibility = if (images.isEmpty()) View.VISIBLE else View.GONE
                binding.llBottom.visibility = if (images.isEmpty()) View.GONE else View.VISIBLE
            }
        }
    }

    private fun updateView(isSelection: Boolean) {
        binding.ivSelectAll.isSelected = isSelectAll
        binding.tvCompress.isEnabled = isSelection
        setTextSize(binding.tvCompress, getString(R.string.compress), total)
    }

    fun setTextSize(view: TextView, content: String, total: Long) {
        view.text = if (total > 0) "$content (${Utils.getSizeFormat(total)})" else content
    }

    override fun initListener() {
        super.initListener()

        binding.ivBack.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
        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.tvCompress.setOnClickListener {
            val list = adapter.list.filter { it.isSelected }.toMutableList()
            if (list.isEmpty()) return@setOnClickListener
            PhotoCompressionDetailActivity.list = list
            goToAc(PhotoCompressionDetailActivity::class.java)
        }
    }

}