package com.base.datarecovery.activity

import android.annotation.SuppressLint
import android.graphics.BitmapFactory
import android.graphics.Color
import android.view.View
import androidx.activity.addCallback
import com.base.datarecovery.adapter.FileMediaColumnsAdapter
import com.base.datarecovery.bean.ConstObject
import com.base.datarecovery.bean.FolderBean
import com.base.datarecovery.bean.RecoveryBean
import com.base.datarecovery.databinding.ActivityFileRecoveryBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.KotlinExt.toFormatTime
import com.base.datarecovery.utils.BarUtils
import com.google.gson.Gson
import java.io.File

class FileRecoveryActivity : BaseActivity<ActivityFileRecoveryBinding>() {

    private var scanType = 0
    override val binding: ActivityFileRecoveryBinding by lazy {
        ActivityFileRecoveryBinding.inflate(layoutInflater)
    }

    private var folderBean: FolderBean? = null
    private lateinit var adapter: FileMediaColumnsAdapter

    @SuppressLint("SetTextI18n")
    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)

        scanType = intent.extras?.getInt("ScanType") ?: 0
        val json = intent.extras?.getString("Data") ?: ""
        folderBean = Gson().fromJson(json, FolderBean::class.java)

        when (scanType) {
            ConstObject.SCAN_PHOTOS -> {
                var size = 0
                folderBean?.recoveryList?.forEach {
                    val bitmap = BitmapFactory.decodeFile(it.path)
                    if (bitmap.height < 256 || bitmap.width < 256) {
                        size++
                        it.isThumbnails = true
                    }
                }
                binding.tvThumbnails.text = "Hide thumbnails (${size})"
            }

            ConstObject.SCAN_DOCUMENTS -> {
                binding.clThumbnails.visibility = View.GONE
            }

            ConstObject.SCAN_VIDEOS -> {
                binding.clThumbnails.visibility = View.GONE
            }


        }
        adapter = FileMediaColumnsAdapter(2,
            select = { all, size ->
                binding.ivSelectAll.isSelected = all
                binding.tvRecover.text = "Recover($size)"
                binding.tvRecover.isEnabled = size > 0
            })
        binding.rv.adapter = adapter

        initData()

    }

    override fun initListener() {

        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }
        binding.switchThumbnails.setOnCheckedChangeListener { buttonView, isChecked ->
            adapter.toggleThumbnails(isChecked)
        }
        binding.ivSelectAll.setOnClickListener {
            binding.ivSelectAll.isSelected = !binding.ivSelectAll.isSelected
            adapter.toggleAllSelect(binding.ivSelectAll.isSelected)
        }
    }

    private fun initData() {


        val hash = HashMap<String, ArrayList<RecoveryBean>>()

        folderBean?.let {
            it.recoveryList.forEach { recoveryBean ->
                val file = File(recoveryBean.path)
                val time = file.lastModified().toFormatTime()
                if (hash[time].isNullOrEmpty()) {
                    hash[time] = arrayListOf()
                }
                hash[time]?.add(recoveryBean)
            }
        }
        val list = arrayListOf<FolderBean>()
        hash.forEach { (key, value) ->
            list.add(FolderBean(time = key, recoveryList = value))
        }
        adapter.setData(list)
    }

}