package com.zxhyis.toolss.ui.page.photocompress.photo

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.airbnb.lottie.LottieAnimationView
import com.blankj.utilcode.util.BarUtils
import com.zxhyis.toolss.R
import com.zxhyis.toolss.ad.AdmobUtils
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.databinding.ActivityCompressioningBinding
import com.zxhyis.toolss.logic.setOnClick
import com.zxhyis.toolss.ui.AFunOb
import com.zxhyis.toolss.ui.page.result.ResultActivity

class CompressioningActivity : TopActivity<ActivityCompressioningBinding>(), CompressionImageUtils.BitmapCallback,
    CompressionImageUtils.OnSaveBitmapListener {


    private var mTextProgress: TextView? = null
    private var mProgressBar: ProgressBar? = null
    private var mNum = 0

    private var mode = 50;


    override fun getViewBinding1(): ActivityCompressioningBinding {
        return ActivityCompressioningBinding.inflate(layoutInflater)
    }

    override fun init() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        vb.root.updatePadding(top = BarUtils.getStatusBarHeight())
        vb.fanhui.setOnClick(javaClass.simpleName){
            finishToMain()
        }
        mTextProgress = findViewById(R.id.text_progress)
        mProgressBar = findViewById(R.id.compressing_progress)
        mode = intent.getIntExtra("mode", 50)
        initAnimView()
        PhotoCache.getInstance().photoList?.forEachIndexed { index, it ->
            CompressionImageUtils.compressImage(it.src, mode, index, this@CompressioningActivity)
        }
        mProgressBar?.max = PhotoCache.getInstance().count
        updateProgress()
    }

    private var mSizeCount: Long = 0L

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

    private fun initAnimView() {
        val animationView = findViewById<LottieAnimationView>(R.id.compressing_animation)
        animationView.setAnimation("compressioning.json")
        animationView.loop(true)
        animationView.playAnimation()
    }

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

    override fun onSaveFailed() {

    }

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

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

    private fun onSuccess() {
        AdmobUtils.showInterstitialAd(this){
            val intent = Intent(this, ResultActivity::class.java)
            intent.putExtra("from", AFunOb.PHOTO_COMPRESS)
            intent.putExtra("num", mNum)
            intent.putExtra("size", mSizeCount)
            startActivity(intent)
            finish()
        }

    }

    override fun onBackPressed() {
//        val dialog = CustomDialog(this, R.layout.dialog_exit)
//        dialog.setButtonClickListener(R.id.cancel) {
//            dialog.dismiss()
//        }
//
//        dialog.setButtonClickListener(R.id.btnContinue) {
            super.onBackPressed()
//        }
//        dialog.show()
    }
}