package com.base.datarecovery.activity.privacyspace

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Environment
import android.view.View
import android.widget.Toast
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.adapter.MediaSubAdapter
import com.base.datarecovery.bean.MediaBean
import com.base.datarecovery.databinding.ActivityPrivacyManageBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.FileHelp.loadFileByFilter
import com.base.datarecovery.help.MediaStoreHelp.updateMediaStore
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.utils.LogEx
import com.base.datarecovery.view.DialogViews.showDeletePermanentlyDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class PrivacyManageActivity : BaseActivity<ActivityPrivacyManageBinding>() {

    private val TAG = "PrivacyManageActivity"
    private var mediaType: String = ""
    private lateinit var mediaSubAdapter: MediaSubAdapter

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


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        mediaType = intent?.extras?.getString("mediaType") ?: ""


        mediaSubAdapter = MediaSubAdapter()
        binding.rv.adapter = mediaSubAdapter
        initData()
    }

    @SuppressLint("SetTextI18n")
    override fun initListener() {

        binding.llDownload.setOnClickListener {
            downloadCopy()
        }
        binding.llDelete.setOnClickListener {
            deleteFilesDirMedia()
        }
        binding.tvSelectAll.setOnClickListener {
            binding.tvSelectAll.isSelected = !binding.tvSelectAll.isSelected
            mediaSubAdapter.toggleSelect(binding.tvSelectAll.isSelected)
            if (binding.tvSelectAll.isSelected) {
                binding.tvSelectAll.text = "Unselect All"
            } else {
                binding.tvSelectAll.text = "Select All"
            }
        }
    }

    private fun deleteFilesDirMedia() {
        showDeletePermanentlyDialog {
            lifecycleScope.launch(Dispatchers.IO) {
                val list = mediaSubAdapter.getSelectData()
                list.forEach {
                    runCatching { File(it.path).delete() }
                }
                launch(Dispatchers.Main) {
                    Toast.makeText(this@PrivacyManageActivity, "success", Toast.LENGTH_SHORT).show()
                    mediaSubAdapter.removeData(list)
                }
            }
        }
    }

    private fun downloadCopy() = lifecycleScope.launch(Dispatchers.IO) {
        mediaSubAdapter.getSelectData().forEach { bean ->
            runCatching {
                val file = File(bean.path)
                val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                file.copyTo(File(dir, file.name))
            }
        }
        this@PrivacyManageActivity.updateMediaStore()
        launch(Dispatchers.Main) {
            Toast.makeText(this@PrivacyManageActivity, "success", Toast.LENGTH_SHORT).show()
            mediaSubAdapter.toggleSelect(false)
        }
    }

    private fun initData() {
        val dir = getFilesMediaDir()
        if (!dir.exists()) {
            dir.mkdirs()
        }
        val list = arrayListOf<MediaBean>()

        lifecycleScope.loadFileByFilter(folder = dir, filter = { true }, onDo = {
            LogEx.logDebug(TAG, "$it")
            list.add(MediaBean(it.absolutePath))
        }, onFinish = {
            if (list.isNotEmpty()) {
                binding.rv.visibility = View.VISIBLE
                mediaSubAdapter.setData(list)
            }
        })
    }

    private fun getFilesMediaDir(): File {
        return if (mediaType == "Photos") {
            File(filesDir, "photos")
        } else {
            File(filesDir, "videos")
        }
    }

}