package com.easy.clean.ui.recyclebin

import android.os.Environment
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.easy.clean.R
import com.easy.clean.base.BaseActivity
import com.easy.clean.business.ads.AdsMgr
import com.easy.clean.business.ads.AdsShowCallBack
import com.easy.clean.databinding.ActivityRecycleBinDetailBinding
import com.easy.clean.ui.recovery.FileRecoveredActivity
import com.easy.clean.ui.recyclebin.RecycleBinUtils.getImgRes
import com.easy.clean.ui.recyclebin.RecycleBinUtils.getType
import com.easy.clean.ui.recyclebin.RecycleBinUtils.mImageStr
import com.easy.clean.ui.recyclebin.RecycleBinUtils.mVideoStr
import com.easy.clean.utils.FileUtils.deleteIfExists
import com.easy.clean.utils.KotlinExt.toFormatSize
import com.easy.clean.utils.KotlinExt.toFormatTime
import com.bumptech.glide.Glide
import kotlinx.coroutines.launch
import java.io.File

class RecycleBinDetailActivity : BaseActivity<ActivityRecycleBinDetailBinding>(ActivityRecycleBinDetailBinding::inflate) {

    companion object {
        lateinit var recycleBinFile: File
    }

    override fun initView() {
        super.initView()
        if (mImageStr.contains("." + recycleBinFile.extension) || mVideoStr.contains("." + recycleBinFile.extension)) {
            Glide.with(this).load(recycleBinFile.absoluteFile).into(binding.iv)
        } else {
            binding.iv.setImageResource(recycleBinFile.getImgRes())
        }
        binding.tvPath.text = recycleBinFile.absolutePath
        binding.tvSize.text = recycleBinFile.length().toFormatSize()
        binding.tvTime.text = recycleBinFile.lastModified().toFormatTime()

        functionInAd {
            binding.v.visibility = View.GONE
        }
    }

    override fun initListener() {
        super.initListener()


        binding.ivBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.ivDelete.setOnClickListener {
            lifecycleScope.launch {
                recycleBinFile.deleteIfExists()
            }

        }
        binding.tvRestore.setOnClickListener {
            lifecycleScope.launch {
                restore()
            }
            AdsMgr.showInsert(this, showCallBack = object : AdsShowCallBack() {
                override fun show() {
                    super.show()
                    binding.v.visibility = View.VISIBLE
                }
                override fun next() {
                    goToAc(FileRecoveredActivity::class.java, exb = {
                        putExtra("Number", 1)
                        putExtra("ScanType", recycleBinFile.getType())
                    })
                }
            })

        }
    }

    fun restore() {
        val appName = getString(R.string.app_name).replace("/", " ")
        val appDir = File(Environment.getExternalStorageDirectory(), appName)
        val dir = File(appDir, "Restore")
        if (!dir.exists()) {
            dir.mkdirs()
        }
        val recoveryFile = File(dir, recycleBinFile.name)
        recycleBinFile.copyTo(recoveryFile, true)
        recycleBinFile.deleteIfExists()

    }

}