package com.test.basd.fastcleanerjunk.activity

import android.annotation.SuppressLint
import android.os.Environment
import androidx.lifecycle.lifecycleScope
import com.ironsource.mediationsdk.f
import com.test.basd.fastcleanerjunk.adapter.ScreenShotAdapter
import com.test.basd.fastcleanerjunk.bean.ScreenPhotoBean
import com.test.basd.fastcleanerjunk.bean.ScreenshotBean
import com.test.basd.fastcleanerjunk.databinding.ActivityScreenShotBinding
import com.test.basd.fastcleanerjunk.helps.BaseActivity
import com.test.basd.fastcleanerjunk.helps.KotlinExt.toFormatSize
import com.test.basd.fastcleanerjunk.view.FileDeleteDialog.showFileDeleteDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import okhttp3.internal.wait
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale
import kotlin.collections.HashSet

class ScreenShotActivity : BaseActivity<ActivityScreenShotBinding>() {

    private lateinit var screenShotAdapter: ScreenShotAdapter

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

    override fun initView() {
        screenShotAdapter = ScreenShotAdapter { size ->
            setCleanUpUi(size)
        }
        binding.rv.adapter = screenShotAdapter

        initData()
    }

    @SuppressLint("SetTextI18n")

    private fun setCleanUpUi(size: Int) {
        if (size > 0) {
            binding.tvClean.isEnabled = true
            binding.tvClean.text = "CLEAN(${size})"
        } else {
            binding.tvClean.isEnabled = false
            binding.tvClean.text = "CLEAN"
        }
    }

    private fun initData() {
        val list = getScreenshotBean()
        screenShotAdapter.setData(list)
        val size = list.sumOf { it.screenPhotoBean.sumOf { ss -> ss.file.length() } }
        val sizeF = size.toFormatSize().split(" ")
        binding.tvSize.text = sizeF[0]
        binding.tvUnit.text = sizeF[1]
    }

    override fun initListener() {
        binding.flBack.setOnClickListener {
            finishToMain()
        }
        binding.llSelectAll.setOnClickListener {
            binding.llSelectAll.isSelected = !binding.llSelectAll.isSelected
            screenShotAdapter.toggleSelect(binding.llSelectAll.isSelected)
            val size = screenShotAdapter.getSelectDataSize()
            setCleanUpUi(size)
        }
        binding.tvClean.setOnClickListener {
            val files = screenShotAdapter.getSelectData()
            showFileDeleteDialog {
                lifecycleScope.launch(Dispatchers.Main) {
                    async {
                        files.forEach { file ->
                            runCatching {
                                if (file.exists()) file.delete()
                            }
                        }
                    }.wait()
                }
                screenShotAdapter.removeData(files)
            }

        }
    }

    private fun getScreenshotBean(): ArrayList<ScreenshotBean> {
        val dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
        val screenFile = File(dcim, "Screenshots")
        val hash = HashMap<String, HashSet<File>>()
        val formatter = SimpleDateFormat("yyyy/MM", Locale.getDefault())

        if (screenFile.exists()) {
            screenFile.listFiles()?.forEach { file ->
                val time = formatter.format(file.lastModified())
                if (hash[time] == null) {
                    hash[time] = HashSet()
                }
                val set = hash[time]
                set?.add(file)
            }
        }

        val beans = arrayListOf<ScreenshotBean>()
        hash.forEach { (t, u) ->
            beans.add(ScreenshotBean(t, u.map { file -> ScreenPhotoBean(file) }))
        }
        return beans
    }
}