package com.base.datarecovery.activity.recovery

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import androidx.activity.addCallback
import com.base.datarecovery.adapter.FileFolderAdapter
import com.base.datarecovery.bean.ConstObject.SCAN_DOCUMENTS
import com.base.datarecovery.bean.ConstObject.SCAN_PHOTOS
import com.base.datarecovery.bean.ConstObject.SCAN_VIDEOS
import com.base.datarecovery.bean.FolderBean
import com.base.datarecovery.bean.RecoveryBean
import com.base.datarecovery.bean.RecoveryBean.Companion.setType
import com.base.datarecovery.databinding.ActivityFileScanResultBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.utils.LogEx
import com.google.gson.Gson
import java.io.File
import java.util.HashMap

class FileScanResultActivity : BaseActivity<ActivityFileScanResultBinding>() {

    private val TAG = "FileScanResultActivity"
    private var scanType = 0
    override val binding: ActivityFileScanResultBinding by lazy {
        ActivityFileScanResultBinding.inflate(layoutInflater)
    }

    private lateinit var fileFolderAdapter: FileFolderAdapter

    private val beanList by lazy {
        intent.getStringArrayExtra("PathList") ?: arrayOf()
    }


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

        scanType = intent.extras?.getInt("ScanType") ?: 0

        LogEx.logDebug(TAG, "scanType=$scanType")
        when (scanType) {
            SCAN_PHOTOS -> {
                binding.tvTittle.text = "Photo Recovery"
                binding.tvFileType.text = "Photos"
            }

            SCAN_DOCUMENTS -> {
                binding.tvTittle.text = "Document Recovery"
                binding.tvFileType.text = "Documents"
            }

            SCAN_VIDEOS -> {
                binding.tvTittle.text = "Video Recovery"
                binding.tvFileType.text = "videos"
            }
        }
        binding.tvFileNumber.text = beanList.size.toString()

        fileFolderAdapter = FileFolderAdapter(scanType) { folderBean ->
            startActivity(Intent(this, FileRecoveryActivity::class.java).apply {
                putExtra("ScanType", scanType)
                putExtra("Data", Gson().toJson(folderBean))
            })
        }
        binding.rv.adapter = fileFolderAdapter
        initData()
    }

    override fun initListener() {
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }
    }

    private fun initData() {
        val hashMap = HashMap<String, ArrayList<RecoveryBean>>()
        beanList.forEach { path ->
            val folder = File(path).parent ?: ""
            if (hashMap[folder] == null) {
                hashMap[folder] = arrayListOf()
            }
            hashMap[folder]?.add(RecoveryBean(path))
        }

        val list = hashMap.map {
            val folder = it.key.split("/").last()

            if (scanType == SCAN_DOCUMENTS) {
                it.value.forEach { bean -> bean.setType() }
            }
            FolderBean(folder, recoveryList = it.value)
        }
        binding.tvFolderNumber.text = list.size.toString()
        fileFolderAdapter.setData(list)
    }


}