package com.base.filerecoveryrecyclebin.activity.photomanager

import android.content.Intent
import android.graphics.Color
import android.os.Environment
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.base.filerecoveryrecyclebin.activity.repeat.RepeatActivity
import com.base.filerecoveryrecyclebin.activity.screenshot.ScreenShotActivity
import com.base.filerecoveryrecyclebin.bean.MediaBean
import com.base.filerecoveryrecyclebin.databinding.ActivityPhotoManagerBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.FileHelp.getDirFiles
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatSize
import com.base.filerecoveryrecyclebin.help.MediaStoreHelp.getImageMedia
import com.base.filerecoveryrecyclebin.help.PermissionHelp.checkStorePermission
import com.base.filerecoveryrecyclebin.help.PermissionHelp.requestStorePermission
import com.base.filerecoveryrecyclebin.utils.BarUtils
import com.base.filerecoveryrecyclebin.utils.FileHexEx.isImage
import com.base.filerecoveryrecyclebin.view.DialogViews.showGerPermission
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class PhotoManagerActivity : BaseActivity<ActivityPhotoManagerBinding>() {

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


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        if (checkStorePermission()) {

            initDataSize()
        } else {
            showGerPermission("This feature requires access to your storage to scan your files and clean up screenshots. We will not transmit your data to any third-party service. Please grant permission so that we can provide you with better service.",
                deny = { finishToMain() }, allow = {
                    requestStorePermission(launcher) {
                        if (it) {
                            initDataSize()
                        } else {
                            finishToMain()
                        }
                    }
                })
        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.cardScreenshot.setOnClickListener {
            startActivity(Intent(this@PhotoManagerActivity, ScreenShotActivity::class.java))
            finish()
        }
        binding.cardRepeatPhotos.setOnClickListener {
            startActivity(Intent(this@PhotoManagerActivity, RepeatActivity::class.java))
            finish()
        }
    }

    private fun initDataSize() = lifecycleScope.launch(Dispatchers.IO) {
        val dcimSize =
            getDirFiles(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)).filter { isImage(it) }
                .sumOf { it.length() }.toFormatSize()

        val list = arrayListOf<MediaBean>()
        getImageMedia(list)
        val photoSize = (list.map { File(it.path) }.sumOf { it.length() } / 2).toFormatSize()

        launch(Dispatchers.Main) {
            binding.tvScreenshotSize.text = dcimSize
            binding.tvDuplicateSize.text = photoSize
        }

    }


}