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

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.view.View
import android.view.View.OnClickListener
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.test.basd.smartjunkcleaner.R

class PhotoListManager(context: Activity) : OnItemClickListener<PhotoBean>, SpanSizeLookup(), OnClickListener {

    private var mRecycleView: RecyclerView? = null

    private var mBtnContinue: TextView? = null
    private val mSelectAll: TextView

    private val mContext: Context = context


    private var mCompressionPhotoListAdapter: CompressionPhotoListAdapter? = null

    init {
        mRecycleView = context.findViewById(R.id.content)
        mBtnContinue = context.findViewById(R.id.btn_continue)
        mSelectAll = context.findViewById(R.id.selectAll)
        init()
    }


    private fun init() {
        (mRecycleView?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        mCompressionPhotoListAdapter = CompressionPhotoListAdapter(this)
        val layoutManager = GridLayoutManager(mContext, 3)
        layoutManager.spanSizeLookup = (this)
        mRecycleView?.layoutManager = layoutManager
        mRecycleView?.adapter = mCompressionPhotoListAdapter
        mBtnContinue?.setOnClickListener(this)
        mSelectAll.setOnClickListener(this)
    }

    override fun getSpanSize(position: Int): Int {
        val entity = mCompressionPhotoListAdapter?.getData()?.get(position)
        return if ((entity?.type ?: 0) != 0) {
            3
        } else {
            1
        }
    }

    override fun onUpdateBtn(isChecked: Boolean, entity: PhotoBean) {
        PhotoCache.getInstance().let {
            if (isChecked) it.addPhoto(entity) else it.removePhoto(entity)
        }
        updateBtnContinue()
    }

    override fun onItemClick(position: Int, entity: PhotoBean) {
    }

    fun onSelectAlbum(album: AlbumBean) {
        val list = ImagesMediaUtils.getPhotosForFolder(album.name, mContext)
        if (list.isNotEmpty()) {
            PhotoCache.getInstance().clearCache()
            mCompressionPhotoListAdapter?.updateData(list)
            updateBtnContinue()
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_continue -> {
                val intent = Intent(mContext, PreviewCompressionPhotoActivity::class.java)
                intent.putExtra("count", PhotoCache.getInstance().photoList.size)
                mContext.startActivity(intent)
            }

            R.id.selectAll -> {
                mCompressionPhotoListAdapter?.setNoOrYesselectAll((mCompressionPhotoListAdapter?.isSelectAll() != true))
            }
        }

    }

    private fun updateBtnContinue() {
        mBtnContinue?.isEnabled = PhotoCache.getInstance().photoList.size > 0
        val int = PhotoCache.getInstance().photoList.size
        mBtnContinue?.text = if (mBtnContinue?.isEnabled == true) "CONTINUE ($int)" else "CONTINUE"

        mCompressionPhotoListAdapter?.isSelectAll()
        val drawable: Drawable? = if (mCompressionPhotoListAdapter?.isSelectAll() == true) {
            ContextCompat.getDrawable(mContext, R.mipmap.quanxuan_s)
        } else {
            ContextCompat.getDrawable(mContext, R.mipmap.quanxuan_n)
        }
        mSelectAll.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable, null, null)
    }
}