package com.base.appzxhy.ui.recyclebin

import android.os.Environment
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.base.appzxhy.R
import com.base.appzxhy.base.BaseActivity
import com.base.appzxhy.business.ads.AdsMgr
import com.base.appzxhy.business.ads.AdsShowCallBack
import com.base.appzxhy.databinding.ActivityRecycleBinDetailBinding
import com.base.appzxhy.ui.recovery.FileRecoveredActivity
import com.base.appzxhy.ui.recyclebin.RecycleBinUtils.getImgRes
import com.base.appzxhy.ui.recyclebin.RecycleBinUtils.getType
import com.base.appzxhy.ui.recyclebin.RecycleBinUtils.mImageStr
import com.base.appzxhy.ui.recyclebin.RecycleBinUtils.mVideoStr
import com.base.appzxhy.utils.FileUtils.deleteIfExists
import com.base.appzxhy.utils.KotlinExt.toFormatSize
import com.base.appzxhy.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).placeholder(R.drawable.icon_file).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 {
            binding.v.visibility = View.VISIBLE
            onBackPressedDispatcher.onBackPressed()
            finish()
        }
        binding.ivDelete.setOnClickListener {
            lifecycleScope.launch {
                recycleBinFile.deleteIfExists()
            }
            onBackPressedDispatcher.onBackPressed()
            finish()
        }
        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())
                    })
                    finish()
                }
            })

        }
    }

    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()

    }

}