package com.base.datarecovery.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.datarecovery.activity.repeat.RepeatActivity
import com.base.datarecovery.activity.repeat.RepeatAnimationActivity
import com.base.datarecovery.activity.screenshot.ScreenShotActivity
import com.base.datarecovery.ads.AdmobMaxHelper
import com.base.datarecovery.databinding.ActivityPhotoManagerBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.FileHelp.getDirFiles
import com.base.datarecovery.help.KotlinExt.toFormatSize
import com.base.datarecovery.help.PermissionHelp.checkStorePermission
import com.base.datarecovery.help.PermissionHelp.requestStorePermission
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.utils.FileHexEx.isImage
import com.base.datarecovery.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())
        AdmobMaxHelper.admobMaxShowNativeAd(this,binding.idFlNative,1)
        val tittle = intent.extras?.getString("tittle")
        tittle?.let { binding.tvTittle.text = it }

        if (checkStorePermission()) {
            initDataSize()
        } else {
            showGerPermission(desc = "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))
        }
        binding.cardRepeatPhotos.setOnClickListener {
            startActivity(Intent(this@PhotoManagerActivity, RepeatAnimationActivity::class.java))
        }
    }

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

        RepeatActivity.beanList.addAll(RepeatActivity.getSimilarList(this@PhotoManagerActivity))
        val photoSize = RepeatActivity.beanList.sumOf { it.beans.sumOf { s -> File(s.path).length() } }.toFormatSize()

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

    }


}