package com.test.basd.onecleanmaster.activity.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.blankj.utilcode.util.BarUtils
import com.test.basd.onecleanmaster.R
import com.test.basd.onecleanmaster.activity.ResultActivity
import com.test.basd.onecleanmaster.databinding.ActivityCompressingBinding
import com.test.basd.onecleanmaster.helps.BaseActivity
import com.test.basd.onecleanmaster.helps.ads.AdmobUtils
import com.test.basd.onecleanmaster.view.AFunOb

class CompressingActivity : BaseActivity<ActivityCompressingBinding>(), CompressionImageUtils.BitmapCallback,
    CompressionImageUtils.OnSaveBitmapListener {


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

    private var mode = 50;

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

    override fun initView() {

        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        binding.fanhui.setOnClickListener {
            finishToMain()
        }
        mTextProgress = findViewById(R.id.text_progress)
        mProgressBar = findViewById(R.id.compressing_progress)
        mode = intent.getIntExtra("mode", 50)
        PhotoCache.getInstance().photoList?.forEachIndexed { index, it ->
            CompressionImageUtils.compressImage(it.src, mode, index, this@CompressingActivity)
        }
        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)
        }
    }


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