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

import android.app.Activity
import android.view.View
import android.view.View.OnClickListener
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.test.basd.smartjunkcleaner.R

class BottomSheetManager : BottomSheetBehavior.BottomSheetCallback, OnClickListener,
    OnItemClickListener<AlbumBean> {

    private val mContext: Activity

    private val mRecycleView: RecyclerView
    private val blackOverlay: View
    private val mAlbumName: TextView
    private val bottomSheetBehavior: BottomSheetBehavior<LinearLayout>

    private val listener: AlbumsSelectListener

    constructor(mContext: Activity, listener: AlbumsSelectListener) {
        this.mContext = mContext
        this.listener = listener
        mRecycleView = mContext.findViewById(R.id.album_content)
        blackOverlay = mContext.findViewById(R.id.black_overlay)
        val bottomSheet = mContext.findViewById<LinearLayout>(R.id.bottom_sheet)
        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
        mAlbumName = mContext.findViewById(R.id.albumname)
        init()
    }

    private fun init() {
        bottomSheetBehavior.addBottomSheetCallback(this)
        blackOverlay.setOnClickListener(this)
        mAlbumName?.setOnClickListener(this)

        mRecycleView.layoutManager = LinearLayoutManager(mContext)
        val mList = ImagesMediaUtils.getAlbums(mContext)
        val mAdapter = AlbumListAdapter(mList, this)
        mRecycleView.adapter = mAdapter
        if (mList.isNotEmpty()) {
            onItemClick(0, mList[0])
        }
    }

    override fun onStateChanged(bottomSheet: View, newState: Int) {
        when (newState) {
            BottomSheetBehavior.STATE_HIDDEN -> {
                blackOverlay.visibility = View.GONE
            }

            BottomSheetBehavior.STATE_COLLAPSED -> {
                blackOverlay.visibility = View.GONE
            }

            else -> {
                blackOverlay.visibility = View.VISIBLE
            }
        }
    }

    override fun onSlide(bottomSheet: View, slideOffset: Float) {
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.albumname -> {
                if (bottomSheetBehavior.state != BottomSheetBehavior.STATE_EXPANDED) {
                    bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
                }
            }

            R.id.black_overlay -> {
                bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
            }
        }
    }

    override fun onUpdateBtn(boolean: Boolean, entity: AlbumBean) {

    }

    override fun onItemClick(position: Int, entity: AlbumBean) {
        mAlbumName.text = entity.name
        bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
        listener.onSelectAlbum(entity)
    }
}

