package fsir.tqqe

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.HddwmjBinding
import pxds.dlpf.buoz.fico.Dsce
import pxds.etmo.Iqnb
import pxds.lmmd.zmwf.Bpfw
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.jyyf.hwbn.wzxb.Utdc

class Tzmt : Bpfw<HddwmjBinding>(), Rfeq.BitmapCallback, Rfeq.OnSaveBitmapListener {

    private var mNum = 0

    private var mProgressBar: ProgressBar? = null

    private var mode = 50;

    private var mSizeCount: Long = 0L

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

    private var mTextProgress: TextView? = null

    private fun onSuccess() {
        Utdc.showInterstitialAd(this){
            val intent = Intent(this, Iqnb::class.java)
            intent.putExtra(R.string.ޣޟމޅކ_D.string(), Dsce.PHOTO_COMPRESS)
            intent.putExtra(R.string.ޘބޅޔފ_D.string(), mNum)
            intent.putExtra(R.string.ޜޑސއޗ_D.string(), mSizeCount)
            startActivity(intent)
            finish()
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        Gtor.getInstance().clearCache()
    }

    override fun onSaveFailed() {

    }

    override fun onSaveSuccess() {
        mNum += 1
        updateProgress()
    }

    override fun onBackPressed() {

            super.onBackPressed()

    }

    override fun onBitmapReady(bitmap: Bitmap?, size: Long, position: Int) {
        val entity = Gtor.getInstance().getPositionData(position)
        if (bitmap != null && entity != null) {
            mSizeCount += (entity.size - size)
            Rfeq.saveBitmapToFile(entity.src, mode, bitmap, this, this)
        }
    }

    private fun updateProgress() {
        mTextProgress?.text = mNum.toString() + "/" + Gtor.getInstance().count
        mProgressBar?.progress = mNum
        if (mNum == mProgressBar?.max) {
            onSuccess()
        }
    }

    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        binding.axywelq.setTrackedOnClickListener {
            finishToMain()
        }
        mTextProgress = findViewById(R.id.yxhgaus)
        mProgressBar = findViewById(R.id.mlyeico)
        mode = intent.getIntExtra(R.string.ވމތވއ_D.string(), 50)
        Gtor.getInstance().photoList?.forEachIndexed { index, it ->
            Rfeq.compressImage(it.src, mode, index, this@Tzmt)
        }
        mProgressBar?.max = Gtor.getInstance().count
        updateProgress()
    }
}