package com.test.basd.smartjunkcleaner.activity.photocompress.photo

import android.app.Activity
import android.graphics.Bitmap
import android.util.Log
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.smartjunkcleaner.R


class PreviewCompressionManager : GalleryLayoutManager.OnItemSelectedListener, CompressionImageUtils.BitmapCallback,
    OnSeekBarChangeListener {

    private val mContext: Activity
    private var galleryRecyclerView: RecyclerView? = null
    private var galleryAdapter: GalleryAdapter? = null
    private val mList: List<PhotoBean>
    private var mTitleView: TextView? = null

    private var mPreviewView: RelativeLayout? = null
    private var mLoadingView: ProgressBar? = null

    private val mBeforeImage: ImageView
    private val mAfterImage: ImageView

    private var mBeforeSize: TextView? = null
    private var mAfterSize: TextView? = null

    private val mSeekBar: SeekBar

    private var mIndex = 0

    constructor(mContext: Activity) {
        this.mContext = mContext
        mList = PhotoCache.getInstance().photoList
        galleryRecyclerView = mContext.findViewById(R.id.galleryRecyclerView)
        mPreviewView = mContext.findViewById(R.id.preview_view)
        mLoadingView = mContext.findViewById(R.id.img_loading)
        mBeforeImage = mContext.findViewById(R.id.before_image)
        mAfterImage = mContext.findViewById(R.id.after_image)
        mBeforeSize = mContext.findViewById(R.id.before_size)
        mAfterSize = mContext.findViewById(R.id.after_size)
        mTitleView = mContext.findViewById(R.id.title)
        mSeekBar = mContext.findViewById(R.id.seekBar)
        initView()
    }

    private fun initView() {
        galleryAdapter = GalleryAdapter(mContext, mList)
        val manager = GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL)
        manager.setCallbackInFling(true) //
        manager.setOnItemSelectedListener(this)
        manager.attach(galleryRecyclerView)
        manager.setItemTransformer(Transformer())

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

    private var lastRefreshTime = 0L

    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

        Log.d("glc","")
        refreshPreviewImage(true)
    }

    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
            if (mBeforeImage != null) {
                Glide.with(mContext).load(entity.src).into(mBeforeImage)
            }
            mBeforeSize?.text = entity.sizeText
            mAfterSize?.text = ImagesMediaUtils.formatFileSize(size)
            if (mAfterImage != null) {
                Glide.with(mContext).load(bitmap).into(mAfterImage)
            }
        }
    }

    private fun loading(boolean: Boolean) {
        mPreviewView?.visibility = View.GONE
        mLoadingView?.visibility = View.VISIBLE
        if (boolean) {
            mBeforeSize?.text = "Loading..."
        }
        mAfterSize?.text = "Loading..."
    }

    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
        when {
            progress <= 25 -> seekBar?.progress = 0
            progress in 26..75 -> seekBar?.progress = 50
            progress > 75 -> seekBar?.progress = 100
        }
    }

    override fun onStartTrackingTouch(seekBar: SeekBar?) {
    }

    private var lastProgress = 50

    override fun onStopTrackingTouch(seekBar: SeekBar?) {
        val progress = seekBar?.progress ?: 0

        when {
            seekBar?.progress ?: 0 <= 1 -> seekBar?.progress = 0
            (seekBar?.progress ?: 0) > 1 && (seekBar?.progress ?: 0) <= 50 -> seekBar?.progress =
                50

            seekBar?.progress ?: 0 > 50 -> seekBar?.progress = 100
        }
        if (progress != lastProgress) {
            refreshPreviewImage(false)
            lastProgress = progress
        }
    }

    private fun refreshPreviewImage(boolean: Boolean) {
        loading(boolean)
        val entity = mList[mIndex]
        mTitleView?.text = "Preview (" + (mIndex + 1) + "/${mList.size})"
        CompressionImageUtils.compressImage(entity.src, mSeekBar.progress, mIndex, this)
    }

    fun getMode() = mSeekBar.progress
}