package fsir.tqqe

import android.app.Activity
import android.graphics.Bitmap
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.test.basd.supercleanermax.R
import pxds.lmmd.zmwf.Khko.string

class Oqqo(private val mContext: Activity) : Hpwp.OnItemSelectedListener, Rfeq.BitmapCallback, OnSeekBarChangeListener {

    private var mIndex = 0

    private val mList: List<Rdrk>

    private val mSeekBar: SeekBar

    private var mAfterSize: TextView? = null

    private var mBeforeSize: TextView? = null

    private var mLoadingView: ProgressBar? = null

    private var mPreviewView: RelativeLayout? = null

    private var galleryRecyclerView: RecyclerView? = null

    private var lastProgress = 50

    private var lastRefreshTime = 0L

    private val mAfterImage: ImageView

    private val mBeforeImage: ImageView

    private var mTitleView: TextView? = null

    private var galleryAdapter: Lfhy? = null

    fun getMode() = mSeekBar.progress

    override fun onBitmapReady(bitmap: Bitmap?, size: Long, position: Int) {
        if (mIndex != position) {
            return
        }

        mContext.runOnUiThread {
            val entity = mList[position]
            mPreviewView?.visibility = View.VISIBLE
            mLoadingView?.visibility = View.GONE
            Glide.with(mContext).load(entity.src).into(mBeforeImage)
            mBeforeSize?.text = entity.sizeText
            mAfterSize?.text = Lpgx.formatFileSize(size)
            Glide.with(mContext).load(bitmap).into(mAfterImage)
        }
    }

    private fun loading(boolean: Boolean) {
        mPreviewView?.visibility = View.GONE
        mLoadingView?.visibility = View.VISIBLE
        if (boolean) {
            mBeforeSize?.text = R.string.ޅޔބސބ_D.string()
        }
        mAfterSize?.text = R.string.ޅޔބސބ_D.string()
    }

    private fun refreshPreviewImage(boolean: Boolean, mode: Int) {
        loading(boolean)
        val entity = mList[mIndex]
        mTitleView?.text = R.string.ހއތސޚ_D.string() + (mIndex + 1) + R.string.ހގބޅޔ_D.string((mList.size).toString())
        Rfeq.compressImage(entity.src, mode, mIndex, this)
    }

    private fun initView() {
        galleryAdapter = Lfhy(mContext, mList)
        val manager = Hpwp(Hpwp.HORIZONTAL)
        manager.setCallbackInFling(true)
        manager.setOnItemSelectedListener(this)
        manager.attach(galleryRecyclerView)
        manager.setItemTransformer(Uihs())

        galleryRecyclerView?.adapter = galleryAdapter
        mSeekBar.setOnSeekBarChangeListener(this)
    }

    init {
        mList = Gtor.getInstance().photoList
        galleryRecyclerView = mContext.findViewById(R.id.kolvpvp)
        mPreviewView = mContext.findViewById(R.id.dmsjgby)
        mLoadingView = mContext.findViewById(R.id.wnlcbbb)
        mBeforeImage = mContext.findViewById(R.id.bdxzlvr)
        mAfterImage = mContext.findViewById(R.id.dgdeicn)
        mBeforeSize = mContext.findViewById(R.id.smlsxkd)
        mAfterSize = mContext.findViewById(R.id.zwgfijx)
        mTitleView = mContext.findViewById(R.id.gsdtmyk)
        mSeekBar = mContext.findViewById(R.id.sbredam)
        initView()
    }

    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
    }

    override fun onItemSelected(recyclerView: RecyclerView?, item: View?, position: Int, newState: Int) {
        val currentTime = System.currentTimeMillis()

        if (newState != RecyclerView.SCROLL_STATE_IDLE || currentTime - lastRefreshTime < 300) {
            return
        }

        lastRefreshTime = currentTime
        mIndex = position

        refreshPreviewImage(true, 50)
    }

    override fun onStartTrackingTouch(seekBar: SeekBar?) {

    }

    override fun onStopTrackingTouch(seekBar: SeekBar?) {
        val progress = seekBar?.progress ?: 0
        var mode: Int = 0
        when {
            (seekBar?.progress ?: 0) <= 1 -> mode = 0
            (seekBar?.progress ?: 0) > 1 && (seekBar?.progress ?: 0) <= 50 -> mode = 50
            (seekBar?.progress ?: 0) > 50 -> mode = 100
        }
        if (progress != lastProgress) {
            refreshPreviewImage(false, mode)
            lastProgress = progress
        }
    }
}