package negligentlyic.wrongic.bottomic

import android.os.Environment
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.easy.clean.R
import tooic.fatalityic.jittersic.Reinstateic
import autonomyic.polemicalic.congestionic.Unwindic
import autonomyic.polemicalic.congestionic.NASAic
import com.easy.clean.databinding.MoldingicBinding
import warpic.Nauticalic
import negligentlyic.wrongic.bottomic.Timidic.getImgRes
import negligentlyic.wrongic.bottomic.Timidic.getType
import negligentlyic.wrongic.bottomic.Timidic.mImageStr
import negligentlyic.wrongic.bottomic.Timidic.mVideoStr
import tooic.fatalityic.redic.speckledic.hankeringic.Incomparableic.deleteIfExists
import tooic.fatalityic.redic.speckledic.hankeringic.Curableic.toFormatSize
import tooic.fatalityic.redic.speckledic.hankeringic.Curableic.toFormatTime
import com.bumptech.glide.Glide
import kotlinx.coroutines.launch
import java.io.File

class Givenic : Reinstateic<MoldingicBinding>(MoldingicBinding::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.ladleic)
        } else {
            binding.ladleic.setImageResource(recycleBinFile.getImgRes())
        }
        binding.informalic.text = recycleBinFile.absolutePath
        binding.prospectic.text = recycleBinFile.length().toFormatSize()
        binding.chowderic.text = recycleBinFile.lastModified().toFormatTime()

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

    override fun initListener() {
        super.initListener()


        binding.telecommutingic.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.Latinic.setOnClickListener {
            lifecycleScope.launch {
                recycleBinFile.deleteIfExists()
            }

        }
        binding.stiflingic.setOnClickListener {
            lifecycleScope.launch {
                restore()
            }
            Unwindic.showInsert(this, showCallBack = object : NASAic() {
                override fun show() {
                    super.show()
                    binding.troubleic.visibility = View.VISIBLE
                }
                override fun next() {
                    goToAc(Nauticalic::class.java, exb = {
                        putExtra("Number", 1)
                        putExtra("ScanType", recycleBinFile.getType())
                    })
                }
            })

        }
    }

    fun restore() {
        val appName = getString(R.string.overrodeic).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()

    }

}